NLLLoss
- class torch.nn.modules.loss.NLLLoss(weight=None, size_average=None, ignore_index=-100, reduce=None, reduction=
The negative log likelihood loss. It is useful to train a classification problem with C classes.
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.
The 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. The latter is useful for higher dimension inputs, such as computing NLL loss per-pixel for 2D images.
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.
The 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 'none'
(default 'mean'), then
weight (Tensor, optional) – a manual rescaling weight given to each class. If given, it has to be a Tensor of size C. Otherwise, it is treated as if having all ones.
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 are multiple elements per sample. If the field size_average
is set to False, the losses are instead summed for each minibatch. Ignored
when reduce is . Default:
Input: or , where C = number of classes, N = batch size, or with in the case of -dimensional loss.
>>> log_softmax = nn.LogSoftmax(dim=1)
>>> loss_fn = nn.NLLLoss()
>>> # input to NLLLoss is of size N x C = 3 x 5
>>> input = torch.randn(3, 5, requires_grad=True)
Runs the forward pass.
Noneignore_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.
reduce (bool, optional) – Deprecated (see reduction). By default, the
losses are averaged or summed over observations for each minibatch depending
on size_average. When reduce is False, returns a loss per
batch element instead and ignores size_average. Default: None
reduction (str, 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. 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: 'mean'
Target: or , where each value is , or with in the case of K-dimensional loss.
Output: If reduction is 'none', shape or
with in the case of K-dimensional loss.
Otherwise, scalar.