Tutorial on how to add new models to NeuralForecast
Prerequisites This Guide assumes advanced familiarity with NeuralForecast. We highly recommend reading first the Getting Started and the NeuralForecast Map tutorials! Additionally, refer to the CONTRIBUTING guide for the basics how to contribute to NeuralForecast.
core
class simplifies building entire pipelines, both for
industry and academia, on any dataset, with user-friendly methods such
as fit
and predict
.
Adding a new model to NeuralForecast is simpler than building a new
PyTorch model from scratch. You only need to write the forward method.
It has the following additional advantages:
BaseModel
classes provide common optimization components, such
as early stopping and learning rate schedulers.MLP
model, which does not include exogenous covariates.
At a given timestamp , the
MLP
model will forecast the next values of the univariate target time,
, using as inputs the last historical values, given by
. The following figure presents a diagram of the model.
neuralforecast
library.neuralforecast
library, core
dependencies, and nbdev
package to code your model in an
interactive notebook.BaseModel
)BaseModel
. Using class
attributes we can make this model recurrent or not, or multivariate or
univariate, or allow the use of exogenous inputs.
TimeSeriesLoader
module. The BaseModel
models will sample individual windows of size
input_size+h
, starting from random timestamps.
BaseModel
’ hyperparametersh
(horizon), input_size
, and optimization hyperparameters
such as learning_rate
, max_steps
, among others. The following list
presents the hyperparameters related to the sampling of windows:
h
(h): number of future values to predict.input_size
(L): number of historic values to use as input for the
model.batch_size
(bs): number of time series sampled by the loader
during training.valid_batch_size
(v_bs): number of time series sampled by the
loader during inference (validation and test).windows_batch_size
(w_bs): number of individual windows sampled
during training (from the previous time series) to form the batch.inference_windows_batch_size
(i_bs): number of individual windows
sampled during inference to form each batch. Used to control the GPU
memory.forward
method receives a batch of data in a dictionary with the
following keys:
insample_y
: historic values of the time series.insample_mask
: mask indicating the available values of the time
series (1 if available, 0 if missing).futr_exog
: future exogenous covariates (if any).hist_exog
: historic exogenous covariates (if any).stat_exog
: static exogenous covariates (if any).MULTIVARIATE = False
is set:
tensor | BaseModel |
---|---|
insample_y | (w_bs , L , 1 ) |
insample_mask | (w_bs , L ) |
futr_exog | (w_bs , L +h , n_f ) |
hist_exog | (w_bs , L , n_h ) |
stat_exog | (w_bs ,n_s ) |
forward
function should return a single tensor with the forecasts
of the next h
timestamps for each window. Use the attributes of the
loss
class to automatically parse the output to the correct shape (see
the example below).
Tip
Since we are using nbdev
, you can easily add prints to the code and
see the shapes of the tensors during training.
BaseModel
’ methodsBaseModel
class contains several common methods for all
windows-based models, simplifying the development of new models by
preventing code duplication. The most important methods of the class
are:
_create_windows
: parses the time series from the
TimeSeriesLoader
into individual windows of size input_size+h
._normalization
: normalizes each window based on the scaler
type._inv_normalization
: inverse normalization of the forecasts.training_step
: training step of the model, called by
PyTorch-Lightning’s Trainer
class during training (fit
method).validation_step
: validation step of the model, called by
PyTorch-Lightning’s Trainer
class during validation.predict_step
: prediction step of the model, called by
PyTorch-Lightning’s Trainer
class during inference (predict
method).BaseModel
class, the next step is
creating your particular model.
The main steps are:
nbs
folder
(https://github.com/Nixtla/neuralforecast/tree/main/nbs). It should
be named models.YOUR_MODEL_NAME.ipynb
.nbdev
file.__init__
method with the model’s inherited and
particular hyperparameters and instantiate the architecture.EXOGENOUS_FUTR
: if the model can handle future exogenous
variables (True) or not (False)EXOGENOUS_HIST
: if the model can handle historical exogenous
variables (True) or not (False)EXOGENOUS_STAT
: if the model can handle static exogenous
variables (True) or not (False)MULTIVARIATE
: If the model produces multivariate forecasts
(True) or univariate (False)RECURRENT
: If the model produces forecasts recursively (True)
or direct (False)forward
method, which recieves the input batch
dictionary and returns the forecast.nbdev
file.
Important Changemlp
to your model’s name, using lowercase and underscores. When you later runnbdev_export
, it will create aYOUR_MODEL.py
script in theneuralforecast/models/
directory.
Tip
Don’t forget to add the #| export
tag on this cell.
Next, create the class with the init
and forward
methods. The
following example shows the example for the simplified
MLP
model. We explain important details after the code.
Tip
- Don’t forget to add the
#| export
tag on each cell.- Larger architectures, such as Transformers, might require splitting the
forward
by using intermediate functions.
h
and input_size
). If you are unsure
of what default value to use, we recommend copying the default values
from existing models for most optimization and sampling hyperparameters.
You can change the default values later at any time.
The reshape
method at the end of the forward
step is used to adjust
the output shape. The loss
class contains an outputsize_multiplier
attribute to automatically adjust the output size of the forecast
depending on the loss
. For example, for the Multi-quantile loss
(MQLoss
),
the model needs to output each quantile for each horizon.
nbdev
allows for testing and documenting the model during the
development process. It allows users to iterate the development within
the notebook, testing the code in the same environment. Refer to
existing models, such as the complete MLP model
here.
These files already contain the tests, documentation, and usage examples
that were used during the development process.
nbdev
neuralforecast
folder with
the actual scripts.
To export the model, run nbdev_export
in your terminal. You should see
a new file with your model in the neuralforecast/models/
folder.
core
class and additional files:
core
class, using the nbdev
file
here:
MODEL_FILENAME_DICT
dictionary (used for
the save
and load
functions).BaseModel
class hyperparameters and
input/output shapes of the forward
method.nbs
folder:
models.YOUR_MODEL_NAME.ipynb
init
and forward
methods and set the class attributes.nbdev_export
.core
class
here.