torch.nn.functional.cross_entropy
- torch.nn.functional.cross_entropy(input, target, weight=None, size_average=None, ignore_index=-100, reduce=None, reduction,
Compute the cross entropy loss between input logits and target.
See CrossEntropyLoss for details.
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 each class. If given, has to be a Tensor of size C
size_average (bool, optional) – Deprecated (see reduction).
ignore_index (int, optional) – Specifies a target value that is ignored
and does not contribute to the input gradient. When size_average is
True, the loss is averaged over non-ignored targets. Note that
ignore_index is only applicable when the target contains class indices.
Default: -100
reduce (bool, optional) – Deprecated (see reduction).
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 of
elements in the output, 'sum': the output will be summed. Note: size_average
and reduce are in the process of being deprecated, and in the meantime,
specifying either of those two args will override reduction. Default:
label_smoothing (float, optional) – A float in [0.0, 1.0]. Specifies the amount of smoothing when computing the loss, where 0.0 means no smoothing. The targets become a mixture of the original ground truth and a uniform distribution as described in Rethinking the Inception Architecture for Computer Vision. Default: .
Input: Shape , or with in the case of -dimensional loss.
Examples:
>>> # Example of target with class indices
>>> input = torch.randn(3, 5, requires_grad=True)
>>> target = torch.randint(5, (3,), dtype=torch.int64)
>>>
'mean'Target: If containing class indices, shape , or with in the case of K-dimensional loss where each value should be between . If containing class probabilities, same shape as the input and each value should be between .
where: