---

title: Losses utils 


keywords: fastai
sidebar: home_sidebar

summary: "Generic loss class."
description: "Generic loss class."
nb_path: "nbs/losses__utils.ipynb"
---
<!--

#################################################
### THIS FILE WAS AUTOGENERATED! DO NOT EDIT! ###
#################################################
# file to edit: nbs/losses__utils.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 class="output_wrapper">
<div class="output">

<div class="output_area">


<div class="output_markdown rendered_html output_subarea ">
<h2 id="LossFunction" class="doc_header"><code>class</code> <code>LossFunction</code><a href="https://github.com/Nixtla/nixtlats/tree/master/nixtlats/losses/utils.py#L18" class="source_link" style="float:right">[source]</a></h2><blockquote><p><code>LossFunction</code>(<strong><code>loss_name</code></strong>:<code>str</code>, <strong><code>seasonality</code></strong>:<code>Optional</code>[<code>int</code>]=<em><code>None</code></em>, <strong><code>percentile</code></strong>:<code>Union</code>[<code>List</code>[<code>int</code>], <code>int</code>, <code>NoneType</code>]=<em><code>None</code></em>, <strong><code>level_variability_penalty</code></strong>:<code>Optional</code>[<code>int</code>]=<em><code>None</code></em>)</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="LossFunction.__call__" class="doc_header"><code>LossFunction.__call__</code><a href="https://github.com/Nixtla/nixtlats/tree/master/nixtlats/losses/utils.py#L61" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>LossFunction.__call__</code>(<strong><code>y</code></strong>:<code>Tensor</code>, <strong><code>y_hat</code></strong>:<code>Tensor</code>, <strong><code>mask</code></strong>:<code>Optional</code>[<code>Tensor</code>]=<em><code>None</code></em>, <strong><code>y_insample</code></strong>:<code>Optional</code>[<code>Tensor</code>]=<em><code>None</code></em>, <strong><code>levels</code></strong>:<code>Optional</code>[<code>Tensor</code>]=<em><code>None</code></em>)</p>
</blockquote>
<p>Returns loss according to <code>loss_name</code>.</p>

</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">
<h2 id="Usage">Usage<a class="anchor-link" href="#Usage"> </a></h2>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="Multi-Quantile-losses">Multi-Quantile losses<a class="anchor-link" href="#Multi-Quantile-losses"> </a></h3>
</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="n">loss</span> <span class="o">=</span> <span class="n">LossFunction</span><span class="p">(</span><span class="s1">&#39;MQ&#39;</span><span class="p">,</span> <span class="n">percentile</span><span class="o">=</span><span class="p">[</span><span class="mi">30</span><span class="p">,</span> <span class="mi">60</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="n">size</span><span class="o">=</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="mi">10</span><span class="p">))</span>
<span class="n">y_hat</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="n">size</span><span class="o">=</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">2</span><span class="p">))</span>

<span class="n">loss</span><span class="p">(</span><span class="n">y</span><span class="p">,</span> <span class="n">y_hat</span><span class="p">)</span>
</pre></div>

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

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

<div class="output_area">



<div class="output_text output_subarea output_execute_result">
<pre>tensor(0.5067)</pre>
</div>

</div>

</div>
</div>

</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="Mean-losses">Mean losses<a class="anchor-link" href="#Mean-losses"> </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="n">loss</span> <span class="o">=</span> <span class="n">LossFunction</span><span class="p">(</span><span class="s1">&#39;MAE&#39;</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="n">size</span><span class="o">=</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="mi">10</span><span class="p">))</span>
<span class="n">y_hat</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="n">size</span><span class="o">=</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="mi">10</span><span class="p">))</span>

<span class="n">loss</span><span class="p">(</span><span class="n">y</span><span class="p">,</span> <span class="n">y_hat</span><span class="p">)</span>
</pre></div>

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

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

<div class="output_area">



<div class="output_text output_subarea output_execute_result">
<pre>tensor(1.1782)</pre>
</div>

</div>

</div>
</div>

</div>
    {% endraw %}

</div>
 

