CosineEmbeddingLoss¶
- class paddle.nn. CosineEmbeddingLoss ( margin=0, reduction='mean', name=None ) [source]
- 
         This interface is used to construct a callable object of the CosineEmbeddingLossclass. The CosineEmbeddingLoss layer measures the cosine_embedding loss between input predictionsinput1,input2and target labelslabelwith values 1 or 0. This is used for measuring whether two inputs are similar or dissimilar and is typically used for learning nonlinear embeddings or semi-supervised learning. The cosine embedding loss can be described as:If label = 1, then the loss value can be calculated as follow: \[Out = 1 - cos(input1, input2)\]If label = -1, then the loss value can be calculated as follow: \[Out = max(0, cos(input1, input2)) - margin\]- The operator cos can be described as follow:
- 
           \[cos(x1, x2) = \frac{x1 \cdot{} x2}{\Vert x1 \Vert_2 * \Vert x2 \Vert_2}\]
 - Parameters
- 
           - margin (float, optional) – Should be a number from \(-1\) to \(1\), \(0\) to \(0.5\) is suggested. If - marginis missing, the default value is \(0\).
- reduction (string, optional) – Specifies the reduction to apply to the output: - 'none'|- 'mean'|- 'sum'.- 'none': no reduction will be applied,- 'mean': the sum of the output will be divided by the number of elements in the output,- 'sum': the output will be summed.
- name (str, optional) – Name for the operation (optional, default is None). For more information, please refer to Name. 
 
 - Shape:
- 
           - input1 (Tensor): tensor with shape: [N, M] or [M], ‘N’ means batch size, ‘M’ means the length of input array.
- 
             Available dtypes are float32, float64. 
- input2 (Tensor): tensor with shape: [N, M] or [M], ‘N’ means batch size, ‘M’ means the length of input array.
- 
             Available dtypes are float32, float64. 
- label (Tensor): tensor with shape: [N] or [1]. The target labels values should be -1 or 1.
- 
             Available dtypes are int32, int64, float32, float64. 
- 
             output (Tensor): Tensor, the cosine embedding Loss of Tensor 
             input1input2andlabel.
- 
             If reduction is 'none', the shape of output loss is [N], the same asinput. If reduction is'mean'or'sum', the shape of output loss is [1].
 
 Examples import paddle input1 = paddle.to_tensor([[1.6, 1.2, -0.5], [3.2, 2.6, -5.8]], 'float32') input2 = paddle.to_tensor([[0.5, 0.5, -1.8], [2.3, -1.4, 1.1]], 'float32') label = paddle.to_tensor([1, -1], 'int64') cosine_embedding_loss = paddle.nn.CosineEmbeddingLoss(margin=0.5, reduction='mean') output = cosine_embedding_loss(input1, input2, label) print(output) # [0.21155193] cosine_embedding_loss = paddle.nn.CosineEmbeddingLoss(margin=0.5, reduction='sum') output = cosine_embedding_loss(input1, input2, label) print(output) # [0.42310387] cosine_embedding_loss = paddle.nn.CosineEmbeddingLoss(margin=0.5, reduction='none') output = cosine_embedding_loss(input1, input2, label) print(output) # [0.42310387, 0. ] - 
            
           forward
           (
           input1, 
           input2, 
           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 
 
 
 
