--- title: Examples keywords: fastai sidebar: home_sidebar nb_path: "nbs/auto.ipynb" ---
{% raw %}
{% endraw %} {% raw %}
{% endraw %}

Base class

{% raw %}

class AutoBaseModel[source]

AutoBaseModel(n_time_out)

{% endraw %} {% raw %}
{% endraw %}

AutoNHITS

{% raw %}

class NHITS[source]

NHITS(n_time_out, space=None) :: AutoBaseModel

{% endraw %} {% raw %}

nhits_space[source]

nhits_space(n_time_out:int)

Suggested hyperparameters search space for tuning. To be used with hyperopt library.

Parameters

n_time_out: int Forecasting horizon.

Returns

space: Dict Dictionary with search space for hyperopt library.

{% endraw %} {% raw %}
{% endraw %}

AutoNBEATS

{% raw %}

class NBEATS[source]

NBEATS(n_time_out, space=None) :: AutoBaseModel

{% endraw %} {% raw %}

nbeats_space[source]

nbeats_space(n_time_out:int)

Suggested hyperparameters search space for tuning. To be used with hyperopt library.

Parameters

n_time_out: int Forecasting horizon.

Returns

space: Dict Dictionary with search space for hyperopt library.

{% endraw %} {% raw %}
{% endraw %}

AutoRNN

{% raw %}

class RNN[source]

RNN(n_time_out, space=None) :: AutoBaseModel

{% endraw %} {% raw %}

rnn_space[source]

rnn_space(n_time_out:int)

Suggested hyperparameters search space for tuning. To be used with hyperopt library. This space is not complete for training, will be completed automatically within the fit method of the AutoBaseModels.

Parameters
----------
n_time_out: int
    Forecasting horizon

Returns
----------
space: Dict
    Dictionary with search space for hyperopt library.
{% endraw %} {% raw %}
{% endraw %}

AutoNF

{% raw %}
{% endraw %} {% raw %}

class AutoNF[source]

AutoNF(config_dict, n_time_out)

{% endraw %} {% raw %}
{% endraw %}

The forecasting task we selected is to predict the number of patients with influenza-like illnesses from the US CDC dataset, the dataset contains 7 target variables, and has 966 weeks of history.

We will be creating point forecasts with N-BEATS, N-HiTS and RNN models. The predictive features will be the autoregressive features. More information on the dataset can be found in the N-HiTS paper.

Table of Contents

  1. Installing NeuralForecast Library
  2. Data Loading and Processing
  3. Define Hyperparameter Space
  4. Hyperparameter Tuning
  5. Evaluate Results

1. Installing Neuralforecast library

You can install the released version of NeuralForecast from the Python package index with:

{% raw %}
#!pip install neuralforecast
#!pip install matplotlib

import matplotlib.pyplot as plt
import neuralforecast as nf
from neuralforecast.data.datasets.long_horizon import LongHorizon
{% endraw %}

2. Data Loading and Processing

For this example we keep 10% of the observations as validation and use the latest 20% of the observations as the test set. To do so we use the sample_mask and declare the windows that will be used to train, and validate the model.

{% raw %}
Y_df, _, _ = LongHorizon.load(directory='./', group='ILI')
Y_df.head()
unique_id ds y
0 % WEIGHTED ILI 2002-01-01 -0.421499
1 % WEIGHTED ILI 2002-01-08 -0.331239
2 % WEIGHTED ILI 2002-01-15 -0.342763
3 % WEIGHTED ILI 2002-01-22 -0.199782
4 % WEIGHTED ILI 2002-01-29 -0.218426
{% endraw %} {% raw %}
n_series = len(Y_df.unique_id.unique())
n_time = len(Y_df.ds.unique()) # dataset is balanced

n_ts_test = 193
n_ts_val = 97

print('n_time', n_time)
print('n_series', n_series)
print('n_ts_test', n_ts_test)
print('n_ts_val', n_ts_val)
n_time 966
n_series 7
n_ts_test 193
n_ts_val 97
{% endraw %} {% raw %}
#                'AGE 5-24', 'ILITOTAL', 'NUM. OF PROVIDERS', 'OT']
y_plot = Y_df[Y_df.unique_id=='% WEIGHTED ILI'].y.values
x_plot = pd.to_datetime(Y_df[Y_df.unique_id=='% WEIGHTED ILI'].ds).values

