Rate this Page

torch.nn.functional.cross_entropy#

torch.nn.functional.cross_entropy(input,target,weight=None,size_average=None,ignore_index=-100,reduce=None,reduction='mean',label_smoothing=0.0)[source]#

Compute the cross entropy loss between input logits and target.

SeeCrossEntropyLoss for details.

Parameters
  • input (Tensor) – Predicted unnormalized logits;see Shape section below for supported shapes.

  • target (Tensor) – Ground truth class indices or class probabilities;see Shape section below for supported shapes.

  • weight (Tensor,optional) – a manual rescaling weight given to eachclass. If given, has to be a Tensor of sizeC

  • size_average (bool,optional) – Deprecated (seereduction).

  • ignore_index (int,optional) – Specifies a target value that is ignoredand does not contribute to the input gradient. Whensize_average isTrue, the loss is averaged over non-ignored targets. Note thatignore_index is only applicable when the target contains class indices.Default: -100

  • reduce (bool,optional) – Deprecated (seereduction).

  • reduction (str,optional) – Specifies the reduction to apply to the output:'none' |'mean' |'sum'.'none': no reduction will be applied,'mean': the sum of the output will be divided by the number ofelements in the output,'sum': the output will be summed. Note:size_averageandreduce are in the process of being deprecated, and in the meantime,specifying either of those two args will overridereduction. Default:'mean'

  • label_smoothing (float,optional) – A float in [0.0, 1.0]. Specifies the amountof smoothing when computing the loss, where 0.0 means no smoothing. The targetsbecome a mixture of the original ground truth and a uniform distribution as described inRethinking the Inception Architecture for Computer Vision. Default:0.00.0.

Return type

Tensor

Shape:
  • Input: Shape(C)(C),(N,C)(N, C) or(N,C,d1,d2,...,dK)(N, C, d_1, d_2, ..., d_K) withK1K \geq 1in the case ofK-dimensional loss.

  • Target: If containing class indices, shape()(),(N)(N) or(N,d1,d2,...,dK)(N, d_1, d_2, ..., d_K) withK1K \geq 1 in the case of K-dimensional loss where each value should be between[0,C)[0, C).If containing class probabilities, same shape as the input and each value should be between[0,1][0, 1].

where:

C=number of classesN=batch size\begin{aligned} C ={} & \text{number of classes} \\ N ={} & \text{batch size} \\\end{aligned}

Examples:

>>># Example of target with class indices>>>input=torch.randn(3,5,requires_grad=True)>>>target=torch.randint(5,(3,),dtype=torch.int64)>>>loss=F.cross_entropy(input,target)>>>loss.backward()>>>>>># Example of target with class probabilities>>>input=torch.randn(3,5,requires_grad=True)>>>target=torch.randn(3,5).softmax(dim=1)>>>loss=F.cross_entropy(input,target)>>>loss.backward()