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.

PR

Just a small problem.

I can fix it online!

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.nn.MultiMarginLoss

View Source On Gitee
class mindspore.nn.MultiMarginLoss(p=1, margin=1.0, reduction='mean', weight=None)[source]

Creates a criterion that optimizes a multi-class classification hinge loss (margin-based loss) between input x (a 2D mini-batch Tensor) and output y (which is a 1D tensor of target class indices, 0yx.size(1)1):

For each mini-batch sample, the loss in terms of the 1D input x and scalar output y is:

loss(x,y)=imax(0,w[y](marginx[y]+x[i]))px.size(0)

where x{0,,x.size(0)1} and iy.

Parameters
  • p (int, optional) – The norm degree for pairwise distance. Should be 1 or 2. Default: 1 .

  • margin (float, optional) – A parameter to change pairwise distance. Default: 1.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 weighted mean of elements in the output.

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

  • weight (Tensor, optional) – The rescaling weight to each class with shape (C,). Data type only support float32, float16 or float64. Default: None , all classes are weighted equally.

Inputs:
  • x (Tensor) - Input x, with shape (N,C). Data type only support float32, float16 or float64. x is x in the above formula.

  • target (Tensor) - Ground truth labels, with shape (N,). Data type only support int64. The value of target should be non-negative, less than C. target is y in the above formula.

Outputs:

Tensor, When reduction is 'none', the shape is (N,). Otherwise, it is a scalar. Has the same data type with x.

Raises
  • TypeError – If dtype of p or target is not int.

  • TypeError – If dtype of margin is not float.

  • TypeError – If dtype of reduction is not str.

  • TypeError – If dtype of x is not float16, float or float64.

  • TypeError – If dtype of weight and x is not the same.

  • ValueError – If ‘p’ is not 1 or 2.

  • ValueError – If ‘reduction’ is not one of { 'none' , 'sum' , 'mean' }.

  • ValueError – If shape[0] of x is not equal to shape[0] of target.

  • ValueError – If shape[1] of x is not equal to shape[0] of weight.

  • ValueError – IF rank of weight is not 1.

  • ValueError – If rank of x is not 2 or rank of ‘target’ is not 1.

Supported Platforms:

Ascend GPU CPU

Examples

>>> import mindspore as ms
>>> import mindspore.nn as nn
>>> import numpy as np
>>> x = ms.Tensor(np.ones(shape=[3, 3]), ms.float32)
>>> target = ms.Tensor(np.array([1, 2, 1]), ms.int64)
>>> loss = nn.MultiMarginLoss()
>>> output = loss(x, target)
>>> print(output)
0.6666667