plt.plot(x_plot, y_plot)
plt.axvline(x_plot[n_time-n_ts_val-n_ts_test], color='black', linestyle='-.')
plt.axvline(x_plot[n_time-n_ts_test], color='black', linestyle='-.')
plt.ylabel('Weighted ILI [ratio]')
plt.xlabel('Date')
plt.grid()
plt.show()
plt.close()
{% endraw %}

3.0 auto.NHITS

{% raw %}
auto_nhits = NHITS(n_time_out=24)
auto_nhits.space['max_steps'] = hp.choice('max_steps', [1]) # Override max_steps for faster example
{% endraw %} {% raw %}
auto_nhits.fit(Y_df=Y_df, X_df=None, S_df=None, hyperopt_steps=2,
               n_ts_val=n_ts_val,
               n_ts_test=n_ts_test,
               results_dir='./results/autonhits',
               save_trials=True,
               loss_function_val=nf.losses.numpy.mae,
               loss_functions_test={'mae':nf.losses.numpy.mae,
                                    'mse':nf.losses.numpy.mse},
               return_test_forecast=True,
               verbose=False)
INFO:hyperopt.tpe:build_posterior_wrapper took 0.014366 seconds
INFO:hyperopt.tpe:TPE using 0 trials
/Users/cchallu/opt/anaconda3/envs/neuralforecast/lib/python3.7/site-packages/pytorch_lightning/trainer/data_loading.py:133: UserWarning: The dataloader, val_dataloader 0, does not have many workers which may be a bottleneck. Consider increasing the value of the `num_workers` argument` (try 12 which is the number of cpus on this machine) in the `DataLoader` init to improve performance.
  f"The dataloader, {name}, does not have many workers which may be a bottleneck."
/Users/cchallu/opt/anaconda3/envs/neuralforecast/lib/python3.7/site-packages/torch/nn/functional.py:3635: UserWarning: Default upsampling behavior when mode=linear is changed to align_corners=False since 0.4.0. Please specify align_corners=True if the old behavior is desired. See the documentation of nn.Upsample for details.
  "See the documentation of nn.Upsample for details.".format(mode)
/Users/cchallu/opt/anaconda3/envs/neuralforecast/lib/python3.7/site-packages/pytorch_lightning/trainer/data_loading.py:133: UserWarning: The dataloader, train_dataloader, does not have many workers which may be a bottleneck. Consider increasing the value of the `num_workers` argument` (try 12 which is the number of cpus on this machine) in the `DataLoader` init to improve performance.
  f"The dataloader, {name}, does not have many workers which may be a bottleneck."
/Users/cchallu/opt/anaconda3/envs/neuralforecast/lib/python3.7/site-packages/pytorch_lightning/trainer/data_loading.py:133: UserWarning: The dataloader, predict_dataloader 0, does not have many workers which may be a bottleneck. Consider increasing the value of the `num_workers` argument` (try 12 which is the number of cpus on this machine) in the `DataLoader` init to improve performance.
  f"The dataloader, {name}, does not have many workers which may be a bottleneck."
INFO:hyperopt.tpe:build_posterior_wrapper took 0.013810 seconds
INFO:hyperopt.tpe:TPE using 1/1 trials with best loss 15.240788
<__main__.NHITS at 0x7f8a51b09fd0>
{% endraw %} {% raw %}
forecasts = auto_nhits.forecast(Y_df=Y_df)
forecasts
INFO:root:Train Validation splits

INFO:root:                                      ds           
                                     min        max
unique_id         sample_mask                      
% WEIGHTED ILI    0           2002-01-01 2020-06-30
                  1           2020-07-07 2020-12-15
%UNWEIGHTED ILI   0           2002-01-01 2020-06-30
                  1           2020-07-07 2020-12-15
AGE 0-4           0           2002-01-01 2020-06-30
                  1           2020-07-07 2020-12-15
AGE 5-24          0           2002-01-01 2020-06-30
                  1           2020-07-07 2020-12-15
ILITOTAL          0           2002-01-01 2020-06-30
                  1           2020-07-07 2020-12-15
NUM. OF PROVIDERS 0           2002-01-01 2020-06-30
                  1           2020-07-07 2020-12-15
OT                0           2002-01-01 2020-06-30
                  1           2020-07-07 2020-12-15
INFO:root:
Total data 			6930 time stamps 
Available percentage=100.0, 	6930 time stamps 
Insample  percentage=2.42, 	168 time stamps 
Outsample percentage=97.58, 	6762 time stamps 

/Users/cchallu/opt/anaconda3/envs/neuralforecast/lib/python3.7/site-packages/pytorch_lightning/trainer/connectors/callback_connector.py:91: LightningDeprecationWarning: Setting `Trainer(progress_bar_refresh_rate=1)` is deprecated in v1.5 and will be removed in v1.7. Please pass `pytorch_lightning.callbacks.progress.TQDMProgressBar` with `refresh_rate` directly to the Trainer's `callbacks` argument instead. Or, to disable the progress bar pass `enable_progress_bar = False` to the Trainer.
  f"Setting `Trainer(progress_bar_refresh_rate={progress_bar_refresh_rate})` is deprecated in v1.5 and"
/Users/cchallu/opt/anaconda3/envs/neuralforecast/lib/python3.7/site-packages/pytorch_lightning/trainer/data_loading.py:133: UserWarning: The dataloader, predict_dataloader 0, does not have many workers which may be a bottleneck. Consider increasing the value of the `num_workers` argument` (try 12 which is the number of cpus on this machine) in the `DataLoader` init to improve performance.
  f"The dataloader, {name}, does not have many workers which may be a bottleneck."
