Skip to content
fn

nll_loss

→Tensor
nll_loss(input: Tensor, target: Tensor, weight: Tensor | None = None, size_average: bool | None = None, ignore_index: int = -100, reduce: bool | None = None, reduction: Reduction = 'mean')

Implementing kernel

C++ engine symbols that back this Python API.

Negative log-likelihood loss for multi-class classification.

The "back-half" of cross_entropy: assumes the input is already a tensor of log-probabilities (typically produced by lucid.nn.functional.log_softmax). Provided as a separate entry-point so models that need log-probabilities for downstream use (e.g., beam search) can avoid recomputing them.

Parameters

inputTensor
Log-probabilities of shape (N,C)(N, C) or (N,C,d1,…,dk)(N, C, d_1, \dots, d_k), or (C,)(C,) for one unbatched sample (with a 0-d target, and a 0-d loss under "none").
targetTensor
Integer class indices of shape (N,)(N,) / (N,d1,…,dk)(N, d_1, \dots, d_k); any other shape raises ValueError. An index outside [0,C)[0, C) that is not ignore_index raises IndexError for a CPU tensor and makes the loss NaN on Metal.
weightTensor or None= None
Per-class weight vector (C,)(C,) of input's dtype, else RuntimeError. Any other shape raises ValueError.
ignore_indexint= -100
Class index whose samples are excluded (default -100).
reductionstr= 'mean'
"mean" (default), "sum", or "none".
size_averagestr= 'mean'
"mean" (default), "sum", or "none".
reducestr= 'mean'
"mean" (default), "sum", or "none".

Returns

Tensor

Scalar or per-sample tensor depending on reduction.

Notes

Per-sample loss:

Li=−wti xi,tiL_i = -w_{t_i}\,x_{i, t_i}

Under "mean" reduction, the divisor is the sum of effective sample weights — i.e., ∑iwti 1[ti≠ignore]\sum_i w_{t_i}\,\mathbb{1}[t_i \ne \text{ignore}] — not the raw count. This matches the standard convention so that the loss is invariant to a global rescaling of weight.

Examples

>>> import lucid
>>> from lucid.nn.functional import nll_loss, log_softmax
>>> logits = lucid.tensor([[2.0, 0.5, 0.1], [0.0, 1.5, 0.2]])
>>> target = lucid.tensor([0, 1])
>>> nll_loss(log_softmax(logits, dim=1), target)
tensor(0.3597)

Used by 2