In this notebook, we’ll implement models for intermittent or sparse data using the M5 dataset.
Poisson
distribution loss. By the end of this tutorial, you’ll have a
good understanding of these models and how to use them.
Outline:
Tip You can use Colab to run this Notebook interactively![]()
Warning
To reduce the computation time, it is recommended to use GPU. Using
Colab, do not forget to activate it. Just go to
Runtime>Change runtime type
and select GPU as hardware accelerator.
pip install neuralforecast
StatsForecast
class.
This method prints 8 random series from the dataset and is useful for
basic
EDA.
Auto
model contains a default search space that was extensively
tested on multiple large-scale datasets. Additionally, users can define
specific search spaces tailored for particular datasets and tasks.
First, we create a custom search space for the
AutoNHITS
and
AutoTFT
models. Search spaces are specified with dictionaries, where keys
corresponds to the model’s hyperparameter and the value is a Tune
function to specify how the hyperparameter will be sampled. For example,
use randint
to sample integers uniformly, and choice
to sample
values of a list.
Auto
model you need to define:
h
: forecasting horizon.loss
: training and validation loss from
neuralforecast.losses.pytorch
.config
: hyperparameter search space. If None
, the Auto
class
will use a pre-defined suggested hyperparameter space.search_alg
: search algorithm (from tune.search
), default is
random search. Refer to
https://docs.ray.io/en/latest/tune/api_docs/suggestion.html for more
information on the different search algorithm options.num_samples
: number of configurations explored.h
as 28, use the Poisson
distribution
loss (ideal for count data) for training and validation, and use the
default search algorithm.
Tip The number of samples,Next, we use thenum_samples
, is a crucial parameter! Larger values will usually produce better results as we explore more configurations in the search space, but it will increase training times. Larger search spaces will usually require more samples. As a general rule, we recommend settingnum_samples
higher than 20.
Neuralforecast
class to train the Auto
model. In
this step, Auto
models will automatically perform hyperparamter tuning
training multiple models with different hyperparameters, producing the
forecasts on the validation set, and evaluating them. The best
configuration is selected based on the error on a validation set. Only
the best model is stored and used during inference.
predict
method to forecast the next 28 days using the
optimal hyperparameters.
cross_validation
method from the
NeuralForecast
class takes the following arguments.
df
: training data framestep_size
(int): step size between each window. In other words:
how often do you want to run the forecasting processes.n_windows
(int): number of windows used for cross validation. In
other words: what number of forecasting processes in the past do you
want to evaluate.cv_df
object is a new data frame that includes the following
columns:
unique_id
: contains the id corresponding to the time seriesds
: datestamp or temporal indexcutoff
: the last datestamp or temporal index for the n_windows. If
n_windows=1, then one unique cuttoff value, if n_windows=2 then two
unique cutoff values.y
: true value"model"
: columns with the model’s name and fitted value.unique_id | ds | cutoff | AutoNHITS | AutoNHITS-median | AutoNHITS-lo-90 | AutoNHITS-lo-80 | AutoNHITS-hi-80 | AutoNHITS-hi-90 | AutoTFT | AutoTFT-median | AutoTFT-lo-90 | AutoTFT-lo-80 | AutoTFT-hi-80 | AutoTFT-hi-90 | y | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | FOODS_3_001_CA_1 | 2016-02-29 | 2016-02-28 | 0.550 | 0.0 | 0.0 | 0.0 | 2.0 | 2.0 | 0.775 | 1.0 | 0.0 | 0.0 | 2.0 | 2.0 | 0.0 |
1 | FOODS_3_001_CA_1 | 2016-03-01 | 2016-02-28 | 0.611 | 0.0 | 0.0 | 0.0 | 2.0 | 2.0 | 0.746 | 1.0 | 0.0 | 0.0 | 2.0 | 2.0 | 1.0 |
2 | FOODS_3_001_CA_1 | 2016-03-02 | 2016-02-28 | 0.567 | 0.0 | 0.0 | 0.0 | 2.0 | 2.0 | 0.750 | 1.0 | 0.0 | 0.0 | 2.0 | 2.0 | 1.0 |
3 | FOODS_3_001_CA_1 | 2016-03-03 | 2016-02-28 | 0.554 | 0.0 | 0.0 | 0.0 | 2.0 | 2.0 | 0.750 | 1.0 | 0.0 | 0.0 | 2.0 | 2.0 | 0.0 |
4 | FOODS_3_001_CA_1 | 2016-03-04 | 2016-02-28 | 0.627 | 0.0 | 0.0 | 0.0 | 2.0 | 2.0 | 0.788 | 1.0 | 0.0 | 0.0 | 2.0 | 3.0 | 0.0 |
metric | AutoNHITS | AutoTFT | cutoff | |
---|---|---|---|---|
0 | mse | 10.059308 | 10.909020 | 2016-02-28 |
1 | mae | 1.485914 | 1.554572 | 2016-02-28 |
0 | mse | 9.590549 | 10.253903 | 2016-03-27 |
1 | mae | 1.494229 | 1.561868 | 2016-03-27 |
0 | mse | 9.596170 | 10.300666 | 2016-04-24 |
1 | mae | 1.501949 | 1.564157 | 2016-04-24 |