cross_entropy
→Tensorcross_entropy(x: Tensor, target: Tensor, weight: Tensor | None = None, ignore_index: int = -100, reduction: str = 'mean', label_smoothing: float = 0.0)Cross-entropy loss for multi-class classification.
The canonical training objective for categorical classifiers.
Combines lucid.nn.functional.log_softmax and
nll_loss into a single, numerically stable expression —
operating directly on raw logits avoids the catastrophic
cancellation that arises when softmax probabilities are taken
to log(). Implements the full contract: per-class
weight rescaling, ignore_index masking, and
label-smoothing regularisation.
Parameters
xTensortargetTensorx.weightTensor or None= Noneignore_indexint= -100-100).
Common for masked / padded targets in sequence models.reductionstr= 'mean'"mean" (default), "sum", or "none". Under
"mean", the divisor is the sum of effective sample weights
(after weight and ignore_index), not the raw element
count.label_smoothingfloat= 0.0Returns
TensorScalar ("mean"/"sum") or per-sample tensor ("none").
Notes
Per-sample loss:
where is the (smoothed) target distribution.
With label_smoothing = \alpha,
Gradient w.r.t. the logits has the well-known clean form (up to weighting), which is the reason cross-entropy is preferred over MSE for classification: no sigmoid saturation, no vanishing gradient.
Examples
>>> import lucid
>>> from lucid.nn.functional import cross_entropy
>>> logits = lucid.tensor([[2.0, 0.5, 0.1], [0.0, 1.5, 0.2]])
>>> target = lucid.tensor([0, 1])
>>> cross_entropy(logits, target)
Tensor(0.3490...)