nn_multilabel_margin_loss {torch} | R Documentation |
Multilabel margin loss
Description
Creates a criterion that optimizes a multi-class multi-classification
hinge loss (margin-based loss) between input (a 2D mini-batch
Tensor
)
and output (which is a 2D
Tensor
of target class indices).
For each sample in the mini-batch:
Usage
nn_multilabel_margin_loss(reduction = "mean")
Arguments
reduction |
(string, optional): Specifies the reduction to apply to the output:
|
Details
where , \
, \
, \
and
for all
and
.
and
must have the same size.
The criterion only considers a contiguous block of non-negative targets that starts at the front. This allows for different samples to have variable amounts of target classes.
Shape
Input:
or
where
N
is the batch size andC
is the number of classes.Target:
or
, label targets padded by -1 ensuring same shape as the input.
Output: scalar. If
reduction
is'none'
, then.
Examples
if (torch_is_installed()) {
loss <- nn_multilabel_margin_loss()
x <- torch_tensor(c(0.1, 0.2, 0.4, 0.8))$view(c(1, 4))
# for target y, only consider labels 4 and 1, not after label -1
y <- torch_tensor(c(4, 1, -1, 2), dtype = torch_long())$view(c(1, 4))
loss(x, y)
}