MaartenGr / MaartenGr/BERTopic
Saving BERTopic model when using Parametric UMAP
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 7.8k
- Forks
- 920
- Avg merge
- 22h 24m
- Merged PRs (30d)
- 5
Description
Hi,
Thank you so much for all your bits of help. I created a model that suits all my needs and currently, the results are as expected. I need to save the model, load it, and transform the new data each month. I use Parametric UMAP instead of the original UMAP for dimensionality reduction as the parametric one produces deterministic results and is not batch-dependent. I am very satisfied with the outcome. However, the issue is that I cannot save the model. Whatever I do to save the model I fail. I was wondering if I could save the dimensionality reduction model (umap_model component of the bertopic) independently and replace it once I load the trained clustering model without disturbing the entire model. Do you have any advice? This is the last stage of my project and if I cannot save the model all my efforts will be in vain. I would really appreciate it if you could provide me with some options that may resolve this issue.
P.S.: When I try the safetensor or pytorch approach I get an error in loading:
```
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/tmp/ipykernel_331/1509421400.py in
1 from bertopic import BERTopic
----> 2 loaded_model = BERTopic.load('/home/mmotall/complaints_subcat/model_training/models_developped/bert/model-save-test/topics_model')
~/venv/lib/python3.7/site-packages/bertopic/_bertopic.py in load(cls, path, embedding_model)
3006 else:
3007 raise ValueError("Make sure to either pass a valid directory or HF model.")
-> 3008 topic_model = _create_model_from_files(topics, params, tensors, ctfidf_tensors, ctfidf_config, images)
3009
3010 # Replace embedding model if one is specifically chosen
~/venv/lib/python3.7/site-packages/bertopic/_bertopic.py in _create_model_from_files(topics, params, tensors, ctfidf_tensors, ctfidf_config, images)
4022
4023 # CountVectorizer
-> 4024 topic_model.vectorizer_model = CountVectorizer(**ctfidf_config["vectorizer_model"]["params"])
4025 topic_model.vectorizer_model.vocabulary_ = ctfidf_config["vectorizer_model"]["vectorizer_model"]["vocab"]
4026
~/venv/lib/python3.7/site-packages/sklearn/utils/validation.py in inner_f(*args, **kwargs)
61 extra_args = len(args) - len(all_args)
62 if extra_args <= 0:
---> 63 return f(*args, **kwargs)
64
65 # extra_args > 0
TypeError: __init__() got an unexpected keyword argument 'norm'
```
When I save the model as pickle, every aspect of the model is saved correctly other than dimensionality reduction model (parametric UMAP). Therefore I was thinking if I can save the parametric UMAP independently and later join it to the loaded bertopic model. Is it possible?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with BERTopic.load and the serialization paths mentioned in the report: safetensors, PyTorch, and pickle. Reproduce the Parametric UMAP case, including the reported CountVectorizer `norm` loading error, then check whether the saved model can be loaded and used to transform new data while retaining its dimensionality-reduction model.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- machine-learning
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100