Transfer Learning
Transfer learning refers to the process 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.
For time series forecasting, the technique allows you to get lightning-fast predictions ⚡ bypassing the tradeoff between accuracy and speed (more than 30 times faster than our alreadsy fast autoARIMA for a similar accuracy).
This notebook shows how to generate a pre-trained model and store it in a checkpoint to make it available to forecast new time series never seen by the model.
Table of Contents
1. Installing NeuralForecast/DatasetsForecast
2. Load M4 Data
3. Instantiate NeuralForecast core, Fit, and
save
4. Load pre-trained model and predict on AirPassengers
5.
Evaluate Results
You can run these experiments using GPU with Google Colab.
1. Installing Libraries
This example will automatically run on GPUs if available. Make sure cuda is available. (If you need help to put this into production send us an email or join or community, we also offer a fully hosted solution)
2. Load M4 Data
The M4
class will automatically download the complete M4 dataset and
process it.
It return three Dataframes: Y_df
contains the values for the target
variables, X_df
contains exogenous calendar features and S_df
contains static features for each time-series (none for M4). For this
example we will only use Y_df
.
If you want to use your own data just replace Y_df
. Be sure to use a
long format and have a simmilar structure than our data set.
3. Model Train and Save
Using the
NeuralForecast.fit
method you can train a set of models to your dataset. You just have to
define the input_size
and horizon
of your model. The input_size
is
the number of historic observations (lags) that the model will use to
learn to predict h
steps in the future. Also, you can modify the
hyperparameters of the model to get a better accuracy.
Save model with core.NeuralForecast.save
method. This method uses
PytorchLightning save_checkpoint
function. We set save_dataset=False
to only save the model.
4. Transfer M4 to AirPassengers
We load the stored model with the core.NeuralForecast.load
method, and
forecast AirPassenger
with the core.NeuralForecast.predict
function.
5. Evaluate Results
We evaluate the forecasts of the pre-trained model with the Mean
Absolute Error
(mae
).