ctc_loss(log_probs: Tensor, targets: Tensor, input_lengths: Tensor | Sequence[int], target_lengths: Tensor | Sequence[int], blank: int = 0, reduction: Reduction = 'mean', zero_infinity: bool = False)Implementing kernel
C++ engine symbols that back this Python API.Connectionist Temporal Classification (CTC) loss.
The standard training objective for unaligned sequence prediction — used in speech recognition, handwriting recognition, and any task where the input sequence is much longer than the target and no per-frame alignment is provided. Introduced by Graves et al. 2006.
Internally marginalises over every valid alignment of a -frame prediction onto an -symbol target by inserting "blank" symbols and allowing each target symbol to span one or more frames, computing the negative log of the total path probability via dynamic programming.
Parameters
log_probsTensorlucid.nn.functional.log_softmax over the class
axis. is one unbatched sequence, with a
target, 0-d lengths, and a 0-d loss under "none".targetsTensorint32. Every label read must lie in , else
IndexError. This is stricter than the reference, which reads
past the class axis. The padding past a row's target length is
not read and may hold anything.input_lengthsTensorint32. Enables
padding-aware batching. Each must lie in , else
IndexError. A length of 0 raises NotImplementedError for
now.target_lengthsTensorint32. Each must lie in
for padded targets, and they must add up to the
number of concatenated targets. A length outside its range
raises IndexError; a count that does not match raises
ValueError.blankint= 00), in ,
else IndexError.reductionstr= 'mean'"mean" (default), "sum", or "none". Under
"mean", each sample's loss is divided by its target
length (at least 1) and the results are averaged across the
batch, as the reference framework defines it.zero_infinitybool= FalseTrue, infinite losses (which arise when a target
cannot fit in the available input frames) and their
gradients are set to zero, effectively skipping those
samples (default False).Returns
TensorScalar ("mean" / "sum") or per-sample tensor of
shape .
Notes
The CTC objective is the negative log of the total alignment probability:
where is the "many-to-one" alignment map
that collapses repeats and removes blanks. The forward and
backward recursions run in the log domain on the CPU; on metal
the inputs make a round trip. The gradient with respect to
log_probs follows the reference framework's formula, which is
the true gradient with respect to the logits once a
lucid.nn.functional.log_softmax sits in front — the way
the loss is meant to be fed. It is differentiable once.
Examples
>>> import lucid
>>> from lucid.nn.functional import ctc_loss, log_softmax
>>> # T=4 frames, N=1 batch, C=3 classes (blank=0)
>>> logits = lucid.randn(4, 1, 3)
>>> log_p = log_softmax(logits, dim=2)
>>> targets = lucid.tensor([[1, 2]], dtype=lucid.int32)
>>> il = lucid.tensor([4], dtype=lucid.int32)
>>> tl = lucid.tensor([2], dtype=lucid.int32)
>>> ctc_loss(log_p, targets, il, tl)
Tensor(...)