Table of Contents
Shortcuts

# torch.nn.functional.nll_loss

torch.nn.functional.nll_loss(input, target, weight=None, size_average=None, ignore_index=- 100, reduce=None, reduction='mean')

  The negative log likelihood loss.

  See `NLLLoss` for details.

  Parameters:

    - input (Tensor) – (N,C)(N, C)(N,C) where C = number of classes or (N,C,H,W)(N, C, H, W)(N,C,H,W) in case of 2D Loss, or (N,C,d 1,d 2,...,d K)(N, C, d_1, d_2, ..., d_K)(N,C,d 1​,d 2​,...,d K​) where K≥1 K \geq 1 K≥1 in the case of K-dimensional loss. input is expected to be log-probabilities.

    - target (Tensor) – (N)(N)(N) where each value is 0≤targets[i]≤C−1 0 \leq \text{targets}[i] \leq C-1 0≤targets[i]≤C−1, or (N,d 1,d 2,...,d K)(N, d_1, d_2, ..., d_K)(N,d 1​,d 2​,...,d K​) where K≥1 K \geq 1 K≥1 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_average` is set to `False`, the losses are instead summed for each minibatch. Ignored when reduce is `False`. Default: `True`

    - 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. Default: -100

    - 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: `True`

    - 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: `'mean'`

  Return type:

    Tensor

  Example:

      >>> # 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 = F.nll_loss(F.log_softmax(input, dim=1), target)
      >>> output.backward()

## Docs

Access comprehensive developer documentation for PyTorch

View Docs

## Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

## Resources

Find development resources and get your questions answered

View Resources

To analyze traffic and optimize your experience, we serve cookies on this site. By clicking or navigating, you agree to allow our usage of cookies. As the current maintainers of this site, Facebook’s Cookies Policy applies. Learn more, including about available controls: Cookies Policy.