MarginRankingLoss

class paddle.nn. MarginRankingLoss ( margin=0.0, reduction='mean', name=None ) [source]

This interface is used to construct a callable object of the MarginRankingLoss class. The MarginRankingLoss layer calculates the margin rank loss between the input, other and label , use the math function as follows.

\[margin\_rank\_loss = max(0, -label * (input - other) + margin)\]

If reduction set to 'mean', the reduced mean loss is:

\[Out = MEAN(margin\_rank\_loss)\]

If reduction set to 'sum', the reduced sum loss is:

\[Out = SUM(margin\_rank\_loss)\]

If reduction set to 'none', just return the origin margin_rank_loss.

Parameters
  • margin (float, optional) – The margin value to add, default value is 0;

  • reduction (str, optional) – Indicate the reduction to apply to the loss, the candidates are 'none', 'mean', 'sum'.If reduction is 'none', the unreduced loss is returned; If reduction is 'mean', the reduced mean loss is returned. If reduction is 'sum', the reduced sum loss is returned. Default is '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.

other: N-D Tensor, other have the same shape and dtype as input.

label: N-D Tensor, label have the same shape and dtype as input.

output: If reduction is 'mean' or 'sum' , the out shape is \([]\), otherwise the shape is the same as input .The same dtype as input tensor.

Returns

A callable object of MarginRankingLoss.

Examples

>>> import paddle

>>> input = paddle.to_tensor([[1, 2], [3, 4]], dtype="float32")
>>> other = paddle.to_tensor([[2, 1], [2, 4]], dtype="float32")
>>> label = paddle.to_tensor([[1, -1], [-1, -1]], dtype="float32")
>>> margin_rank_loss = paddle.nn.MarginRankingLoss()
>>> loss = margin_rank_loss(input, other, label)
>>> print(loss)
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
0.75000000)
forward ( input, other, label )

forward

Defines the computation performed at every call. Should be overridden by all subclasses.

Parameters
  • *inputs (tuple) – unpacked tuple arguments

  • **kwargs (dict) – unpacked dict arguments