Errors when loading trained models
Closed this issue · 4 comments
Hi authors,
Many thanks for your excellent work and contributions! I am learning the tutorial "2021-12-26_MIRA_tutorial.ipynb".
When I load the trained model using mira.topic_model.ExpressionTopicModel.load()
, I got this error:
UnpicklingError Traceback (most recent call last)
/tmp/ipykernel_47536/3391690466.py in
----> 1 tt.load('./models/best_rna_model.pth')
~/miniconda3/envs/mira/lib/python3.7/site-packages/mira/topic_model/base.py in load(cls, filename)
165 '''
166
--> 167 data = torch.load(filename)
168
169 model = cls(**data['params'])
~/miniconda3/envs/mira/lib/python3.7/site-packages/torch/serialization.py in load(f, map_location, pickle_module, **pickle_load_args)
606 return torch.jit.load(opened_file)
607 return _load(opened_zipfile, map_location, pickle_module, **pickle_load_args)
--> 608 return _legacy_load(opened_file, map_location, pickle_module, **pickle_load_args)
609
610
~/miniconda3/envs/mira/lib/python3.7/site-packages/torch/serialization.py in _legacy_load(f, map_location, pickle_module, **pickle_load_args)
775 "functionality.")
776
--> 777 magic_number = pickle_module.load(f, **pickle_load_args)
778 if magic_number != MAGIC_NUMBER:
779 raise RuntimeError("Invalid magic number; corrupt file?")
UnpicklingError: NEWOBJ expected an arg tuple.
I also tried to load it using torch.load()
but it doesn't work.
I am wondering how I can load trained models (.pth files)?
Best,
Mingxi
Hi AL,
Thanks for your help! Yes, I specified the path for the .pth file in my "load" command.
My code for model building, saving and loading is as follows:
- to initialize a model for scRNA data:
example_rna_model = mira.topics.ExpressionTopicModel(exogenous_key='exog_feature', endogenous_key='highly_variable')
- to train and save the best model after hyperparameter tuning:
tuner = mira.topics.TopicModelTuner(example_rna_model, save_name="./my_folder/best_rna_model.pth")
tuner.train_test_split(rna_data, train_size = 0.8)
study = tuner.tune(rna_data)
example_rna_model = tuner.select_best_model(rna_data)
- to load the trained model:
rna_model_trained = mira.topic_model.ExpressionTopicModel.load('./my_folder/best_rna_model.pth')
Also, I tried to load the trained model using the following command, but it failed to load:
rna_model_trained = torch.load('./my_folder/best_rna_model.pth')
Please let me know if you need more code.
Thanks!
Mingxi
Thanks! I can save and load models now!
Thanks again for your help!