mindspore.mint.nn.CosineEmbeddingLoss

View Source On Gitee
class mindspore.mint.nn.CosineEmbeddingLoss(margin=0.0, reduction='mean')[source]

CosineEmbeddingLoss creates a criterion to measure the similarity between two tensors using cosine distance.

Given two Tensors \(x1\), \(x2\), and a Tensor label \(y\) (positive samples use 1 and negative samples use -1), the formula is as follows:

\[\begin{split}loss(x_1, x_2, y) = \begin{cases} 1-cos(x_1, x_2), & \text{if } y = 1\\ \max(0, cos(x_1, x_2)-margin), & \text{if } y = -1\\ \end{cases}\end{split}\]
Parameters
  • margin (float, optional) – A tuning factor used in the negative-sample branch, which should be in [-1.0, 1.0], values outside this range will not raise an error, but have no practical meaning. Default: 0.0 .

  • reduction (str, optional) –

    Apply specific reduction method to the output: 'none' , 'mean' , 'sum' . Default: 'mean' .

    • 'none' : no reduction will be applied.

    • 'mean' : compute and return the mean of elements in the output.

    • 'sum' : the output elements will be summed.

Inputs:
  • input1 (Tensor) - Input Tensor of shape \((N, D)\) or \((D)\) , where \(N\) is the batch size and \(D\) is the embedding dimension.

  • input2 (Tensor) - Input Tensor of shape \((N, D)\) or \((D)\) , which has same dtype as input1, and its shape should be the same as input1 or broadcastable to the shape of input1.

  • target (Tensor) - Target Tensor of shape \((N)\) or \(()\) , contains value 1 or -1.

Outputs:

Tensor or Scalar, if reduction is "none", a Tensor with the same shape as target will be returned. Otherwise, a Scalar value will be returned.

Raises
  • ValueError – If reduction is not "none", "mean" or "sum".

  • ValueError – If the shapes of input1 and input2 do not match.

  • ValueError – If the shape of target does not match the shapes of input1 and input2.

Supported Platforms:

Ascend

Examples

>>> import mindspore as ms
>>> import numpy as np
>>> from mindspore import mint
>>> input1 = ms.Tensor(np.array([[0.3, 0.8], [0.4, 0.3]]), ms.float32)
>>> input2 = ms.Tensor(np.array([[0.4, 1.2], [-0.4, -0.9]]), ms.float32)
>>> target = ms.Tensor(np.array([1, -1]), ms.int32)
>>> cosine_embedding_loss = mint.nn.CosineEmbeddingLoss()
>>> output = cosine_embedding_loss(input1, input2, target)
>>> print(output)
0.0003425479