Open In Colab

image.png

Save and load a Forecaster#

Introduction#

Once we have trained a forecaster, we usually need to save the model and load it later for further training, either model evaluation or model deployment. In this guidance, we demonstrate how to save and load a forecaster in detail.

We will take TCNForecaster and nyc_taxi dataset as an example in this guide.

Setup#

Before we begin, we need to install chronos if it isn’t already available, we choose to use pytorch as deep learning backend.

[ ]:
!pip install --pre --upgrade bigdl-chronos[pytorch]
!pip uninstall -y torchtext # uninstall torchtext to avoid version conflict

Forecaster preparation#

Before saving a forecaster, a forecaster should be created and trained. The training process is introduced in the previous guidance Train forcaster on single node in detail, therefore we directly create and train a TCNForecaster based on the nyc taxi dataset.

[ ]:
# get TSDataset for training and testing
tsdata_train, tsdata_test = get_data()
# get a trained forecaster
forecaster = get_trained_forecaster(tsdata_train)

Save and Load#

After you have trained a forecaster, you can simply save your forecaster by calling save() with a filename. Then you should load it with the same filename.

[ ]:
forecaster.save("./forecaster.txt")
forecaster.load("./forecaster.txt")

If you are in a new session, then you should define a forecaster first, then load it by filename.

[ ]:
from bigdl.chronos.forecaster.tcn_forecaster import TCNForecaster
forecaster = TCNForecaster(past_seq_len=48,
                           future_seq_len=1,
                           input_feature_num=1,
                           output_feature_num=1)
forecaster.load("./forecaster.txt")