Foundations to Frontier
Tests
E. Deep Learning › E2 Loss Functions (one page each)

CTC loss

Connectionist temporal classification (CTC) trains a sequence model to output a label sequence, such as a transcript, from a longer input sequence of frames without knowing which frames belong to which label. It adds a blank symbol, defines a many-to-one collapse from frame-level paths to labels, and maximises the total probability of all paths that collapse to the correct label, computed efficiently by a forward–backward recursion. This page covers the blank and collapse rule, the forward algorithm, why repeated labels need blanks, the gradient, decoding and the peaky behaviour of trained models. In the exercises you check the forward algorithm against brute force, then train with CTC and with framewise cross-entropy on the true alignment and find that CTC, without alignment, halved the greedy-decoding error rate and learned peaky outputs.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Training without knowing where each letter is

Speech arrives as hundreds of short audio frames, but the transcript has only a few letters, and nobody marked which frames make which letter. CTC handles this. The network outputs, for every frame, probabilities over letters plus a special “blank”. Any frame-by-frame path that becomes the transcript after merging repeats and dropping blanks counts as correct. The loss adds up the probability of all such paths, and a clever recursion makes that sum cheap.

CTC: summing over every alignment of a label to the frames
∅A∅B∅B∅A∅
P(label | input) = 1.635e-1 · CTC loss −log P = 1.811 · valid alignments 1,287 · greedy decode “ABBA”
Rows: the label with blanks (∅) inserted before, between and after its symbols. A valid alignment is a path from the top-left (∅ or first symbol) to the bottom-right (last symbol or final ∅) that moves right one frame at a time and stays, steps down one row, or skips a blank between two different symbols. Collapsing a path (merge repeats, then drop blanks) gives the label. For repeated letters (AA, ABBA) the path must pass through a blank between them, so the label needs more frames: with too few frames there is no valid alignment and the loss is infinite. Raise the confidence to watch the probability concentrate and the loss fall.
Why engineers care

CTC powers speech recognition for cockpit and command interfaces, handwriting and text recognition on documents and placards, and sequence labelling of sensor streams (gesture, activity, manoeuvre) where frame-level labels are too expensive to produce. It needs only the sequence of labels, not their timing.

📝 Quick check (Level 0)
0/3 answered
  1. 1. CTC needs…
  2. 2. The blank symbol lets the model…
  3. 3. The CTC loss sums the probability of…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

By hand

  • Collapse rule: merge consecutive repeats, then remove blanks. With ∅ the blank, “AA∅AB∅B” first becomes A ∅ A B ∅ B, then AABB. Likewise “A∅A” → AA, but “AA” → A.
  • Repeated labels need a blank: “AA” requires at least 3 frames (A ∅ A). In general, the minimum number of frames is the label length plus one for each pair of equal neighbours.
  • Extended label: ∅ l₁ ∅ l₂ … ∅ has 2L + 1 symbols. Valid paths through the trellis move one frame right at a time and stay, step down one row, or skip the blank between two different labels.
  • Counting: label AB over 3 frames has 5 alignments (∅AB, A∅B, AB∅, AAB, ABB); AA over 3 frames has exactly 1 (A∅A).
  • Loss: L = −log Σ over paths π with collapse(π) = y of Π_t P(π_t at frame t).
  • Greedy decoding: take the most likely symbol per frame, then collapse. Beam search with a language model does better.
📝 Quick check (Level 1)
0/3 answered
  1. 1. What does 'A∅A' collapse to?
  2. 2. Minimum frames for the label 'ABB'?
  3. 3. Alignments of 'AB' over 3 frames?

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

The forward algorithm

Exercise 1.

  • Collapse and blank-extension behave as stated.
  • On random frame probabilities, the forward algorithm’s P(label) matches a brute-force sum over every path, for several labels.
  • Too few frames gives probability exactly 0 (an infinite loss).

The recursion. α(t, s) is the probability of all valid partial paths ending at extended-label position s at frame t:

α(t, s) = (α(t−1, s) + α(t−1, s−1) + [skip allowed]·α(t−1, s−2))·y(t, ext[s]).

The skip is allowed only from a non-blank symbol to a different non-blank symbol. P(y) = α(T, last) + α(T, second-to-last). A matching backward recursion β gives the gradient, and the work is O(T·L), not exponential.

Practice.

  • Work in log space or with per-frame scaling; products of many probabilities underflow.
  • Inputs must have at least as many frames as the minimum for the label; frameworks return inf otherwise (use zero_infinity=True).
  • The blank is usually class 0.
  • The model’s output frame rate must be high enough for the densest labels.
