--- title: Losses keywords: fastai sidebar: home_sidebar summary: "This contains losses not available in fastai or Pytorch." description: "This contains losses not available in fastai or Pytorch." nb_path: "nbs/050_losses.ipynb" ---
{% raw %}
{% endraw %} {% raw %}
{% endraw %} {% raw %}

class HuberLoss[source]

HuberLoss(reduction='mean', delta=1.0) :: Module

Huber loss

Creates a criterion that uses a squared term if the absolute element-wise error falls below delta and a delta-scaled L1 term otherwise. This loss combines advantages of both :class:L1Loss and :class:MSELoss; the delta-scaled L1 region makes the loss less sensitive to outliers than :class:MSELoss, while the L2 region provides smoothness over :class:L1Loss near 0. See Huber loss <https://en.wikipedia.org/wiki/Huber_loss>_ for more information. This loss is equivalent to nn.SmoothL1Loss when delta == 1.

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

class LogCoshLoss[source]

LogCoshLoss(reduction='mean', delta=1.0) :: Module

Base class for all neural network modules.

Your models should also subclass this class.

Modules can also contain other Modules, allowing to nest them in a tree structure. You can assign the submodules as regular attributes::

import torch.nn as nn
import torch.nn.functional as F

class Model(nn.Module):
    def __init__(self):
        super(Model, self).__init__()
        self.conv1 = nn.Conv2d(1, 20, 5)
        self.conv2 = nn.Conv2d(20, 20, 5)

    def forward(self, x):
        x = F.relu(self.conv1(x))
        return F.relu(self.conv2(x))

Submodules assigned in this way will be registered, and will have their parameters converted too when you call :meth:to, etc.

:ivar training: Boolean represents whether this module is in training or evaluation mode. :vartype training: bool

{% endraw %} {% raw %}
{% endraw %} {% raw %}
inp = torch.rand(8, 3, 10)
targ = torch.randn(8, 3, 10)
test_close(HuberLoss(delta=1)(inp, targ), nn.SmoothL1Loss()(inp, targ))
LogCoshLoss()(inp, targ)
tensor(0.5384)
{% endraw %} {% raw %}

class MaskedLossWrapper[source]

MaskedLossWrapper(crit) :: Module

Same as nn.Module, but no need for subclasses to call super().__init__

{% endraw %} {% raw %}
{% endraw %} {% raw %}
inp = torch.rand(8, 3, 10)
targ = torch.randn(8, 3, 10)
targ[targ >.8] = np.nan
nn.L1Loss()(inp, targ), MaskedLossWrapper(nn.L1Loss())(inp, targ)
torch.Size([8, 30])
(tensor(nan), tensor(0.8645))
{% endraw %}