Creates a criterion that optimizes a multi-class multi-classification
hinge loss (margin-based loss) between input Tensor
)
and output Tensor
of target class indices).
For each sample in the mini-batch:
Details
where
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 whereN
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)
}
#> torch_tensor
#> 0.85
#> [ CPUFloatType{} ]