F.ctc_loss(log_probs.transpose(0, 1), targets, input_lengths, target_lengths, blank=0, zero_infinity=True)
🐍 Exercise 1: collapse, forward algorithm and brute force
Implement the collapse rule, blank extension, the CTC forward algorithm, a brute-force sum over all paths for checking, and the minimum number of frames.
📝 Quick check (Level 2)
0/3 answered
  1. 1. The forward algorithm's cost is…
  2. 2. Why work in log space?
  3. 3. zero_infinity=True in PyTorch…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

Marginal likelihood. CTC defines P(y | x) = Σ over paths π with B(π) = y of Π_t P(π_t | x), where B is the collapse map. Each frame’s output is conditionally independent given the input. The loss is the negative log of this marginal, maximum likelihood with the alignment as a latent variable, exactly like an HMM, and the forward–backward algorithm is the same.

Gradient. With per-frame logits z_t and softmax outputs y_t:

∂L/∂z(t, k) = y(t, k) − (1/P) × [sum over positions s with ext[s] = k of α(t, s)β(t, s)/y(t, k)].

That is the model’s probability minus the posterior probability that frame t is aligned to symbol k. It is a framewise cross-entropy against soft, automatically inferred targets.

Peaky behaviour. Trained CTC models typically emit each label in a single spike and blank elsewhere, even when the label spans many frames. Blank is a valid choice at every frame of every path, so probability concentrates there. This is harmless for transcription, but CTC outputs are poor frame-level segmentations.

Limitations. The conditional-independence assumption means CTC cannot model dependencies between output labels; a language model or an attention or transducer model (RNN-T) is needed for that. Outputs cannot be longer than the inputs.

📝 Quick check (Level 3)
0/3 answered
  1. 1. CTC treats the alignment as…
  2. 2. CTC outputs are typically…
  3. 3. CTC assumes outputs at different frames are…

Level 4: Expert

★★★★★ Expert⏱ 25 minResearchers: original papers and open problems.

Alignment-free versus aligned training

History.

  • A. Graves, S. Fernández, F. Gomez and J. Schmidhuber (2006) introduced CTC.
  • A. Graves and N. Jaitly (2014) and Baidu’s Deep Speech (A. Hannun et al., 2014) built end-to-end speech recognisers on it.
  • A. Graves (2012) introduced the RNN transducer, removing the independence assumption.
  • A. Zeyer et al. (2021) analysed why CTC outputs become peaky.

Exercise 2. Sequences of 4 symbols from three classes, each held 3–5 frames, padded to 24 frames with silence. The per-frame inputs are noisy one-hot codes (noise 0.5). A per-frame linear softmax over the current and neighbouring frames is trained on 64 sequences either with CTC (labels only, no timing) or with framewise cross-entropy on the true alignment (silence frames as blank). Both are decoded greedily on 200 test sequences.

training label error rate frames predicted blank blank on frames where a symbol is present
CTC (no alignment) 0.133, 0.147, 0.170 0.582, 0.579, 0.590 0.388, 0.387, 0.396
framewise CE (true alignment) 0.273, 0.263, 0.294 0.331, 0.352, 0.338 0.084, 0.102, 0.091
  • CTC needed no alignment and still did better. Its error rate was about half that of framewise training with perfect timing. The framewise model flickered between symbols within a segment, and greedy collapse turned each flicker into an extra symbol. CTC learned to output blanks around its decisions, which suppresses those insertions.
  • CTC became peaky. It predicted blank on almost 40% of the frames where a symbol was actually present: it marks each symbol with a short spike rather than its whole duration.
  • Caveat: with smoothing or an HMM decoder the framewise model would improve. The comparison shows that CTC builds this robustness into training itself, and that its frame-level outputs are not a segmentation.
🐍 Exercise 2 (research): alignment-free versus aligned training
Train a per-frame classifier on noisy symbol sequences with CTC (labels only) and with framewise cross-entropy on the true alignment; compare greedy-decoding label error rates and how often each predicts blank.

CTC in AI, robotics and aerospace

  • Speech interfaces: command recognition for pilots, operators and robots uses CTC-trained acoustic models, often fused with a domain-specific language model.
  • Text recognition: reading serial numbers, placards and gauges from camera images (CRNN + CTC).
  • Sensor sequences: labelling manoeuvres or gestures in IMU streams when only the order of events is known, not their exact timing.

References

  1. A. Graves, S. Fernández, F. Gomez, J. Schmidhuber (2006). “Connectionist temporal classification: labelling unsegmented sequence data with recurrent neural networks.” Proc. ICML.
  2. A. Graves (2012). “Sequence transduction with recurrent neural networks.” arXiv:1211.3711.
  3. A. Hannun et al. (2014). “Deep Speech: scaling up end-to-end speech recognition.” arXiv:1412.5567.
  4. A. Hannun (2017). “Sequence modeling with CTC.” Distill.
  5. A. Zeyer, R. Schlüter, H. Ney (2021). “Why does CTC result in peaky behavior?” arXiv:2105.14849.