Document feedback

Question document fragment

When a question document fragment contains a formula, it is displayed as a space.

Submission type
issue

It's a little complicated...

I'd like to ask someone.

Please select the submission type

Problem type
Specifications and Common Mistakes

- Specifications and Common Mistakes:

- Misspellings or punctuation mistakes,incorrect formulas, abnormal display.

- Incorrect links, empty cells, or wrong formats.

- Chinese characters in English context.

- Minor inconsistencies between the UI and descriptions.

- Low writing fluency that does not affect understanding.

- Incorrect version numbers, including software package names and version numbers on the UI.

Usability

- Usability:

- Incorrect or missing key steps.

- Missing main function descriptions, keyword explanation, necessary prerequisites, or precautions.

- Ambiguous descriptions, unclear reference, or contradictory context.

- Unclear logic, such as missing classifications, items, and steps.

Correctness

- Correctness:

- Technical principles, function descriptions, supported platforms, parameter types, or exceptions inconsistent with that of software implementation.

- Incorrect schematic or architecture diagrams.

- Incorrect commands or command parameters.

- Incorrect code.

- Commands inconsistent with the functions.

- Wrong screenshots.

- Sample code running error, or running results inconsistent with the expectation.

Risk Warnings

- Risk Warnings:

- Lack of risk warnings for operations that may damage the system or important data.

Content Compliance

- Content Compliance:

- Contents that may violate applicable laws and regulations or geo-cultural context-sensitive words and expressions.

- Copyright infringement.

Problem description

Agree to Privacy Statement

mindspore.ops.TripletMarginLoss

class mindspore.ops.TripletMarginLoss(p=2, swap=False, eps=1e-06, reduction='mean')[source]

TripletMarginLoss operation.

Creates a criterion that measures the triplet loss given an input tensors x1, x2, x3 and a margin with a value greater than 0. This is used for measuring a relative similarity between samples. A triplet is composed by a, p and n (i.e., anchor, positive examples and negative examples respectively). The shapes of all input tensors should be (N,D).

The distance swap is described in detail in the paper Learning local feature descriptors with triplets and shallow convolutional neural networks by V. Balntas, E. Riba et al.

The loss function for each sample in the mini-batch is:

L(a,p,n)=max{d(ai,pi)d(ai,ni)+margin,0}

where

d(xi,yi)=xiyip
Parameters
  • p (int, optional) – The norm degree for pairwise distance. Default: 2 .

  • eps (float, optional) – Default: 1e-6 .

  • swap (bool, optional) – The distance swap. Default: False .

  • 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:
  • x (Tensor) - A sample randomly selected from the training set. Data type must be BasicType.

  • positive (Tensor) - A sample belonging to the same category as x, with the same type and shape as x.

  • negative (Tensor) - A sample belonging to the different class from x, with the same type and shape as x.

  • margin (Tensor) - Make a margin between the positive pair and the negative pair.

Outputs:

Union[Tensor, Scalar], if reduction is "none", its shape is (N). Otherwise, a scalar value will be returned.

Raises
  • TypeError – If x or positive or negative or margin is not a Tensor.

  • TypeError – If dtype of x or positive or negative is not BasicType.

  • TypeError – If dtype of x, positive and negative is not the same.

  • TypeError – If margin is not float32.

  • TypeError – If p is not an int.

  • TypeError – If eps is not a float.

  • TypeError – If swap is not a bool.

  • ValueError – If dimensions of input x, positive and negative are less than or equal to 1 at the same time.

  • ValueError – If the dimension of input x or positive or negative is bigger than or equal to 8.

  • ValueError – If length of shape of margin is not 0.

  • ValueError – If shape of x, positive and negative cannot broadcast.

  • ValueError – If reduction is not one of 'none', 'mean', 'sum'.

Supported Platforms:

GPU

Examples

>>> import mindspore
>>> import numpy as np
>>> from mindspore import Tensor, ops
>>> loss = ops.TripletMarginLoss()
>>> x = Tensor(np.array([[0.3, 0.7], [0.5, 0.5]]), mindspore.float32)
>>> positive = Tensor(np.array([[0.4, 0.6], [0.4, 0.6]]), mindspore.float32)
>>> negative = Tensor(np.array([[0.2, 0.9], [0.3, 0.7]]), mindspore.float32)
>>> margin = Tensor(1.0, mindspore.float32)
>>> output = loss(x, positive, negative, margin)
>>> print(output)
0.8881968