Predicting: 100%|██████████| 7/7 [00:00<00:00, 482.03it/s]
/Users/cchallu/opt/anaconda3/envs/neuralforecast/lib/python3.7/site-packages/torch/nn/functional.py:3635: UserWarning: Default upsampling behavior when mode=linear is changed to align_corners=False since 0.4.0. Please specify align_corners=True if the old behavior is desired. See the documentation of nn.Upsample for details.
  "See the documentation of nn.Upsample for details.".format(mode)
unique_id ds y
0 % WEIGHTED ILI 2020-07-07 -1.265398
1 % WEIGHTED ILI 2020-07-14 -3.120025
2 % WEIGHTED ILI 2020-07-21 -3.601343
3 % WEIGHTED ILI 2020-07-28 -5.753497
4 % WEIGHTED ILI 2020-08-04 -5.873628
... ... ... ...
163 OT 2020-11-17 -4.912064
164 OT 2020-11-24 -3.569168
165 OT 2020-12-01 -3.541500
166 OT 2020-12-08 -4.298891
167 OT 2020-12-15 -4.216655

168 rows × 3 columns

{% endraw %}

3.1 auto.NBEATS

{% raw %}
auto_nbeats = NBEATS(n_time_out=24)
auto_nbeats.space['max_steps'] = hp.choice('max_steps', [1]) # Override max_steps for faster example
{% endraw %} {% raw %}
auto_nbeats.fit(Y_df=Y_df, X_df=None, S_df=None, hyperopt_steps=2,
                n_ts_val=n_ts_val,
                n_ts_test=n_ts_test,
                results_dir='./results/autonbeats',
                save_trials=True,
                loss_function_val=nf.losses.numpy.mae,
                loss_functions_test={'mae':nf.losses.numpy.mae,
                                     'mse':nf.losses.numpy.mse},
                return_test_forecast=True,
                verbose=False)
INFO:hyperopt.tpe:build_posterior_wrapper took 0.012296 seconds
INFO:hyperopt.tpe:TPE using 0 trials
/Users/cchallu/opt/anaconda3/envs/neuralforecast/lib/python3.7/site-packages/pytorch_lightning/trainer/data_loading.py:133: UserWarning: The dataloader, val_dataloader 0, does not have many workers which may be a bottleneck. Consider increasing the value of the `num_workers` argument` (try 12 which is the number of cpus on this machine) in the `DataLoader` init to improve performance.
  f"The dataloader, {name}, does not have many workers which may be a bottleneck."
/Users/cchallu/opt/anaconda3/envs/neuralforecast/lib/python3.7/site-packages/pytorch_lightning/trainer/data_loading.py:133: UserWarning: The dataloader, train_dataloader, does not have many workers which may be a bottleneck. Consider increasing the value of the `num_workers` argument` (try 12 which is the number of cpus on this machine) in the `DataLoader` init to improve performance.
  f"The dataloader, {name}, does not have many workers which may be a bottleneck."
