class torch.nn.NLLLoss(weight: Optional[torch.Tensor] = None, size_average=None, ignore_index: int = -100, reduce=None, reduction: str = 'mean')
The negative log likelihood loss. It is useful to train a classification problem with
If provided, the optional argument
weight should be a 1D Tensor assigning weight to each of the classes. This is particularly useful when you have an unbalanced training set.
input given through a forward call is expected to contain log-probabilities of each class.
input has to be a Tensor of size either or with for the
K-dimensional case (described later).
Obtaining log-probabilities in a neural network is easily achieved by adding a
LogSoftmax layer in the last layer of your network. You may use
CrossEntropyLoss instead, if you prefer not to add an extra layer.
target that this loss expects should be a class index in the range where
C = number of classes; if
ignore_index is specified, this loss also accepts this class index (this index may not necessarily be in the class range).
The unreduced (i.e. with
reduction set to
'none') loss can be described as:
where is the input, is the target, is the weight, and is the batch size. If
reduction is not
Can also be used for higher dimension inputs, such as 2D images, by providing an input of size with , where is the number of dimensions, and a target of appropriate shape (see below). In the case of images, it computes NLL loss per-pixel.
C. Otherwise, it is treated as if having all ones.
reduction). By default, the losses are averaged over each loss element in the batch. Note that for some losses, there are multiple elements per sample. If the field
size_averageis set to
False, the losses are instead summed for each minibatch. Ignored when reduce is
True, the loss is averaged over non-ignored targets.
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
'none': no reduction will be applied,
'mean': the weighted mean of the output is taken,
'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
C = number of classes, or with in the case of
'none', then the same size as the target: , or with in the case of K-dimensional loss.
>>> m = nn.LogSoftmax(dim=1) >>> loss = nn.NLLLoss() >>> # input is of size N x C = 3 x 5 >>> input = torch.randn(3, 5, requires_grad=True) >>> # each element in target has to have 0 <= value < C >>> target = torch.tensor([1, 0, 4]) >>> output = loss(m(input), target) >>> output.backward() >>> >>> >>> # 2D loss example (used, for example, with image inputs) >>> N, C = 5, 4 >>> loss = nn.NLLLoss() >>> # input is of size N x C x height x width >>> data = torch.randn(N, 16, 10, 10) >>> conv = nn.Conv2d(16, C, (3, 3)) >>> m = nn.LogSoftmax(dim=1) >>> # each element in target has to have 0 <= value < C >>> target = torch.empty(N, 8, 8, dtype=torch.long).random_(0, C) >>> output = loss(m(conv(data)), target) >>> output.backward()
© 2019 Torch Contributors
Licensed under the 3-clause BSD License.