mindspore.ops.cross_entropy๏ƒ

View Source On Gitee
mindspore.ops.cross_entropy(input, target, weight=None, ignore_index=- 100, reduction='mean', label_smoothing=0.0)[source]๏ƒ

The cross entropy loss between input and target.

The cross entropy support two kind of targets:

  • Class indices (int) in the range [0,C) where C is the number of classes, the loss with reduction=none can be described as:

    โ„“(x,y)=L={l1,โ€ฆ,lN}โŠค,ln=โˆ’wynlogโกexpโก(xn,yn)โˆ‘c=1Cexpโก(xn,c)โ‹…1{ynโ‰ ignore_index}

    where x is the inputs, y is the target, w is the weight, N is the batch size, c belonging to [0,Cโˆ’1] is class index, where C is the number of classes.

    If reduction is not None (default 'mean' ), then

    โ„“(x,y)={โˆ‘n=1N1โˆ‘n=1Nwynโ‹…1{ynโ‰ ignore_index}ln,if reduction='mean',โˆ‘n=1Nln,if reduction='sum'.
  • Probabilities (float) for each class, useful when labels beyond a single class per minibatch item are required, the loss with reduction=none can be described as:

    โ„“(x,y)=L={l1,โ€ฆ,lN}โŠค,ln=โˆ’โˆ‘c=1Cwclogโกexpโก(xn,c)โˆ‘i=1Cexpโก(xn,i)yn,c

    where x is the inputs, y is the target, w is the weight, N is the batch size, c belonging to [0,Cโˆ’1] is class index, where C is the number of classes.

    If reduction is not None (default 'mean' ), then

    โ„“(x,y)={โˆ‘n=1NlnN,if reduction='mean',โˆ‘n=1Nln,if reduction='sum'.
Parameters
  • input (Tensor) โ€“ (N) or (N,C) where C = number of classes or (N,C,H,W) in case of 2D Loss, or (N,C,d1,d2,...,dK). input is expected to be log-probabilities, data type must be float16 or float32.

  • target (Tensor) โ€“ For class indices, tensor of shape (), (N) or (N,d1,d2,...,dK) , data type must be int32. For probabilities, tensor of shape (C,) , (N,C) or (N,C,d1,d2,...,dK) , data type must be float16 or float32 or float64.

  • weight (Tensor) โ€“ A rescaling weight applied to the loss of each batch element. If not None, the shape is (C,), data type must be float16 or float32. Default: None .

  • ignore_index (int) โ€“ Specifies a target value that is ignored and does not contribute to the input gradient. Default: -100 .

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

  • label_smoothing (float) โ€“ Label smoothing values, a regularization tool used to prevent the model from overfitting when calculating Loss. The value range is [0.0, 1.0]. Default value: 0.0 .

Returns

Tensor, the computed loss value.

Supported Platforms:

Ascend GPU CPU

Examples

>>> import mindspore as ms
>>> import numpy as np
>>> # Case 1: Indices labels
>>> inputs = ms.Tensor(np.random.randn(3, 5), ms.float32)
>>> target = ms.Tensor(np.array([1, 0, 4]), ms.int32)
>>> output = ms.ops.cross_entropy(inputs, target)
>>> # Case 2: Probability labels
>>> inputs = ms.Tensor(np.random.randn(3, 5), ms.float32)
>>> target = ms.Tensor(np.random.randn(3, 5), ms.float32)
>>> output = ms.ops.cross_entropy(inputs, target)