The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

AI::MXNet::Gluon::Loss - Base class for loss.

DESCRIPTION

Base class for loss.

Parameters
----------
weight : float or None
    Global scalar weight for loss.
batch_axis : int, default 0
    The axis that represents mini-batch.

_apply_weighting

Apply weighting to loss.

Parameters
----------
loss : Symbol
    The loss to be weighted.
weight : float or None
    Global scalar weight for loss.
sample_weight : Symbol or None
    Per sample weighting. Must be broadcastable to
    the same shape as loss. For example, if loss has
    shape (64, 10) and you want to weight each sample
    in the batch separately, `sample_weight` should have
    shape (64, 1).

Returns
-------
loss : Symbol
    Weighted loss

NAME

AI::MXNet::Gluon::L2Loss

DESCRIPTION

Calculates the mean squared error between output and label:

Output and label can have arbitrary shape as long as they have the same
number of elements.

Parameters
----------
weight : float or None
    Global scalar weight for loss.
sample_weight : Symbol or None
    Per sample weighting. Must be broadcastable to
    the same shape as loss. For example, if loss has
    shape (64, 10) and you want to weight each sample
    in the batch, `sample_weight` should have shape (64, 1).
batch_axis : int, default 0
    The axis that represents mini-batch.

NAME

AI::MXNet::Gluon::L1Loss

DESCRIPTION

Calculates the mean absolute error between output and label:

.. math::
    L = \\frac{1}{2}\\sum_i \\vert {output}_i - {label}_i \\vert.

Output and label must have the same shape.

Parameters
----------
weight : float or None
    Global scalar weight for loss.
sample_weight : Symbol or None
    Per sample weighting. Must be broadcastable to
    the same shape as loss. For example, if loss has
    shape (64, 10) and you want to weight each sample
    in the batch, `sample_weight` should have shape (64, 1).
batch_axis : int, default 0
    The axis that represents mini-batch.

NAME

AI::MXNet::Gluon::SigmoidBinaryCrossEntropyLoss

DESCRIPTION

The cross-entropy loss for binary classification. (alias: SigmoidBCELoss)

BCE loss is useful when training logistic regression.

.. math::
    loss(o, t) = - 1/n \sum_i (t[i] * log(o[i]) + (1 - t[i]) * log(1 - o[i]))


Parameters
----------
from_sigmoid : bool, default is `False`
    Whether the input is from the output of sigmoid. Set this to false will make
    the loss calculate sigmoid and then BCE, which is more numerically stable through
    log-sum-exp trick.
weight : float or None
    Global scalar weight for loss.
sample_weight : Symbol or None
    Per sample weighting. Must be broadcastable to
    the same shape as loss. For example, if loss has
    shape (64, 10) and you want to weight each sample
    in the batch, `sample_weight` should have shape (64, 1).
batch_axis : int, default 0
    The axis that represents mini-batch.

NAME

AI::MXNet::Gluon::SoftmaxCrossEntropyLoss

DESCRIPTION

Computes the softmax cross entropy loss. (alias: SoftmaxCELoss)

If `sparse_label` is `True`, label should contain integer category indicators:

.. math::
    p = {softmax}({output})

    L = -\\sum_i {log}(p_{i,{label}_i})

Label's shape should be output's shape without the `axis` dimension. i.e. for
`output.shape` = (1,2,3,4) and axis = 2, `label.shape` should be (1,2,4).

If `sparse_label` is `False`, label should contain probability distribution
with the same shape as output:

.. math::
    p = {softmax}({output})

    L = -\\sum_i \\sum_j {label}_j {log}(p_{ij})

Parameters
----------
axis : int, default -1
    The axis to sum over when computing softmax and entropy.
sparse_label : bool, default True
    Whether label is an integer array instead of probability distribution.
from_logits : bool, default False
    Whether input is a log probability (usually from log_softmax) instead
    of unnormalized numbers.
weight : float or None
    Global scalar weight for loss.
sample_weight : Symbol or None
    Per sample weighting. Must be broadcastable to
    the same shape as loss. For example, if loss has
    shape (64, 10) and you want to weight each sample
    in the batch, `sample_weight` should have shape (64, 1).
batch_axis : int, default 0
    The axis that represents mini-batch.

NAME

AI::MXNet::Gluon::KLDivLoss