/Users/cchallu/opt/anaconda3/envs/neuralforecast/lib/python3.7/site-packages/pytorch_lightning/trainer/data_loading.py:133: UserWarning: The dataloader, predict_dataloader 0, does not have many workers which may be a bottleneck. Consider increasing the value of the `num_workers` argument` (try 12 which is the number of cpus on this machine) in the `DataLoader` init to improve performance.
  f"The dataloader, {name}, does not have many workers which may be a bottleneck."
INFO:hyperopt.tpe:build_posterior_wrapper took 0.011091 seconds
INFO:hyperopt.tpe:TPE using 1/1 trials with best loss 0.832682
<__main__.NBEATS at 0x7f89f0a90b90>
{% endraw %} {% raw %}
forecasts = auto_nbeats.forecast(Y_df=Y_df)
forecasts
INFO:root:Train Validation splits

INFO:root:                                      ds           
                                     min        max
unique_id         sample_mask                      
% WEIGHTED ILI    0           2002-01-01 2020-06-30
                  1           2020-07-07 2020-12-15
%UNWEIGHTED ILI   0           2002-01-01 2020-06-30
                  1           2020-07-07 2020-12-15
AGE 0-4           0           2002-01-01 2020-06-30
                  1           2020-07-07 2020-12-15
AGE 5-24          0           2002-01-01 2020-06-30
                  1           2020-07-07 2020-12-15
ILITOTAL          0           2002-01-01 2020-06-30
                  1           2020-07-07 2020-12-15
NUM. OF PROVIDERS 0           2002-01-01 2020-06-30
                  1           2020-07-07 2020-12-15
OT                0           2002-01-01 2020-06-30
                  1           2020-07-07 2020-12-15
INFO:root:
Total data 			6930 time stamps 
Available percentage=100.0, 	6930 time stamps 
Insample  percentage=2.42, 	168 time stamps 
Outsample percentage=97.58, 	6762 time stamps 

/Users/cchallu/opt/anaconda3/envs/neuralforecast/lib/python3.7/site-packages/pytorch_lightning/trainer/connectors/callback_connector.py:91: LightningDeprecationWarning: Setting `Trainer(progress_bar_refresh_rate=1)` is deprecated in v1.5 and will be removed in v1.7. Please pass `pytorch_lightning.callbacks.progress.TQDMProgressBar` with `refresh_rate` directly to the Trainer's `callbacks` argument instead. Or, to disable the progress bar pass `enable_progress_bar = False` to the Trainer.
  f"Setting `Trainer(progress_bar_refresh_rate={progress_bar_refresh_rate})` is deprecated in v1.5 and"
/Users/cchallu/opt/anaconda3/envs/neuralforecast/lib/python3.7/site-packages/pytorch_lightning/trainer/data_loading.py:133: UserWarning: The dataloader, predict_dataloader 0, does not have many workers which may be a bottleneck. Consider increasing the value of the `num_workers` argument` (try 12 which is the number of cpus on this machine) in the `DataLoader` init to improve performance.
  f"The dataloader, {name}, does not have many workers which may be a bottleneck."
Predicting: 100%|██████████| 7/7 [00:00<00:00, 545.29it/s]
unique_id ds y
0 % WEIGHTED ILI 2020-07-07 -0.568923
1 % WEIGHTED ILI 2020-07-14 -1.287022
2 % WEIGHTED ILI 2020-07-21 -0.459948
3 % WEIGHTED ILI 2020-07-28 -0.598790
4 % WEIGHTED ILI 2020-08-04 -0.275242
... ... ... ...
163 OT 2020-11-17 4.172915
164 OT 2020-11-24 3.797713
165 OT 2020-12-01 4.516331
166 OT 2020-12-08 4.072850
167 OT 2020-12-15 3.531220

168 rows × 3 columns

{% endraw %}

3.1 auto.RNN

{% raw %}
auto_rnn = RNN(n_time_out=24)
auto_rnn.space['max_steps'] = hp.choice('max_steps', [1]) # Override max_steps for faster example
{% endraw %} {% raw %}
auto_rnn.fit(Y_df=Y_df, X_df=None, S_df=None, hyperopt_steps=2,
             n_ts_val=n_ts_val,
             n_ts_test=n_ts_test,
             results_dir='./results/autornn',
             save_trials=True,
             loss_function_val=nf.losses.numpy.mae,
             loss_functions_test={'mae':nf.losses.numpy.mae,
                                  'mse':nf.losses.numpy.mse},
             return_test_forecast=True,
             verbose=False)
