---

title: M4 Ensemble


keywords: fastai
sidebar: home_sidebar



nb_path: "nbs/models_nbeats__ensemble.ipynb"
---
<!--

#################################################
### THIS FILE WAS AUTOGENERATED! DO NOT EDIT! ###
#################################################
# file to edit: nbs/models_nbeats__ensemble.ipynb
# command to build the docs after a change: nbdev_build_docs

-->

<div class="container" id="notebook-container">
        
    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">

</div>
    {% endraw %}

    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">
<div class="input">

<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="c1"># from google.colab import drive</span>
<span class="c1"># drive.mount(&#39;/content/drive&#39;)</span>
<span class="c1"># os.chdir(&#39;./drive/MyDrive/nixtlats&#39;)</span>
<span class="c1"># print(os.getcwd())</span>
</pre></div>

    </div>
</div>
</div>

</div>
    {% endraw %}

    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">
<div class="input">

<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="c1"># !pip install torchinfo</span>
<span class="c1"># !pip install fastcore</span>
<span class="c1"># !pip install s3fs</span>
<span class="c1"># !pip install patool</span>
<span class="c1"># !pip install --upgrade pandas==1.2.4</span>
<span class="c1"># !pip install --upgrade requests==2.25.1</span>
</pre></div>

    </div>
</div>
</div>

</div>
    {% endraw %}

    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">

</div>
    {% endraw %}

    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">

</div>
    {% endraw %}

    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">

<div class="output_wrapper">
<div class="output">

<div class="output_area">


<div class="output_markdown rendered_html output_subarea ">
<h2 id="Yearly" class="doc_header"><code>class</code> <code>Yearly</code><a href="https://github.com/Nixtla/nixtlats/tree/master/nixtlats/models/nbeats/ensemble.py#L76" class="source_link" style="float:right">[source]</a></h2><blockquote><p><code>Yearly</code>()</p>
</blockquote>
<p>Yearly()</p>

</div>

</div>

</div>
</div>

</div>
    {% endraw %}

    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">

<div class="output_wrapper">
<div class="output">

<div class="output_area">


<div class="output_markdown rendered_html output_subarea ">
<h2 id="Quarterly" class="doc_header"><code>class</code> <code>Quarterly</code><a href="https://github.com/Nixtla/nixtlats/tree/master/nixtlats/models/nbeats/ensemble.py#L92" class="source_link" style="float:right">[source]</a></h2><blockquote><p><code>Quarterly</code>()</p>
</blockquote>
<p>Quarterly()</p>

</div>

</div>

</div>
</div>

</div>
    {% endraw %}

    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">

<div class="output_wrapper">
<div class="output">

<div class="output_area">


<div class="output_markdown rendered_html output_subarea ">
<h2 id="Monthly" class="doc_header"><code>class</code> <code>Monthly</code><a href="https://github.com/Nixtla/nixtlats/tree/master/nixtlats/models/nbeats/ensemble.py#L108" class="source_link" style="float:right">[source]</a></h2><blockquote><p><code>Monthly</code>()</p>
</blockquote>
<p>Monthly()</p>

</div>

</div>

</div>
</div>

</div>
    {% endraw %}

    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">

</div>
    {% endraw %}

    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">

<div class="output_wrapper">
<div class="output">

<div class="output_area">


<div class="output_markdown rendered_html output_subarea ">
<h4 id="print_models_list" class="doc_header"><code>print_models_list</code><a href="https://github.com/Nixtla/nixtlats/tree/master/nixtlats/models/nbeats/ensemble.py#L124" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>print_models_list</code>(<strong><code>frequencies</code></strong>:<code>list</code>, <strong><code>table_width</code></strong>:<code>int</code>)</p>
</blockquote>

</div>

</div>

</div>
</div>

</div>
    {% endraw %}

    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">

</div>
    {% endraw %}

<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h1 id="Loaders">Loaders<a class="anchor-link" href="#Loaders"> </a></h1>
</div>
</div>
</div>
    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">

<div class="output_wrapper">
<div class="output">

<div class="output_area">


<div class="output_markdown rendered_html output_subarea ">
<h4 id="create_loaders_M4" class="doc_header"><code>create_loaders_M4</code><a href="https://github.com/Nixtla/nixtlats/tree/master/nixtlats/models/nbeats/ensemble.py#L133" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>create_loaders_M4</code>(<strong><code>Y_df</code></strong>, <strong><code>S_df</code></strong>, <strong><code>hparams</code></strong>, <strong><code>num_workers</code></strong>)</p>
</blockquote>

</div>

</div>

</div>
</div>

</div>
    {% endraw %}

    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">

</div>
    {% endraw %}

<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h1 id="NBEATS-Ensemble">NBEATS Ensemble<a class="anchor-link" href="#NBEATS-Ensemble"> </a></h1>
</div>
</div>
</div>
    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">

