Python wrappers
Public Python APIs implemented by this engine symbol.Autograd node for negative log-likelihood loss.
Operates on log-probabilities (e.g. the output of log_softmax).
The per-sample loss is
and the final scalar is obtained by reducing according to
reduction_; samples whose target equals ignore_index_ are skipped.
The backward emits a sparse gradient that is nonzero only at the target
class:
Math
Attributes
schema_ v1OpSchema"nll_loss", AmpPolicy::ForceFP32.reduction_ReductionReduction mode.
ignore_ index_intTarget class to skip; default
-100.has_ weight_boolWhether
saved_weight_ is populated.orig_ input_ shape_ShapeFull input shape
(N, C, ...) for backward reconstruction.saved_ target_StorageInteger class labels.
saved_ weight_StorageOptional per-class weight vector.
saved_ valid_ count_StorageScalar count of non-ignored samples for the
Mean denominator.Notes
Feeding raw logits or softmax probabilities produces incorrect results —
the input must already be in log-probability space. Prefer
CrossEntropyBackward when starting from logits.
Static methods
1static
forward
→TensorImplPtrint forward(const int & input, const int & target, const int & weight_or_null, Reduction reduction, int ignore_index)Compute NLL loss with autograd wiring.
Parameters
inputTensorImplPtrLog-probabilities of shape
(N, C) or (N, C, d_1, ...).targetTensorImplPtrInteger class labels of shape
(N,) or (N, d_1, ...).weight_ or_ nullTensorImplPtrOptional per-class weight vector of length ; may be null.
reductionReductionMean, Sum, or None.ignore_ indexintClass index excluded from the loss / gradient.
Returns
TensorImplPtrReduced or per-sample loss tensor.