--- title: NBEATS Ensemble for M4 dataset keywords: fastai sidebar: home_sidebar nb_path: "nbs/models_nbeats__nbeats_model_ensemble.ipynb" ---
{% raw %}
{% endraw %} {% raw %}

    # from google.colab import drive
    # drive.mount('/content/drive')
    # os.chdir('./drive/MyDrive/nixtlats')
    # print(os.getcwd())

    Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount("/content/drive", force_remount=True).
    /content/drive/MyDrive/nixtlats

{% endraw %} {% raw %}

    # !pip install torchinfo
    # !pip install fastcore
    # !pip install s3fs
    # !pip install patool
    # !pip install --upgrade pandas==1.2.4
    # !pip install --upgrade requests==2.25.1

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

## Model hyper-parameters

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

## `class` `Yearly`[source]

> `Yearly`()

Yearly()

{% endraw %} {% raw %}

## `class` `Quarterly`[source]

> `Quarterly`()

Quarterly()

{% endraw %} {% raw %}

## `class` `Monthly`[source]

> `Monthly`()

Monthly()

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

#### `print_models_list`[source]

> `print_models_list`(`frequencies`:`list`, `table_width`:`int`)

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

## Loaders

{% raw %}

#### `create_loaders_M4`[source]

> `create_loaders_M4`(`Y_df`, `S_df`, `hparams`, `num_workers`)

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

## N-BEATS Model instantiation

{% raw %}

#### `NBEATS_instantiate`[source]

> `NBEATS_instantiate`(`hparams`)

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

## Tensorboard load

{% raw %}

    def show_tensorboard(logs_path, model_path):
        logs_model_path = f'{logs_path}/{model_path}'
        %load_ext tensorboard
        %tensorboard --logdir $logs_model_path

{% endraw %}

## N-BEATS Ensemble

{% raw %}

## `class` `EnsembleNBEATSM4`[source]

> `EnsembleNBEATSM4`()

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

## Ensemble experiments

{% raw %}

    LOGS_PATH = Path('lightning_logs')
    val_freq_steps = 1
    tensorboard_logs = True
    NUM_WORKERS = 4

    frequencies = [Yearly]
    print_models_list(frequencies=frequencies, table_width=75)

    Yearly grid (# of different model configurations = 12):
    ===========================================================================
    activation                                                           [ReLU]
    n_x                                                                     [0]
    n_s                                                                     [0]
    n_x_hidden                                                              [0]
    n_s_hidden                                                              [0]
    stack_types                                          [[trend, seasonality]]
    n_blocks                                                           [[3, 3]]
    n_layers                                                           [[4, 4]]
    shared_weights                                                       [True]
    n_harmonics                                                             [1]
    n_polynomials                                                           [2]
    n_theta_hidden            [[[256, 256, 256, 256], [2048, 2048, 2048, 204...
    initialization                                               [lecun_normal]
    learning_rate                                                       [0.001]
    lr_decay                                                                [0]
    lr_decay_step_size                                                   [1000]
    loss_val                                                            [SMAPE]
    dropout_prob_theta                                                      [0]
    weight_decay                                                            [0]
    batch_size                                                           [1024]
    batch_normalization                                                 [False]
    complete_inputs                                                     [False]
    mode                                                               [simple]
    n_time_in                                                          [36, 42]
    n_time_out                                                              [6]
    idx_to_sample_freq                                                      [1]
    val_idx_to_sample_freq                                                  [1]
    frequency                                                               [Y]
    seasonality                                                             [1]
    l_h                                                                   [1.5]
    loss_train                                              [MAPE, SMAPE, MASE]
    n_steps                                                               [250]
    random_seed                                                          [0, 1]
    dtype: object
    ===========================================================================

{% endraw %} {% raw %}

    ensemble = EnsembleNBEATSM4()

    forecasts = ensemble.fit(frequencies=frequencies,
                             loader=TimeSeriesLoader,
                             val_freq_steps=val_freq_steps,
                             tensorboard_logs=tensorboard_logs,
                             logs_path=LOGS_PATH,
                             num_workers=NUM_WORKERS)

    Output hidden; open in https://colab.research.google.com to view.

{% endraw %}

## Model evaluation

{% raw %}

    M4Evaluation.evaluate('data', 'Yearly', forecasts['Yearly'].drop('unique_id', axis=1).to_numpy())

| SMAPE | MASE | OWA
--- | --- | --- | ---
Yearly | 13.543033 | 3.077037 | 0.801469

{% endraw %}