DESCRIPTION

The Kullback-Leibler divergence loss.

KL divergence is a useful distance measure for continuous distributions
and is often useful when performing direct regression over the space of
(discretely sampled) continuous output distributions.

.. _Kullback-Leibler divergence:
    https://en.wikipedia.org/wiki/Kullback-Leibler_divergence
.. math::
    L = 1/n \\sum_i (label_i * (log(label_i) - output_i))

Label's shape should be the same as output's.

Parameters
----------
from_logits : bool, default is `True`
    Whether the input is log probability (usually from log_softmax) instead
    of unnormalized numbers.
weight : float or None
    Global scalar weight for loss.
axis : int, default -1
    The dimension along with to compute softmax. Only used when `from_logits`
    is False.
sample_weight : Symbol or None
    Per sample weighting. Must be broadcastable to
    the same shape as loss. For example, if loss has
    shape (64, 10) and you want to weight each sample
    in the batch, `sample_weight` should have shape (64, 1).
batch_axis : int, default 0
    The axis that represents mini-batch.

NAME

AI::MXNet::Gluon::CTCLoss

DESCRIPTION

Connectionist Temporal Classification Loss.

See `"Connectionist Temporal Classification: Labelling Unsegmented
Sequence Data with Recurrent Neural Networks"
<http://www.cs.toronto.edu/~graves/icml_2006.pdf>`_ paper for more information.

Parameters
----------
layout : str, default 'NTC'
    Layout of the output sequence activation vector.
label_layout : str, default 'NT'
    Layout of the labels.
weight : float or None
    Global scalar weight for loss.
sample_weight : Symbol or None
    Per sample weighting. Must be broadcastable to
    the same shape as loss. For example, if loss has
    shape (64, 10) and you want to weight each sample
    in the batch, `sample_weight` should have shape (64, 1).
    This should be used as the fifth argument when calling this loss.

Input shapes:
    `data` is an activation tensor (i.e. before softmax).
    Its shape depends on `layout`. For `layout='TNC'`, this
    input has shape `(sequence_length, batch_size, alphabet_size)`
    Note that the last dimension with index `alphabet_size-1` is reserved for special
    blank character.

    `label` is the label index matrix with zero-indexed labels.
    Its shape depends on `label_layout`. For `label_layout='TN'`, this
    input has shape `(label_sequence_length, batch_size)`. Padding mask of value ``-1``
    is available for dealing with unaligned label lengths.
    When `label_lengths` is specified, label lengths are directly used and padding mask
    is not allowed in the label.
    When `label_lengths` is not specified, the first occurrence of ``-1``
    in each sample marks the end of the label sequence of that sample.

    For example, suppose the vocabulary is `[a, b, c]`, and in one batch we have three
    sequences 'ba', 'cbb', and 'abac'. We can index the labels as `{'a': 0, 'b': 1, 'c': 2}`.
    The alphabet size should be 4, and we reserve the channel index 3 for blank label
    in data tensor. The padding mask value for extra length is -1, so the resulting `label`
    tensor should be padded to be::

      [[1, 0, -1, -1], [2, 1, 1, -1], [0, 1, 0, 2]]

    `data_lengths` is optional and defaults to None.
    When specified, it represents the actual lengths of data.
    The shape should be (batch_size,).
    If None, the data lengths are treated as being equal to the max sequence length.
    This should be used as the third argument when calling this loss.

    `label_lengths` is optional and defaults to None.
    When specified, it represents the actual lengths of labels.
    The shape should be (batch_size,).
    If None, the label lengths are derived from the first occurrence of
    the value specified by `padding_mask`.
    This should be used as the fourth argument when calling this loss.

Output shape:
    The CTC loss output has the shape (batch_size,).

NAME

AI::MXNet::Gluon::HuberLoss

DESCRIPTION

Calculates smoothed L1 loss that is equal to L1 loss if absolute error
exceeds rho but is equal to L2 loss otherwise. Also called SmoothedL1 loss.

.. math::
    L = \sum_i \begin{cases} \frac{1}{2 {rho}} ({pred}_i - {label}_i)^2 &
                       \text{ if } |{pred}_i - {label}_i| < {rho} \\
                       |{pred}_i - {label}_i| - \frac{{rho}}{2} &
                       \text{ otherwise }
        \end{cases}

