https://github.com/nixtla logo
#neural-forecast
Title
# neural-forecast
l

lobbie lobbie

11/04/2023, 11:39 AM
Hi, With reference to documentation on saving and loading neural forecast models. it stated to use model_index to save required models and for each model saved, 1 ckpt and 1 pkl with model name and suffix will be saved i.e.
[model_name]_[suffix].ckpt
[model_name]_[suffix].pkl
Is it possible to save these using custom names? I need to follow some standard naming convention and I don't want to manually rename the files. Also, when I saved the models as per documentation, it did not save the 3 models with their respective suffix. i only get 2 files instead of 6. is there something i am not doing right? Thanks,
j

José Morales

11/06/2023, 3:55 PM
Hey. Can you provide a sample filename that you want to use? The names are important because they're used to figure out which model you're loading and set it up correctly. I think that documentation is outdated, the files saved are: • Model's checkpoints • Configuration • Dataset (if
save_dataset=True
)
l

lobbie lobbie

11/06/2023, 10:04 PM
Good morning @José Morales, I was using the sample code from the documentation,
nf.save(path='./checkpoints/test_run/',
model_index=None,
overwrite=True,
save_dataset=True)
The model files saved were,
automlp_0.ckpt
configuration.pkl
dataset.pkl
nbeats_0.ckpt
nhits_0.ckpt
I noticed the saved files do not have the following pkl files as per documentation. Does the saved configuration.pkl covers all 3 models?
automlp_0.pkl
nbeats_0.pkl
nhits_0.pkl
I would like to use my own custom model filenames. Is this possible? If yes, what should the code be like?
Acc_Model_UV_automlp_0.pkl
Acc_Model_UV_automlp_0.ckpt
Acc_Model_UV_nbeats_0.pkl
Acc_Model_UV_nbeats_0.ckpt
Acc_Model_UV_nhits_0.pkl
Acc_Model_UV_nhits_0.ckpt
Thanks.
j

José Morales

11/06/2023, 10:53 PM
Yes, the configuration is general, so only the model weights are required for each model. About the filenames, those won't work. The current logic of the load method is to split by underscore and take the first entry as the model name. If you want to use those filenames you can manually save and load them yourself, e.g. for save:
Copy code
nf.save(path, model_index=[]) #  this doesn't save the models
for model in self.models:
    model.save(your_custom_path)
load:
Copy code
from neuralforecast.core import MODEL_FILENAME_DICT

nf = NeuralForecast.load(path)
for fname in your_custom_path:
    # extract the model name from the filename
    model_name = fname.split('_')[3]  # this would extract the automlp model from your sample path (Acc_Model_UV_automlp_0.pkl)
    model = MODEL_FILENAME_DICT[model_name].load_from_checkpoint(fname)
    nf.models.append(model)
👍 1
l

lobbie lobbie

11/06/2023, 11:17 PM
@José Morales, many thanks for the workaround code. I think I can use it. So, the configuration,pkl file is not required for loading the models and for future period predictions? if not required for predictions, then I will just disregard it.
j

José Morales

11/06/2023, 11:18 PM
The configuration is required for the predictions if you're using a dataset, i.e. predicting for the same series. If you use something like
nf.predict(new_df)
then you don't need it. If you do need it you can use the snippets I provided above and those will save and load the configuration
👍 1
l

lobbie lobbie

11/08/2023, 8:07 AM
@José Morales, sorry to bump this thread. are we able to save and load nfc models using pickle? if yes, what would the code be like? My team also suggested i auto rename the saved model files to the names i need?
j

José Morales

11/08/2023, 3:57 PM
If you want to you can just save the whole object with something like:
Copy code
nf = NeuralForecast(...)
...
with open('nf.pkl', 'wb') as f:
    pickle.dump(nf, f)
that'd have the dataset, models, etc
l

lobbie lobbie

11/09/2023, 6:52 AM
@José Morales, pickle worked! thank you! I can now save the nfc model in custom model file name. I have tested loading the pickle file and predict. The prediction results are correct.
👍 1