Saving and loading trained Deep Learning models has multiple valuable uses. These models are often costly to train; storing a pre-trained model can help reduce costs as it can be loaded and reused to forecast multiple times. Moreover, it enables Transfer learning capabilities, consisting of pre-training a flexible model on a large dataset and using it later on other data with little to no training. It is one of the most outstanding 🚀 achievements in Machine Learning 🧠 and has many practical applications. In this notebook we show an example on how to save and loadDocumentation Index
Fetch the complete documentation index at: https://nixtlaverse.nixtla.io/llms.txt
Use this file to discover all available pages before exploring further.
NeuralForecast models.
The two methods to consider are:1.
NeuralForecast.save: Saves
models into disk, allows save dataset and config.2.
NeuralForecast.load: Loads models from a given path.Important This Guide assumes basic knowledge on the NeuralForecast library. For a minimal example visit the Getting Started guide.You can run these experiments using GPU with Google Colab.
1. Installing NeuralForecast
2. Loading AirPassengers Data
For this example we will use the classical AirPassenger Data set. Import the pre-processed AirPassenger fromutils.
| unique_id | ds | y | |
|---|---|---|---|
| 0 | 1.0 | 1949-01-31 | 112.0 |
| 1 | 1.0 | 1949-02-28 | 118.0 |
| 2 | 1.0 | 1949-03-31 | 132.0 |
| 3 | 1.0 | 1949-04-30 | 129.0 |
| 4 | 1.0 | 1949-05-31 | 121.0 |
3. Model Training
Next, we instantiate and train three models:NBEATS, NHITS, and
AutoMLP. The models with their hyperparameters are defined in the
models list.
predict method.
| unique_id | ds | NBEATS | NHITS | AutoMLP | |
|---|---|---|---|---|---|
| 0 | 1.0 | 1961-01-31 | 446.882172 | 447.219238 | 454.914154 |
| 1 | 1.0 | 1961-02-28 | 465.145813 | 464.558014 | 430.188446 |
| 2 | 1.0 | 1961-03-31 | 469.978424 | 474.637238 | 458.478577 |
| 3 | 1.0 | 1961-04-30 | 493.650665 | 502.670349 | 477.244507 |
| 4 | 1.0 | 1961-05-31 | 537.569275 | 559.405212 | 522.252991 |

4. Save models
To save all the trained models use thesave method. This method will
save both the hyperparameters and the learnable weights (parameters).
The save method has the following inputs:
path: directory where models will be saved.model_index: optional list to specify which models to save. For example, to only save theNHITSmodel usemodel_index=[2].overwrite: boolean to overwrite existing files inpath. When True, the method will only overwrite models with conflicting names.save_dataset: boolean to saveDatasetobject with the dataset.
[model_name]_[suffix].ckpt: Pytorch Lightning checkpoint file with the model parameters and hyperparameters.[model_name]_[suffix].pkl: Dictionary with configuration attributes.
model_name corresponds to the name of the model in lowercase
(eg. nhits). We use a numerical suffix to distinguish multiple models
of each class. In this example the names will be automlp_0,
nbeats_0, and nhits_0.
Important TheAutomodels will be stored as their base model. For example, theAutoMLPtrained above is stored as anMLPmodel, with the best hyparparameters found during tuning.
5. Load models
Load the saved models with theload method, specifying the path, and
use the new nf2 object to produce forecasts.
| unique_id | ds | NHITS | NBEATS | AutoMLP | |
|---|---|---|---|---|---|
| 0 | 1.0 | 1961-01-31 | 447.219238 | 446.882172 | 454.914154 |
| 1 | 1.0 | 1961-02-28 | 464.558014 | 465.145813 | 430.188446 |
| 2 | 1.0 | 1961-03-31 | 474.637238 | 469.978424 | 458.478577 |
| 3 | 1.0 | 1961-04-30 | 502.670349 | 493.650665 | 477.244507 |
| 4 | 1.0 | 1961-05-31 | 559.405212 | 537.569275 | 522.252991 |


