nn_multi_margin_loss {torch}R Documentation

Multi margin loss

Description

Creates a criterion that optimizes a multi-class classification hinge loss (margin-based loss) between input xx (a 2D mini-batch Tensor) and output yy (which is a 1D tensor of target class indices, 0y\mboxx.size(1)10 \leq y \leq \mbox{x.size}(1)-1):

Usage

nn_multi_margin_loss(p = 1, margin = 1, weight = NULL, reduction = "mean")

Arguments

p

(int, optional): Has a default value of 11. 11 and 22 are the only supported values.

margin

(float, optional): Has a default value of 11.

weight

(Tensor, optional): a manual rescaling weight given to each class. If given, it has to be a Tensor of size C. Otherwise, it is treated as if having all ones.

reduction

(string, optional): Specifies the reduction to apply to the output: 'none' | 'mean' | 'sum'. 'none': no reduction will be applied, 'mean': the sum of the output will be divided by the number of elements in the output, 'sum': the output will be summed.

Details

For each mini-batch sample, the loss in terms of the 1D input xx and scalar output yy is:

\mboxloss(x,y)=imax(0,\mboxmarginx[y]+x[i]))p\mboxx.size(0) \mbox{loss}(x, y) = \frac{\sum_i \max(0, \mbox{margin} - x[y] + x[i]))^p}{\mbox{x.size}(0)}

where x{0,  ,  \mboxx.size(0)1}x \in \left\{0, \; \cdots , \; \mbox{x.size}(0) - 1\right\} and iyi \neq y.

Optionally, you can give non-equal weighting on the classes by passing a 1D weight tensor into the constructor. The loss function then becomes:

\mboxloss(x,y)=imax(0,w[y](\mboxmarginx[y]+x[i]))p)\mboxx.size(0) \mbox{loss}(x, y) = \frac{\sum_i \max(0, w[y] * (\mbox{margin} - x[y] + x[i]))^p)}{\mbox{x.size}(0)}


[Package torch version 0.13.0 Index]