cross_entropy(input, target, weight=None, size_average=None, ignore_index=-100, reduce=None, reduction='mean')¶
This criterion combines log_softmax and nll_loss in a single function.
input (Tensor) – where C = number of classes or in case of 2D Loss, or where in the case of K-dimensional loss.
target (Tensor) – where each value is , or where for K-dimensional loss.
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). By default, the losses are averaged over each loss element in the batch. Note that for some losses, there multiple elements per sample. If the field
size_averageis set to
False, the losses are instead summed for each minibatch. Ignored when reduce is
ignore_index (int, optional) – Specifies a target value that is ignored and does not contribute to the input gradient. When
True, the loss is averaged over non-ignored targets. Default: -100
reduce (bool, optional) – Deprecated (see
reduction). By default, the losses are averaged or summed over observations for each minibatch depending on
False, returns a loss per batch element instead and ignores
reduction (string, optional) – Specifies the reduction to apply to the output:
'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:
reduceare in the process of being deprecated, and in the meantime, specifying either of those two args will override
>>> input = torch.randn(3, 5, requires_grad=True) >>> target = torch.randint(5, (3,), dtype=torch.int64) >>> loss = F.cross_entropy(input, target) >>> loss.backward()