---

title: Exponential Smoothing - Recurrent Neural Network


keywords: fastai
sidebar: home_sidebar

summary: "ESRNN pytorch implementation."
description: "ESRNN pytorch implementation."
nb_path: "nbs/models_esrnn__esrnn_model.ipynb"
---
<!--

#################################################
### THIS FILE WAS AUTOGENERATED! DO NOT EDIT! ###
#################################################
# file to edit: nbs/models_esrnn__esrnn_model.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>
    {% 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>
    {% 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>
    {% endraw %}

<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="Tests">Tests<a class="anchor-link" href="#Tests"> </a></h2>
</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="k">def</span> <span class="nf">test_no_leakage_season</span><span class="p">(</span><span class="n">n_series</span><span class="p">,</span> <span class="n">input_size</span><span class="p">,</span> <span class="n">output_size</span><span class="p">,</span> <span class="n">seasonality</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    This test checks no leakage for seasonality.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">t</span><span class="o">.</span><span class="n">manual_seed</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
    <span class="n">model</span> <span class="o">=</span> <span class="n">_ESRNN</span><span class="p">(</span><span class="n">n_series</span><span class="o">=</span><span class="n">n_series</span><span class="p">,</span> <span class="n">input_size</span><span class="o">=</span><span class="n">input_size</span><span class="p">,</span> <span class="n">output_size</span><span class="o">=</span><span class="n">output_size</span><span class="p">,</span> 
                   <span class="n">output_size_m</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">n_t</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">n_s</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> 
                   <span class="n">es_component</span><span class="o">=</span><span class="s1">&#39;multiplicative&#39;</span><span class="p">,</span> <span class="n">seasonality</span><span class="o">=</span><span class="n">seasonality</span><span class="p">,</span> <span class="n">noise_std</span><span class="o">=</span><span class="mf">0.000000001</span><span class="p">,</span>
                   <span class="n">cell_type</span><span class="o">=</span><span class="s1">&#39;GRU&#39;</span><span class="p">,</span>
                   <span class="n">add_nl_layer</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">dilations</span><span class="o">=</span><span class="p">[[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">]],</span> <span class="n">state_hsize</span><span class="o">=</span><span class="mi">30</span><span class="p">)</span>
    <span class="n">S</span> <span class="o">=</span> <span class="n">t</span><span class="o">.</span><span class="n">empty</span><span class="p">((</span><span class="n">n_series</span><span class="p">,</span> <span class="mi">0</span><span class="p">))</span>
    <span class="n">X</span> <span class="o">=</span> <span class="n">t</span><span class="o">.</span><span class="n">empty</span><span class="p">((</span><span class="n">n_series</span><span class="p">,</span> <span class="mi">0</span><span class="p">))</span>
    <span class="n">Y</span> <span class="o">=</span> <span class="n">t</span><span class="o">.</span><span class="n">normal</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="p">(</span><span class="n">n_series</span><span class="p">,</span> <span class="mi">2</span> <span class="o">*</span> <span class="p">(</span><span class="n">input_size</span> <span class="o">+</span> <span class="n">output_size</span><span class="p">)))</span>
    <span class="n">Y</span> <span class="o">+=</span> <span class="n">Y</span><span class="o">.</span><span class="n">min</span><span class="p">()</span><span class="o">.</span><span class="n">abs</span><span class="p">()</span> <span class="o">+</span> <span class="mi">10</span> 
    <span class="n">idxs</span> <span class="o">=</span> <span class="n">t</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="n">n_series</span><span class="p">)</span>
    
    <span class="c1"># Testing different values for output_size</span>
    <span class="c1"># forecasts should be the same in all cases</span>
    <span class="c1"># except for numeric exceptions</span>
    <span class="n">Y_to_test</span> <span class="o">=</span> <span class="p">[</span><span class="n">Y</span><span class="p">]</span> <span class="o">*</span> <span class="mi">4</span>
    <span class="n">Y_to_test</span><span class="p">[</span><span class="mi">1</span><span class="p">][:,</span> <span class="o">-</span><span class="n">output_size</span><span class="p">:]</span> <span class="o">=</span> <span class="mi">10_000</span>
    <span class="n">Y_to_test</span><span class="p">[</span><span class="mi">2</span><span class="p">][:,</span> <span class="o">-</span><span class="n">output_size</span><span class="p">:]</span> <span class="o">=</span> <span class="mi">0</span>
    <span class="n">Y_to_test</span><span class="p">[</span><span class="mi">3</span><span class="p">][:,</span> <span class="o">-</span><span class="n">output_size</span><span class="p">:]</span> <span class="o">=</span> <span class="o">-</span><span class="mi">10_000</span>
    
    <span class="n">forecasts</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">for</span> <span class="n">Y</span> <span class="ow">in</span> <span class="n">Y_to_test</span><span class="p">:</span>
        <span class="c1"># forward es and rnn</span>
        <span class="n">windows_y_insample</span><span class="p">,</span> <span class="n">windows_y_outsample</span><span class="p">,</span> <span class="n">levels</span><span class="p">,</span> <span class="n">seasonalities</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">es</span><span class="p">(</span><span class="n">S</span><span class="p">,</span> <span class="n">Y</span><span class="p">,</span> <span class="n">X</span><span class="p">,</span> <span class="n">idxs</span><span class="p">,</span> <span class="n">step_size</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
        <span class="n">trends</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">rnn</span><span class="p">(</span><span class="n">windows_y_insample</span><span class="p">)</span>
        <span class="n">trends</span> <span class="o">=</span> <span class="n">trends</span><span class="o">.</span><span class="n">permute</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>

        <span class="c1"># predict es</span>
        <span class="n">y_hat</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">es</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">trends</span><span class="o">=</span><span class="n">trends</span><span class="p">,</span> <span class="n">levels</span><span class="o">=</span><span class="n">levels</span><span class="p">,</span> <span class="n">seasonalities</span><span class="o">=</span><span class="n">seasonalities</span><span class="p">,</span> <span class="n">step_size</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>

        <span class="n">forecasts</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">y_hat</span><span class="p">)</span>
        
    <span class="k">assert</span> <span class="nb">all</span><span class="p">(</span><span class="n">t</span><span class="o">.</span><span class="n">allclose</span><span class="p">(</span><span class="n">forecasts</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">forecast</span><span class="p">)</span> <span class="k">for</span> <span class="n">forecast</span> <span class="ow">in</span> <span class="n">forecasts</span><span class="p">),</span> <span class="p">(</span>
        <span class="s1">&#39;Season leakage detected &#39;</span><span class="p">,</span>
        <span class="s1">&#39;please check.&#39;</span>
    <span class="p">)</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="n">test_no_leakage_season</span><span class="p">(</span><span class="n">n_series</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">input_size</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">output_size</span><span class="o">=</span><span class="mi">8</span><span class="p">,</span> <span class="n">seasonality</span><span class="o">=</span><span class="p">[</span><span class="mi">6</span><span class="p">])</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="n">test_no_leakage_season</span><span class="p">(</span><span class="n">n_series</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">input_size</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">output_size</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">seasonality</span><span class="o">=</span><span class="p">[</span><span class="mi">6</span><span class="p">])</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="n">test_no_leakage_season</span><span class="p">(</span><span class="n">n_series</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">input_size</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">output_size</span><span class="o">=</span><span class="mi">8</span><span class="p">,</span> <span class="n">seasonality</span><span class="o">=</span><span class="p">[</span><span class="mi">12</span><span class="p">,</span> <span class="mi">24</span><span class="p">])</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="n">test_no_leakage_season</span><span class="p">(</span><span class="n">n_series</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">input_size</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">output_size</span><span class="o">=</span><span class="mi">8</span><span class="p">,</span> <span class="n">seasonality</span><span class="o">=</span><span class="p">[</span><span class="mi">6</span><span class="p">,</span> <span class="mi">12</span><span class="p">])</span>
</pre></div>

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

</div>
    {% endraw %}

</div>
 

