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.

Please select the type of question

Problem description

Describe the bug so that we can quickly locate the problem.

mindspore.ops.margin_ranking_loss

mindspore.ops.margin_ranking_loss(input1, input2, target, margin=0.0, reduction='mean')[source]

MarginRankingLoss creates a criterion that measures the loss.

Given two tensors input1, input2 and a Tensor label target with values 1 or -1, the operation is as follows:

loss(input1,input2,target)=max(0,target(input1input2)+margin)
Parameters
  • input1 (Tensor) – Tensor of shape (N,) where means, any number of additional dimensions.

  • input2 (Tensor) – Tensor of shape (N,), same shape and dtype as input1.

  • target (Tensor) – Contains value 1 or -1. Suppose the shape of input1 is (x1,x2,x3,...,xR), then the shape of target must be (x1,x2,x3,...,xR).

  • margin (float, optional) – Specify the adjustment factor of the operation. 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.

Returns

Tensor or Scalar. if reduction is 'none', its shape is the same as input1. Otherwise, a scalar value will be returned.

Raises
  • TypeError – If margin is not a float.

  • TypeError – If input1, input2 or target is not a Tensor.

  • TypeError – If the types of input1 and input2 are inconsistent.

  • TypeError – If the types of input1 and target are inconsistent.

  • ValueError – If the shape of input1 and input2 are inconsistent.

  • ValueError – If the shape of input1 and target are inconsistent.

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

Supported Platforms:

Ascend GPU CPU

Examples

>>> import mindspore as ms
>>> from mindspore import Tensor, ops
>>> import numpy as np
>>> input1 = Tensor(np.array([0.3864, -2.4093, -1.4076]), ms.float32)
>>> input2 = Tensor(np.array([-0.6012, -1.6681, 1.2928]), ms.float32)
>>> target = ops.Sign()(Tensor(np.array([-2, -2, 3]), ms.float32))
>>> output = ops.margin_ranking_loss(input1, input2, target)
>>> print(output)
1.2293333