`pred` and `label` can have arbitrary shape as long as they have the same
number of elements.

Parameters
----------
rho : float, default 1
    Threshold for trimmed mean estimator.
weight : float or None
    Global scalar weight for loss.
batch_axis : int, default 0
    The axis that represents mini-batch.


Inputs:
    - **pred**: prediction tensor with arbitrary shape
    - **label**: target tensor with the same size as pred.
    - **sample_weight**: element-wise weighting tensor. Must be broadcastable
      to the same shape as pred. For example, if pred has shape [64, 10]
      and you want to weigh each sample in the batch separately,
      sample_weight should have shape [64, 1].

Outputs:
    - **loss**: loss tensor with shape [batch_size]. Dimenions other than
      batch_axis are averaged out.

NAME

AI::MXNet::Gluon::HingeLoss

DESCRIPTION

Calculates the hinge loss function often used in SVMs:

.. math::
    L = \sum_i max(0, {margin} - {pred}_i \cdot {label}_i)

where `pred` is the classifier prediction and `label` is the target tensor
containing values -1 or 1. `pred` and `label` must have the same number of
elements.

Parameters
----------
margin : float
    The margin in hinge loss. Defaults to 1.0
weight : float or None
    Global scalar weight for loss.
batch_axis : int, default 0
    The axis that represents mini-batch.


Inputs:
    - **pred**: prediction tensor with arbitrary shape.
    - **label**: truth tensor with values -1 or 1. Must have the same size
      as pred.
    - **sample_weight**: element-wise weighting tensor. Must be broadcastable
      to the same shape as pred. For example, if pred has shape (64, 10)
      and you want to weigh each sample in the batch separately,
      sample_weight should have shape (64, 1).

Outputs:
    - **loss**: loss tensor with shape (batch_size,). Dimenions other than
      batch_axis are averaged out.

NAME

AI::MXNet::Gluon::SquaredHingeLoss

DESCRIPTION

Calculates the soft-margin loss function used in SVMs:

.. math::
    L = \sum_i max(0, {margin} - {pred}_i \cdot {label}_i)^2

where `pred` is the classifier prediction and `label` is the target tensor
containing values -1 or 1. `pred` and `label` can have arbitrary shape as
long as they have the same number of elements.

Parameters
----------
margin : float
    The margin in hinge loss. Defaults to 1.0
weight : float or None
    Global scalar weight for loss.
batch_axis : int, default 0
    The axis that represents mini-batch.


Inputs:
    - **pred**: prediction tensor with arbitrary shape
    - **label**: truth tensor with values -1 or 1. Must have the same size
      as pred.
    - **sample_weight**: element-wise weighting tensor. Must be broadcastable
      to the same shape as pred. For example, if pred has shape (64, 10)
      and you want to weigh each sample in the batch separately,
      sample_weight should have shape (64, 1).

Outputs:
    - **loss**: loss tensor with shape (batch_size,). Dimenions other than
      batch_axis are averaged out.

NAME

AI::MXNet::Gluon::LogisticLoss

DESCRIPTION

Calculates the logistic loss (for binary losses only):

.. math::
    L = \sum_i \log(1 + \exp(- {pred}_i \cdot {label}_i))

where `pred` is the classifier prediction and `label` is the target tensor
containing values -1 or 1 (0 or 1 if `label_format` is binary).
 `pred` and `label` can have arbitrary shape as long as they have the same number of elements.

Parameters
----------
weight : float or None
    Global scalar weight for loss.
batch_axis : int, default 0
    The axis that represents mini-batch.
label_format : str, default 'signed'
    Can be either 'signed' or 'binary'. If the label_format is 'signed', all label values should
    be either -1 or 1. If the label_format is 'binary', all label values should be either
    0 or 1.

Inputs:
    - **pred**: prediction tensor with arbitrary shape.
    - **label**: truth tensor with values -1/1 (label_format is 'signed')
      or 0/1 (label_format is 'binary'). Must have the same size as pred.
    - **sample_weight**: element-wise weighting tensor. Must be broadcastable
      to the same shape as pred. For example, if pred has shape (64, 10)
      and you want to weigh each sample in the batch separately,
      sample_weight should have shape (64, 1).

Outputs:
    - **loss**: loss tensor with shape (batch_size,). Dimenions other than
      batch_axis are averaged out.

