# oneflow.nn.CrossEntropyLoss¶

class oneflow.nn.CrossEntropyLoss(weight: Optional[oneflow.Tensor] = None, ignore_index: int = - 100, reduction: str = 'mean')

This criterion combines LogSoftmax and NLLLoss in one single class.

It is useful when training a classification problem with C classes.

The input is expected to contain raw, unnormalized scores for each class.

input has to be a Tensor of size either $$(minibatch, C)$$ or $$(minibatch, C, d_1, d_2, ..., d_K)$$ with $$K \geq 1$$ for the K-dimensional case (described later).

This criterion expects a class index in the range $$[0, C-1]$$ as the target for each value of a 1D tensor of size minibatch;

The loss can be described as:

$\text{loss}(x, class) = -\log\left(\frac{\exp(x[class])}{\sum_j \exp(x[j])}\right) = -x[class] + \log\left(\sum_j \exp(x[j])\right)$

Can also be used for higher dimension inputs, such as 2D images, by providing an input of size $$(minibatch, C, d_1, d_2, ..., d_K)$$ with $$K \geq 1$$, where $$K$$ is the number of dimensions, and a target of appropriate shape (see below).

Parameters

reduction (string, optional) – Specifies the reduction to apply to the output: 'none' | 'mean' | 'sum'. 'none': no reduction will be applied, 'mean': the weighted mean of the output is taken, 'sum': the output will be summed. Default: 'mean'

For example:

>>> import oneflow as flow
>>> import numpy as np

>>> input = flow.tensor(
...    [[-0.1664078, -1.7256707, -0.14690138],
...        [-0.21474946, 0.53737473, 0.99684894],
...        [-1.135804, -0.50371903, 0.7645404]], dtype=flow.float32)
>>> target = flow.tensor(np.array([0, 1, 2]), dtype=flow.int32)
>>> out = flow.nn.CrossEntropyLoss(reduction="none")(input, target)
>>> out
tensor([0.8020, 1.1167, 0.3583], dtype=oneflow.float32)
>>> out_sum = flow.nn.CrossEntropyLoss(reduction="sum")(input, target)
>>> out_sum
tensor(2.2769, dtype=oneflow.float32)
>>> out_mean = flow.nn.CrossEntropyLoss(reduction="mean")(input, target)
>>> out_mean
tensor(0.7590, dtype=oneflow.float32)

__init__(weight: Optional[oneflow.Tensor] = None, ignore_index: int = - 100, reduction: str = 'mean')None

Initialize self. See help(type(self)) for accurate signature.

Methods

 __call__(*args, **kwargs) Call self as a function. __delattr__(name, /) Implement delattr(self, name). __dir__() Default dir() implementation. __eq__(value, /) Return self==value. __format__(format_spec, /) Default object formatter. __ge__(value, /) Return self>=value. __getattr__(name) __getattribute__(name, /) Return getattr(self, name). __gt__(value, /) Return self>value. __hash__() Return hash(self). __init__([weight, ignore_index, reduction]) Initialize self. __init_subclass__ This method is called when a class is subclassed. __le__(value, /) Return self<=value. __lt__(value, /) Return self