<div class="output_wrapper">
<div class="output">

<div class="output_area">


<div class="output_markdown rendered_html output_subarea ">
<h4 id="NBEATS_instantiate" class="doc_header"><code>NBEATS_instantiate</code><a href="https://github.com/Nixtla/nixtlats/tree/master/nixtlats/models/nbeats/ensemble.py#L175" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>NBEATS_instantiate</code>(<strong><code>hparams</code></strong>)</p>
</blockquote>

</div>

</div>

</div>
</div>

</div>
    {% endraw %}

    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">

</div>
    {% endraw %}

    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">

<div class="output_wrapper">
<div class="output">

<div class="output_area">


<div class="output_markdown rendered_html output_subarea ">
<h4 id="show_tensorboard" class="doc_header"><code>show_tensorboard</code><a href="https://github.com/Nixtla/nixtlats/tree/master/nixtlats/models/nbeats/ensemble.py#L208" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>show_tensorboard</code>(<strong><code>logs_path</code></strong>, <strong><code>model_path</code></strong>)</p>
</blockquote>

</div>

</div>

</div>
</div>

</div>
    {% endraw %}

    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">

</div>
    {% endraw %}

    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">

<div class="output_wrapper">
<div class="output">

<div class="output_area">


<div class="output_markdown rendered_html output_subarea ">
<h2 id="NBEATSEnsemble" class="doc_header"><code>class</code> <code>NBEATSEnsemble</code><a href="https://github.com/Nixtla/nixtlats/tree/master/nixtlats/models/nbeats/ensemble.py#L216" class="source_link" style="float:right">[source]</a></h2><blockquote><p><code>NBEATSEnsemble</code>(<strong><code>use_gpus</code></strong>:<code>bool</code>=<em><code>False</code></em>, <strong><code>gpus</code></strong>:<code>int</code>=<em><code>None</code></em>, <strong><code>auto_select_gpus</code></strong>:<code>bool</code>=<em><code>False</code></em>)</p>
</blockquote>

</div>

</div>

</div>
</div>

</div>
    {% endraw %}

    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">

</div>
    {% endraw %}

<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h1 id="Ensemble-Experiments">Ensemble Experiments<a class="anchor-link" href="#Ensemble-Experiments"> </a></h1>
</div>
</div>
</div>
    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">
<div class="input">

<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="c1"># val_freq_steps = 2</span>
<span class="c1"># tensorboard_logs = True</span>
<span class="c1"># NUM_WORKERS = 4</span>

<span class="c1"># frequencies = [Yearly]</span>
<span class="c1"># print_models_list(frequencies=frequencies, table_width=75)</span>
</pre></div>

    </div>
</div>
</div>

<div class="output_wrapper">
<div class="output">

<div class="output_area">

<div class="output_subarea output_stream output_stdout output_text">
<pre>
Yearly grid (# of different model configurations = 18):
===========================================================================
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                                       [12, 18, 24, 30, 36, 42]
n_time_out                                                           [6]
train_sample_freq                                                    [1]
frequency                                                            [Y]
seasonality                                                          [1]
l_h                                                                [1.5]
loss_train                                           [MAPE, SMAPE, MASE]
n_steps                                                            [260]
random_seed                                                          [0]
dtype: object
===========================================================================

</pre>
</div>
</div>

</div>
</div>

</div>
    {% endraw %}

    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">
<div class="input">

<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="c1">#                           gpus=-1 if torch.cuda.is_available() else None, </span>
<span class="c1">#                           auto_select_gpus=True if torch.cuda.is_available() else False)</span>

<span class="c1"># forecasts = ensemble.fit(frequencies=frequencies,</span>
<span class="c1">#                          loader=TimeSeriesLoader,  </span>
<span class="c1">#                          val_freq_steps=val_freq_steps,</span>
<span class="c1">#                          tensorboard_logs=tensorboard_logs,</span>
<span class="c1">#                          logs_path=LOGS_PATH,</span>
<span class="c1">#                          num_workers=NUM_WORKERS)</span>
</pre></div>

    </div>
</div>
</div>

<div class="output_wrapper">
<div class="output">

<div class="output_area">



<div class="output_text output_subarea ">
<pre>Output hidden; open in https://colab.research.google.com to view.</pre>
</div>

</div>

</div>
</div>

</div>
    {% endraw %}

    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">
<div class="input">

<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span> 
</pre></div>

    </div>
</div>
</div>

<div class="output_wrapper">
<div class="output">

<div class="output_area">


<div class="output_html rendered_html output_subarea output_execute_result">
<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>SMAPE</th>
      <th>MASE</th>
      <th>OWA</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>Yearly</th>
      <td>17.448574</td>
      <td>5.438234</td>
      <td>1.218016</td>
    </tr>
  </tbody>
</table>
</div>
</div>

</div>

</div>
</div>

</div>
    {% endraw %}

</div>
 

