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

View Source On Gitee
class mindspore.nn.FocalLoss(weight=None, gamma=2.0, reduction='mean')[source]

It is a loss function to solve the imbalance of categories and the difference of classification difficulty. The loss function proposed by Kaiming team in their paper Focal Loss for Dense Object Detection improves the effect of image object detection. The function is shown as follows:

FL(pt)=(1pt)γlog(pt)
Parameters
  • gamma (float) – Gamma is used to adjust the steepness of weight curve in focal loss. Default: 2.0 .

  • weight (Union[Tensor, None]) – A rescaling weight applied to the loss of each batch element. The dimension of weight should be 1. If None, no weight is applied. Default: None .

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

Inputs:
  • logits (Tensor) - Tensor of shape should be (N,C) or (N,C,H) or (N,C,H,W). Where C is the number of classes. Its value is greater than 1. If the shape is (N,C,H,W) or (N,C,H), the H or product of H and W should be the same as labels.

  • labels (Tensor) - Tensor of shape should be (N,C) or (N,C,H) or (N,C,H,W). The value of C is 1 or it needs to be the same as predict's C. If C is not 1, the shape of target should be the same as that of predict, where C is the number of classes. If the shape is (N,C,H,W) or (N,C,H), the H or product of H and W should be the same as logits. The value of labels is should be in the range [-C, C). Where C is the number of classes in logits.

Outputs:

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

Raises
  • TypeError – If the data type of gamma is not a float.

  • TypeError – If weight is not a Tensor.

  • ValueError – If labels dim is different from logits.

  • ValueError – If labels channel is not 1 and labels shape is different from logits.

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

Supported Platforms:

Ascend

Examples

>>> import mindspore as ms
>>> import mindspore.nn as nn
>>> logits = ms.Tensor([[0.8, 1.4], [0.5, 0.9], [1.2, 0.9]], ms.float32)
>>> labels = ms.Tensor([[1], [1], [0]], ms.int32)
>>> focalloss = nn.FocalLoss(weight=ms.Tensor([1, 2]), gamma=2.0, reduction='mean')
>>> output = focalloss(logits, labels)
>>> print(output)
0.12516622