hinge_embedding_loss¶
- paddle.nn.functional. hinge_embedding_loss ( input, label, margin=1.0, reduction='mean', name=None ) [source]
-
Calculates hinge_embedding_loss. Measures the loss given an input tensor \(x\) and a labels tensor \(y\), and is typically used for learning nonlinear embeddings or semi-supervised learning.
The loss function for \(n\)-th sample in the mini-batch is
\[\begin{split}l_n = \begin{cases} x_n, & \text{if}\; y_n = 1,\\ \max \{0, \Delta - x_n\}, & \text{if}\; y_n = -1, \end{cases}\end{split}\]and the total loss functions is
\[\begin{split}\ell(x, y) = \begin{cases} \operatorname{mean}(L), & \text{if reduction} = \text{'mean';}\\ \operatorname{sum}(L), & \text{if reduction} = \text{'sum'.} \end{cases}\end{split}\]where \(L = \{l_1,\dots,l_N\}^\top\).
- Parameters
-
input (Tensor) – Input tensor, the data type is float32 or float64. the shape is [N, *], N is batch size and * means any number of additional dimensions, available dtype is float32, float64.
label (Tensor) – Label tensor containing 1 or -1, the data type is float32 or float64. The shape of label is the same as the shape of input.
margin (float, optional) – Specifies the hyperparameter margin to be used. The value determines how large the input need to be to calculate in hinge_embedding_loss. When label is -1, Input smaller than margin are minimized with hinge_embedding_loss. Default = 1.0
reduction (str, optional) – Indicate how to average the loss by batch_size. the candidates are
'none'
|'mean'
|'sum'
. Ifreduction
is'none'
, the unreduced loss is returned; Ifreduction
is'mean'
, the reduced mean loss is returned; Ifreduction
is'sum'
, the summed loss is returned. Default:'mean'
name (str, optional) – Name for the operation (optional, default is None). For more information, please refer to Name.
Shape:
input: N-D Tensor, the shape is [N, *], N is batch size and * means any number of additional dimensions, available dtype is float32, float64. The sum operation operates over all the elements.
label: N-D Tensor, same shape as the input. tensor elements should containing 1 or -1, the data type is float32 or float64.
output: scalar. If
reduction
is'none'
, then same shape as the input.- Returns
-
Tensor. The tensor variable storing the hinge_embedding_loss of input and label.
Examples
>>> import paddle >>> import paddle.nn.functional as F >>> input = paddle.to_tensor([[1, -2, 3], [0, -1, 2], [1, 0, 1]], dtype=paddle.float32) >>> # label elements in {1., -1.} >>> label = paddle.to_tensor([[-1, 1, -1], [1, 1, 1], [1, -1, 1]], dtype=paddle.float32) >>> loss = F.hinge_embedding_loss(input, label, margin=1.0, reduction='none') >>> print(loss) Tensor(shape=[3, 3], dtype=float32, place=Place(cpu), stop_gradient=True, [[ 0., -2., 0.], [ 0., -1., 2.], [ 1., 1., 1.]]) >>> loss = F.hinge_embedding_loss(input, label, margin=1.0, reduction='mean') >>> print(loss) Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, 0.22222222)