NAME

AI::MXNet::Gluon::TripletLoss

DESCRIPTION

Calculates triplet loss given three input tensors and a positive margin.
Triplet loss measures the relative similarity between prediction, a positive
example and a negative example:

.. math::
    L = \sum_i \max(\Vert {pred}_i - {pos_i} \Vert_2^2 -
                    \Vert {pred}_i - {neg_i} \Vert_2^2 + {margin}, 0)

`pred`, `positive` and `negative` can have arbitrary shape as long as they
have the same number of elements.

Parameters
----------
margin : float
    Margin of separation between correct and incorrect pair.
weight : float or None
    Global scalar weight for loss.
batch_axis : int, default 0
    The axis that represents mini-batch.


Inputs:
    - **pred**: prediction tensor with arbitrary shape
    - **positive**: positive example tensor with arbitrary shape. Must have
      the same size as pred.
    - **negative**: negative example tensor with arbitrary shape Must have
      the same size as pred.

Outputs:
    - **loss**: loss tensor with shape (batch_size,).

NAME

AI::MXNet::Gluon::PoissonNLLLoss

DESCRIPTION

For a target (Random Variable) in a Poisson distribution, the function calculates the Negative
Log likelihood loss.
PoissonNLLLoss measures the loss accrued from a poisson regression prediction made by the model.

.. math::
    L = \text{pred} - \text{target} * \log(\text{pred}) +\log(\text{target!})

`pred`, `target` can have arbitrary shape as long as they have the same number of elements.

Parameters
----------
from_logits : boolean, default True
    indicating whether log(predicted) value has already been computed. If True, the loss is computed as
    :math:`\exp(\text{pred}) - \text{target} * \text{pred}`, and if False, then loss is computed as
    :math:`\text{pred} - \text{target} * \log(\text{pred}+\text{epsilon})`.The default value
weight : float or None
    Global scalar weight for loss.
batch_axis : int, default 0
    The axis that represents mini-batch.
compute_full: boolean, default False
    Indicates whether to add an approximation(Stirling factor) for the Factorial term in the formula for the loss.
    The Stirling factor is:
    :math:`\text{target} * \log(\text{target}) - \text{target} + 0.5 * \log(2 * \pi * \text{target})`
epsilon: float, default 1e-08
    This is to avoid calculating log(0) which is not defined.


Inputs:
    - **pred**:   Predicted value
    - **target**: Random variable(count or number) which belongs to a Poisson distribution.
    - **sample_weight**: element-wise weighting tensor. Must be broadcastable
      to the same shape as pred. For example, if pred has shape (64, 10)
      and you want to weigh each sample in the batch separately,
      sample_weight should have shape (64, 1).

Outputs:
    - **loss**: Average loss (shape=(1,1)) of the loss tensor with shape (batch_size,).

NAME

AI::MXNet::Gluon::CosineEmbeddingLoss

DESCRIPTION

For a target label 1 or -1, vectors input1 and input2, the function computes the cosine distance
between the vectors. This can be interpreted as how similar/dissimilar two input vectors are.

.. math::

    L = \sum_i \begin{cases} 1 - {cos\_sim({input1}_i, {input2}_i)} & \text{ if } {label}_i = 1\\
                     {cos\_sim({input1}_i, {input2}_i)} & \text{ if } {label}_i = -1 \end{cases}\\
    cos\_sim(input1, input2) = \frac{{input1}_i.{input2}_i}{||{input1}_i||.||{input2}_i||}

`input1`, `input2` can have arbitrary shape as long as they have the same number of elements.

Parameters
----------
weight : float or None
    Global scalar weight for loss.
batch_axis : int, default 0
    The axis that represents mini-batch.
margin : float
    Margin of separation between correct and incorrect pair.


Inputs:
    - **input1**: a tensor with arbitrary shape
    - **input2**: another tensor with same shape as pred to which input1 is
      compared for similarity and loss calculation
    - **label**: A 1-D tensor indicating for each pair input1 and input2, target label is 1 or -1
    - **sample_weight**: element-wise weighting tensor. Must be broadcastable
      to the same shape as input1. For example, if input1 has shape (64, 10)
      and you want to weigh each sample in the batch separately,
      sample_weight should have shape (64, 1).

Outputs:
    - **loss**: The loss tensor with shape (batch_size,).