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.

mindformers.core.CrossEntropyLoss

View Source On Gitee
class mindformers.core.CrossEntropyLoss(parallel_config=default_dpmp_config, **kwargs)[source]

Calculate the cross entropy loss.

CrossEntropyLoss supports two different types of targets:

  • Class indices (int), where the range of values is [0,C) with C being the number of classes. When reduction is set to 'none', the cross-entropy loss is computed as follows:

    (x,y)=L={l1,,lN},ln=wynlogexp(xn,yn)c=1Cexp(xn,c)1{ynignore_index}

    where x denotes the predicted values, t denotes the target values, w denotes the weights, and N is the batch size. The index c ranges from [0, C-1], representing the class indices, where C is the number of classes.

    If reduction is not set to 'none' (the default is 'mean'), the loss is computed as:

    (x,y)={n=1N1n=1Nwyn1{ynignore_index}ln,if reduction='mean',n=1Nln,if reduction='sum'.
  • Class probabilities (float), used when the target is a probability distribution over multiple class labels. When reduction is set to 'none', the cross-entropy loss is computed as follows:

    (x,y)=L={l1,,lN},ln=c=1Cwclogexp(xn,c)i=1Cexp(xn,i)yn,c

    where x denotes the predicted values, t denotes the target values, w denotes the weights, and N is the batch size. The index c ranges from [0, C-1], representing the class indices, where C is the number of classes.

    If reduction is not set to 'none' (the default is 'mean'), the loss is computed as:

    (x,y)={n=1NlnN,if reduction='mean',n=1Nln,if reduction='sum'.
Parameters

parallel_config (mindformers.modules.transformer.op_parallel_config.OpParallelConfig) – The parallel configuration. Default default_dpmp_config.

Inputs:
  • logits (Tensor) - Tensor of shape (N, C). Data type must be float16 or float32. The output logits of the backbone.

  • label (Tensor) - Tensor of shape (N, ). The ground truth label of the sample.

  • input_mask (Tensor) - Tensor of shape (N, ). input_mask indicates whether there are padded inputs and for padded inputs it will not be counted into loss.

Returns

Tensor, the computed cross entropy loss value.

Examples

>>> import numpy as np
>>> from mindspore import dtype as mstype
>>> from mindspore import Tensor
>>> from mindformers.core import CrossEntropyLoss
>>> loss = CrossEntropyLoss()
>>> logits = Tensor(np.array([[3, 5, 6, 9, 12, 33, 42, 12, 32, 72]]), mstype.float32)
>>> labels_np = np.array([1]).astype(np.int32)
>>> input_mask = Tensor(np.ones(1).astype(np.float32))
>>> labels = Tensor(labels_np)
>>> output = loss(logits, labels, input_mask)
>>> output.shape
(1,)