INFO:hyperopt.tpe:build_posterior_wrapper took 0.013226 seconds
INFO:hyperopt.tpe:TPE using 0 trials
/Users/cchallu/opt/anaconda3/envs/neuralforecast/lib/python3.7/site-packages/pytorch_lightning/trainer/data_loading.py:133: UserWarning: The dataloader, val_dataloader 0, does not have many workers which may be a bottleneck. Consider increasing the value of the `num_workers` argument` (try 12 which is the number of cpus on this machine) in the `DataLoader` init to improve performance.
  f"The dataloader, {name}, does not have many workers which may be a bottleneck."
/Users/cchallu/opt/anaconda3/envs/neuralforecast/lib/python3.7/site-packages/pytorch_lightning/trainer/data_loading.py:133: UserWarning: The dataloader, train_dataloader, does not have many workers which may be a bottleneck. Consider increasing the value of the `num_workers` argument` (try 12 which is the number of cpus on this machine) in the `DataLoader` init to improve performance.
  f"The dataloader, {name}, does not have many workers which may be a bottleneck."
/Users/cchallu/opt/anaconda3/envs/neuralforecast/lib/python3.7/site-packages/pytorch_lightning/trainer/data_loading.py:133: UserWarning: The dataloader, predict_dataloader 0, does not have many workers which may be a bottleneck. Consider increasing the value of the `num_workers` argument` (try 12 which is the number of cpus on this machine) in the `DataLoader` init to improve performance.
  f"The dataloader, {name}, does not have many workers which may be a bottleneck."
INFO:hyperopt.tpe:build_posterior_wrapper took 0.015764 seconds
INFO:hyperopt.tpe:TPE using 1/1 trials with best loss 0.877769
<__main__.RNN at 0x7f8a51b0bf50>
{% endraw %} {% raw %}
forecasts = auto_rnn.forecast(Y_df=Y_df)
forecasts
INFO:root:Train Validation splits

INFO:root:                                      ds           
                                     min        max
unique_id         sample_mask                      
% WEIGHTED ILI    0           2002-01-01 2020-06-30
                  1           2020-07-07 2020-12-15
%UNWEIGHTED ILI   0           2002-01-01 2020-06-30
                  1           2020-07-07 2020-12-15
AGE 0-4           0           2002-01-01 2020-06-30
                  1           2020-07-07 2020-12-15
AGE 5-24          0           2002-01-01 2020-06-30
                  1           2020-07-07 2020-12-15
ILITOTAL          0           2002-01-01 2020-06-30
                  1           2020-07-07 2020-12-15
NUM. OF PROVIDERS 0           2002-01-01 2020-06-30
                  1           2020-07-07 2020-12-15
OT                0           2002-01-01 2020-06-30
                  1           2020-07-07 2020-12-15
INFO:root:
Total data 			6930 time stamps 
Available percentage=100.0, 	6930 time stamps 
Insample  percentage=2.42, 	168 time stamps 
Outsample percentage=97.58, 	6762 time stamps 

/Users/cchallu/opt/anaconda3/envs/neuralforecast/lib/python3.7/site-packages/pytorch_lightning/trainer/connectors/callback_connector.py:91: LightningDeprecationWarning: Setting `Trainer(progress_bar_refresh_rate=1)` is deprecated in v1.5 and will be removed in v1.7. Please pass `pytorch_lightning.callbacks.progress.TQDMProgressBar` with `refresh_rate` directly to the Trainer's `callbacks` argument instead. Or, to disable the progress bar pass `enable_progress_bar = False` to the Trainer.
  f"Setting `Trainer(progress_bar_refresh_rate={progress_bar_refresh_rate})` is deprecated in v1.5 and"
/Users/cchallu/opt/anaconda3/envs/neuralforecast/lib/python3.7/site-packages/pytorch_lightning/trainer/data_loading.py:133: UserWarning: The dataloader, predict_dataloader 0, does not have many workers which may be a bottleneck. Consider increasing the value of the `num_workers` argument` (try 12 which is the number of cpus on this machine) in the `DataLoader` init to improve performance.
  f"The dataloader, {name}, does not have many workers which may be a bottleneck."
Predicting: 100%|██████████| 7/7 [00:00<00:00, 22.68it/s]
unique_id ds y
0 % WEIGHTED ILI 2020-07-07 -0.183406
1 % WEIGHTED ILI 2020-07-14 0.073594
2 % WEIGHTED ILI 2020-07-21 0.015090
3 % WEIGHTED ILI 2020-07-28 -0.280709
4 % WEIGHTED ILI 2020-08-04 -0.053751
... ... ... ...
163 OT 2020-11-17 0.325544
164 OT 2020-11-24 0.039716
165 OT 2020-12-01 0.341176
166 OT 2020-12-08 0.412436
167 OT 2020-12-15 0.496897

