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.MultilabelMarginLoss

View Source On Gitee
class mindspore.nn.MultilabelMarginLoss(reduction='mean')[source]

Creates a loss criterion that minimizes the hinge loss for multi-class classification tasks. It takes a 2D mini-batch Tensor x as input and a 2D Tensor y containing target class indices as output.

Each sample in the mini-batch, the loss is computed as follows:

loss(x,y)=ijmax(0,1(x[y[j]]x[i]))x.size(0)

where x{0,,x.size(0)1}, y{0,,y.size(0)1}, 0y[j]x.size(0)1, and for all i and j, i does not equal to y[j].

Furthermore, both y and x should have identical sizes.

Note

For this operator, only a contiguous sequence of non-negative targets that starts at the beginning is taken into consideration, which means that different samples can have different number of target classes.

Parameters

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) - Predict data. Tensor of shape (C) or (N,C), where N is the batch size and C is the number of classes. Data type must be float16 or float32.

  • target (Tensor) - Ground truth data, with the same shape as x, data type must be int32 and label targets padded by -1.

Outputs:
  • y (Union[Tensor, Scalar]) - The loss of MultilabelMarginLoss. If reduction is "none", its shape is (N). Otherwise, a scalar value will be returned.

Raises
  • TypeError – If x or target is not a Tensor.

  • TypeError – If dtype of x is neither float16 nor float32.

  • TypeError – If dtype of target is not int32.

  • ValueError – If length of shape of x is neither 1 nor 2.

  • ValueError – If shape of x is not the same as target.

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

Supported Platforms:

Ascend GPU

Examples

>>> import mindspore as ms
>>> import mindspore.nn as nn
>>> import numpy as np
>>> loss = nn.MultilabelMarginLoss()
>>> x = ms.Tensor(np.array([[0.1, 0.2, 0.4, 0.8], [0.2, 0.3, 0.5, 0.7]]), ms.float32)
>>> target = ms.Tensor(np.array([[1, 2, 0, 3], [2, 3, -1, 1]]), ms.int32)
>>> output = loss(x, target)
>>> print(output)
0.325