fn
nll_loss
→Tensornll_loss(x: Tensor, target: Tensor, weight: Tensor | None = None, ignore_index: int = -100, reduction: str = 'mean')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
xTensorLog-probabilities of shape or
.
targetTensorInteger class indices of shape /
.
weightTensor or None= NonePer-class weight vector .
ignore_indexint= -100Class index whose samples are excluded (default
-100).reductionstr= 'mean'"mean" (default), "sum", or "none".Returns
TensorScalar or per-sample tensor depending on reduction.
Notes
Per-sample loss:
Under "mean" reduction, the divisor is the sum of effective
sample weights — i.e.,
— 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.3490...)