168 rows × 3 columns

{% endraw %}

4. AutoNF

Define Hyperparameter Space

A temporal train-evaluation split procedure allows us to estimate the model’s generalization performance on future data unseen by the model. We use the train set to optimize the model parameters, and the validation and test sets to evaluate the accuracy of the model’s predictions.

In this case we set the space to None, that implicitly uses the predefined model space, but the space can be specified as a dictionary following the conventions of the Hyperopt package.

{% raw %}
forecast_horizon = 24

nhits_space_dict = nhits_space(n_time_out=forecast_horizon)
nhits_space_dict['max_steps'] = hp.choice('max_steps', [10])
nhits_space_dict['max_epochs'] = hp.choice('max_epochs', [None])

nbeats_space_dict = nbeats_space(n_time_out=forecast_horizon)
nbeats_space_dict['max_steps'] = hp.choice('max_steps', [10])
nbeats_space_dict['max_epochs'] = hp.choice('max_epochs', [None])

rnn_space_dict = rnn_space(n_time_out=forecast_horizon)
rnn_space_dict['max_steps'] = hp.choice('max_steps', [10])
rnn_space_dict['max_epochs'] = hp.choice('max_epochs', [None])
{% endraw %} {% raw %}
config_dict = {'nbeats': dict(space=nhits_space_dict, hyperopt_steps=3), # Use space=None for default dict
               'nhits':  dict(space=nbeats_space_dict, hyperopt_steps=3), # Use space=None for default dict
               'rnn':    dict(space=rnn_space_dict, hyperopt_steps=3) # Use space=None for default dict
              }
{% endraw %}

Hyperparameter Tuning

A temporal train-validation-test (676,97,193) split procedure allows us to estimate the model’s generalization performance on future data unseen by the model. We use the train set to optimize the model parameters, and the validation and test sets to evaluate the accuracy of the model’s predictions.

{% raw %}
forecast_horizon = 24

model = AutoNF(config_dict=config_dict, n_time_out=forecast_horizon)

model.fit(Y_df=Y_df, X_df=None, S_df=None,
          loss_function_val=nf.losses.numpy.mae, 
          loss_functions_test={'mae':nf.losses.numpy.mae,
                               'mse':nf.losses.numpy.mse},
          n_ts_val=n_ts_val,
          n_ts_test=n_ts_test,
          results_dir='./results/auto',
          return_forecasts=True,
          verbose=False)
MODEL:  nbeats
INFO:hyperopt.tpe:build_posterior_wrapper took 0.014261 seconds
INFO:hyperopt.tpe:TPE using 0 trials
/Users/cchallu/opt/anaconda3/envs/neuralforecast/lib/python3.7/site-packages/pytorch_lightning/trainer/data_loading.py:133: UserWarning: The dataloader, val_dataloader 0, does not have many workers which may be a bottleneck. Consider increasing the value of the `num_workers` argument` (try 12 which is the number of cpus on this machine) in the `DataLoader` init to improve performance.
  f"The dataloader, {name}, does not have many workers which may be a bottleneck."
/Users/cchallu/opt/anaconda3/envs/neuralforecast/lib/python3.7/site-packages/torch/nn/functional.py:3635: UserWarning: Default upsampling behavior when mode=linear is changed to align_corners=False since 0.4.0. Please specify align_corners=True if the old behavior is desired. See the documentation of nn.Upsample for details.
  "See the documentation of nn.Upsample for details.".format(mode)
/Users/cchallu/opt/anaconda3/envs/neuralforecast/lib/python3.7/site-packages/pytorch_lightning/trainer/data_loading.py:133: UserWarning: The dataloader, train_dataloader, does not have many workers which may be a bottleneck. Consider increasing the value of the `num_workers` argument` (try 12 which is the number of cpus on this machine) in the `DataLoader` init to improve performance.
  f"The dataloader, {name}, does not have many workers which may be a bottleneck."
/Users/cchallu/opt/anaconda3/envs/neuralforecast/lib/python3.7/site-packages/pytorch_lightning/trainer/data_loading.py:133: UserWarning: The dataloader, predict_dataloader 0, does not have many workers which may be a bottleneck. Consider increasing the value of the `num_workers` argument` (try 12 which is the number of cpus on this machine) in the `DataLoader` init to improve performance.
  f"The dataloader, {name}, does not have many workers which may be a bottleneck."
INFO:hyperopt.tpe:build_posterior_wrapper took 0.019330 seconds
INFO:hyperopt.tpe:TPE using 1/1 trials with best loss 0.420319
INFO:hyperopt.tpe:build_posterior_wrapper took 0.016743 seconds
INFO:hyperopt.tpe:TPE using 2/2 trials with best loss 0.420319
INFO:hyperopt.tpe:build_posterior_wrapper took 0.009737 seconds
INFO:hyperopt.tpe:TPE using 0 trials
MODEL:  nhits
INFO:hyperopt.tpe:build_posterior_wrapper took 0.010499 seconds
INFO:hyperopt.tpe:TPE using 1/1 trials with best loss 0.426984
INFO:hyperopt.tpe:build_posterior_wrapper took 0.016317 seconds
INFO:hyperopt.tpe:TPE using 2/2 trials with best loss 0.426984
INFO:hyperopt.tpe:build_posterior_wrapper took 0.009824 seconds
INFO:hyperopt.tpe:TPE using 0 trials
MODEL:  rnn
INFO:hyperopt.tpe:build_posterior_wrapper took 0.011776 seconds
INFO:hyperopt.tpe:TPE using 1/1 trials with best loss 0.871917
INFO:hyperopt.tpe:build_posterior_wrapper took 0.012385 seconds
INFO:hyperopt.tpe:TPE using 2/2 trials with best loss 0.871917
{% endraw %}

Val losses

{% raw %}
print('MAE')
print('NHITS:\t', model.results_dict['nhits']['best_val_loss'])
print('NBEATS:\t', model.results_dict['nbeats']['best_val_loss'])
print('RNN:\t', model.results_dict['rnn']['best_val_loss'])
MAE
NHITS:	 0.42698436975479126
NBEATS:	 0.42031946778297424
RNN:	 0.750873327255249
{% endraw %}

Val losses trajectory

{% raw %}
time   = model.results_dict['nbeats']['optimization_times']
losses = model.results_dict['nbeats']['optimization_losses']
plt.plot(time, losses)
plt.xlabel('segs')
plt.ylabel('val loss')
plt.grid()
plt.show()
{% endraw %}

5. Evaluate Results

Here we wrangle the numpy predictions to evaluate and plot the predictions.

{% raw %}
assert((model.results_dict['nbeats']['y_true'] == model.results_dict['rnn']['y_true']).mean() == 1)
{% endraw %} {% raw %}
y_hat_nhits  = model.results_dict['nhits']['y_hat']
y_hat_nbeats = model.results_dict['nbeats']['y_hat']
y_hat_rnn    = model.results_dict['rnn']['y_hat']
y_true       = model.results_dict['nbeats']['y_true']

print('\n Shapes')
print('1. y_hat_nhits.shape (N,T,H) \t', y_hat_nhits.shape)
print('1. y_hat_nbeats.shape (N,T,H)\t', y_hat_nbeats.shape)
print('1. y_hat_rnn.shape (N,T,H)\t', y_hat_rnn.shape)
print('1. y_true.shape (N,T,H)\t\t', y_true.shape)
 Shapes
1. y_hat_nhits.shape (N,T,H) 	 (7, 170, 24)
1. y_hat_nbeats.shape (N,T,H)	 (7, 170, 24)
1. y_hat_rnn.shape (N,T,H)	 (7, 170, 24)
1. y_true.shape (N,T,H)		 (7, 170, 24)
{% endraw %} {% raw %}
w_idx = 0
u_idx = 0

plt.plot(y_true[u_idx,w_idx,:], label='True Signal')
plt.plot(y_hat_nbeats[u_idx,w_idx,:], label='N-BEATS')
plt.plot(y_hat_nhits[u_idx,w_idx,:], label='N-HiTS')
plt.plot(y_hat_rnn[u_idx,w_idx,:], label='RNN')

plt.legend()
plt.xlabel('Time')
plt.ylabel('Weighted ILI [ratio]')
plt.grid()
plt.show()
{% endraw %} {% raw %}
model.best_model
<__main__.NBEATS at 0x7f8a628e8250>
{% endraw %}