Search

Search papers and lessons

Connectionist Temporal Classification (CTC)

Level 4 · 13 min read

CTC trains a network to output a label sequence from an unaligned input sequence by summing the probability of every alignment that collapses to the right labels, computed efficiently with dynamic programming.

On this page

  • Colour = topic mix
  • Discrete math
  • Probability
  • Algorithms
  • Calculus
  • Losses & regularization
  • Neural network basics
  • ReadReady to readNot yet

TL;DR

Many learning problems pair a long input with a shorter output, such as audio frames with the letters of a sentence, but nobody marks which frame produces which letter. Connectionist temporal classification (CTC) is a loss function that lets a neural network learn from such pairs anyway. It adds up the probability of every way the network could line its outputs up with the target letters, and it computes that sum efficiently.

Builds on: Softmax, Dynamic Programming, Logarithm · Leads to: Automatic Speech Recognition

Where it's used

Speech recognition. A speech model reads audio that has tens of frames per second and must output the words spoken. The audio is not split into words or letters for it. Baidu's Deep Speech system (2014) trained an end-to-end recognizer with CTC, mapping a spectrogram straight to characters. CTC removed the need for a separately trained alignment step.

Handwriting and text-line recognition. A scanned line of handwriting is read left to right as a sequence of image columns. Each column gives a distribution over characters or a blank, and CTC turns the line's transcription into a training signal without anyone marking which column holds which letter.

Any task whose output is a monotone, shorter sequence of the input. Examples include reading a sequence of sensor readings into a sequence of events, or labelling a time series of gestures. The condition is that the output order follows the input order and the output is no longer than the input.

How it works

Take a toy version: the network reads 2 frames, and its vocabulary is the letters a and b plus a special blank symbol ∅. The blank means "no new letter here". For each frame, the network's Softmax gives a probability for each symbol. Here are the outputs, and they are the only thing the model sees:

FrameP(∅)P(a)P(b)
10.50.50
20.50.50

The target is the label sequence "a". The question is how likely the network is to produce "a" from these frames.

  1. Pick the outputs. Each frame emits one symbol. With 2 frames and 3 symbols there are 3 × 3 = 9 possible pairs, and each pair has a probability: the product of its two frame probabilities. For example, (∅, a) has probability 0.5 × 0.5 = 0.25.
  2. Collapse each pair. A pair is turned into a label sequence by two rules: merge consecutive repeats, then delete blanks. The pair (a, a) becomes a, then a: merged to a. The pair (∅, a) has no repeats and drops the blank, giving a. The pair (a, ∅) gives a. The pair (∅, ∅) gives nothing at all.
  3. Keep the pairs that give "a". Exactly three pairs collapse to "a": (∅, a), (a, ∅) and (a, a). Their probabilities are 0.25, 0.25 and 0.25, which are all the same here because each frame splits 0.5 and 0.5.
  4. Add them up. P("a") = 0.25 + 0.25 + 0.25 = 0.75. The network says "a" with probability 0.75, and the loss is −ln 0.75 ≈ 0.288. Training pushes this number up.
  5. Check. The nine probabilities sum to 1, and the three paths that give "a" are the only ones counted. Nothing else is needed.
flowchart LR
  A["Per-frame softmax<br/>outputs"] --> B["Every path of one symbol per frame"]
  B --> C["Collapse: merge repeats, drop blanks"]
  C --> D["Keep paths that give the target"]
  D --> E["Sum their probabilities"]
  E --> F["Loss = minus log of the sum"]

The pairs above are the whole idea. With 2 frames it is easy to list them. With 100 frames it is not, and that is why the next sections formalise the sum and make it fast.

The math

Definition

Definition: CTC loss

Let xx be the input of TT frames, and let pt(k∣x)p_t(k \mid x) be the softmax probability that frame tt emits symbol kk, where kk ranges over the vocabulary plus the blank ∅\varnothing. Let y=(y1,…,yU)y = (y_1, \ldots, y_U) be the target label sequence, with U≤TU \le T. The CTC loss is L(x,y)=−ln⁡P(y∣x),\mathcal{L}(x, y) = -\ln P(y \mid x), where P(y∣x)P(y \mid x) is the total probability of all paths that collapse to yy, defined in Step 3 below.

Step 1 in symbols: a path and its probability

A path π=(π1,…,πT)\pi = (\pi_1, \ldots, \pi_T) picks one symbol per frame. Frames are treated as independent given the input, so the probability of a path is a product: P(π∣x)=∏t=1Tpt(πt∣x).P(\pi \mid x) = \prod_{t=1}^{T} p_t(\pi_t \mid x). In the toy example, π=(∅,a)\pi = (\varnothing, a) has P=0.5×0.5=0.25P = 0.5 \times 0.5 = 0.25.

Step 2 in symbols: the collapse map

Let B\mathcal{B} be the collapse map: first merge consecutive identical symbols, then delete every ∅\varnothing. So B(a,a,∅,a,b,b)=(a,a,b)\mathcal{B}(a, a, \varnothing, a, b, b) = (a, a, b). The blank is what lets a letter appear twice in a row: B(a,∅,a)=(a,a)\mathcal{B}(a, \varnothing, a) = (a, a), whereas B(a,a)=(a)\mathcal{B}(a, a) = (a).

Step 3 in symbols: sum over alignments

Every path that collapses to the target is a valid alignment. The probability of the target is the sum over all of them: P(y∣x)=∑π : B(π)=y∏t=1Tpt(πt∣x).P(y \mid x) = \sum_{\pi \,:\, \mathcal{B}(\pi) = y} \prod_{t=1}^{T} p_t(\pi_t \mid x). The number of such paths grows exponentially with TT, so this formula cannot be evaluated directly for real audio. Step 4 is the fix.

Step 4 in symbols: the forward recursion

Write the target with blanks around every letter: l′=(∅,y1,∅,y2,…,∅,yU,∅)l' = (\varnothing, y_1, \varnothing, y_2, \ldots, \varnothing, y_U, \varnothing), of length S=2U+1S = 2U + 1. Let αt(s)\alpha_t(s) be the total probability of the length-tt prefixes of paths that are at position ss of l′l' at time tt. Start with α1(1)=p1(∅),α1(2)=p1(y1),α1(s)=0 for s>2,\alpha_1(1) = p_1(\varnothing), \qquad \alpha_1(2) = p_1(y_1), \qquad \alpha_1(s) = 0 \text{ for } s > 2, and then for t≥2t \ge 2: αt(s)=(αt−1(s)+αt−1(s−1)+αt−1(s−2)) pt(ls′),\alpha_t(s) = \Big(\alpha_{t-1}(s) + \alpha_{t-1}(s-1) + \alpha_{t-1}(s-2)\Big)\, p_t(l'_s), where the term αt−1(s−2)\alpha_{t-1}(s-2) is dropped when ls′=∅l'_s = \varnothing or when ls′=ls−2′l'_s = l'_{s-2}, and any term with an index below 1 counts as 0. The reason for the rule is that a letter may follow a different letter directly, or follow a blank, but a repeated letter must have a blank between. The answer is P(y∣x)=αT(S)+αT(S−1),P(y \mid x) = \alpha_T(S) + \alpha_T(S-1), since a valid path must end on the last letter or on the final blank.

Each step of the recursion does a constant amount of work per position, so the total cost is proportional to T⋅ST \cdot S. This is the dynamic-programming idea from dynamic programming: each partial sum is computed once and reused.

Step 5 in symbols: the loss and its gradient

The loss is L=−ln⁡P(y∣x)\mathcal{L} = -\ln P(y \mid x). A matching backward recursion βt(s)\beta_t(s), the mirror image of α\alpha, gives the derivative of the loss with respect to each frame's output. Training then uses ordinary maximum-likelihood style gradient descent through the network. The Logarithm is there because it turns the product of many small probabilities into a sum of logs and keeps numbers from underflowing.

0.20.40.60.8100.511.522.53P(y given x)loss
  • loss = -ln P
The loss rises steeply as the probability of the right labelling falls. The marked points are the three cases worked in the examples.

Worked examples

Example
1. Tiny: two frames, target b

Frame probabilities: frame 1 gives P(∅) = 0.6 and P(b) = 0.4; frame 2 gives P(∅) = 0.3 and P(b) = 0.7.

  1. List the paths that collapse to "b". They are (∅, b), (b, ∅) and (b, b). The path (∅, ∅) gives nothing, so it is excluded.
  2. Probability of (∅, b): 0.6 × 0.7 = 0.42.
  3. Probability of (b, ∅): 0.4 × 0.3 = 0.12.
  4. Probability of (b, b): 0.4 × 0.7 = 0.28. The two b's merge into one, so this path also gives "b".
  5. Add them: 0.42 + 0.12 + 0.28 = 0.82.
  6. Loss: −ln 0.82 ≈ 0.198. The network is fairly confident in "b" and the loss is small.
Example
2. Standard: three frames, target ab, with the forward recursion

Frame probabilities over (∅, a, b): frame 1 gives 0.4, 0.4, 0.2; frame 2 gives 0.2, 0.5, 0.3; frame 3 gives 0.3, 0.3, 0.4. The target is "ab", so U=2U = 2 and the extended label is l′=(∅,a,∅,b,∅)l' = (\varnothing, a, \varnothing, b, \varnothing), with S=5S = 5.

  1. Start: α1=(0.4, 0.4, 0, 0, 0)\alpha_1 = (0.4,\ 0.4,\ 0,\ 0,\ 0). Only the blank at position 1 and the letter a at position 2 are possible in the first frame.
  2. Frame 2, position 1 (blank): α2(1)=0.4×0.2=0.08\alpha_2(1) = 0.4 \times 0.2 = 0.08.
  3. Frame 2, position 2 (a): the sum is 0.4+0.4=0.80.4 + 0.4 = 0.8, times 0.50.5, giving α2(2)=0.40\alpha_2(2) = 0.40.
  4. Frame 2, position 3 (blank): the sum is 0+0.4=0.40 + 0.4 = 0.4, times 0.20.2, giving α2(3)=0.08\alpha_2(3) = 0.08.
  5. Frame 2, position 4 (b): the three terms are α1(4)+α1(3)+α1(2)=0+0+0.4\alpha_1(4) + \alpha_1(3) + \alpha_1(2) = 0 + 0 + 0.4. The term from two positions back is kept because position 2 holds a, which differs from b. The sum is 0.40.4, times 0.30.3, giving α2(4)=0.12\alpha_2(4) = 0.12.
  6. Frame 2, position 5 (blank): the sum is α1(5)+α1(4)=0\alpha_1(5) + \alpha_1(4) = 0, so α2(5)=0\alpha_2(5) = 0. A path cannot reach the final blank after only one frame.
  7. Frame 3, position 1: 0.08×0.3=0.0240.08 \times 0.3 = 0.024. Position 2: (0.40+0.08)×0.3=0.144(0.40 + 0.08) \times 0.3 = 0.144. Position 3: (0.08+0.40)×0.3=0.144(0.08 + 0.40) \times 0.3 = 0.144. Position 4: (0.12+0.08+0.40)×0.4=0.240(0.12 + 0.08 + 0.40) \times 0.4 = 0.240. Position 5: (0+0.12)×0.3=0.036(0 + 0.12) \times 0.3 = 0.036.
  8. Answer: P(ab∣x)=α3(5)+α3(4)=0.036+0.240=0.276P(ab \mid x) = \alpha_3(5) + \alpha_3(4) = 0.036 + 0.240 = 0.276.
  9. Check by brute force: the five paths that collapse to ab are (a, a, b), (a, ∅, b), (∅, a, b), (a, b, b) and (a, b, ∅). Their probabilities are 0.08, 0.032, 0.08, 0.048 and 0.036, which add to 0.276. The recursion and the enumeration agree.
  10. Loss: −ln 0.276 ≈ 1.287.
Example
3. Realistic: reading a greedy decode of a speech model

A speech model is run on an 8-frame window of audio. In each frame it gives its most likely symbol, and the sequence of those is h, h, ∅, e, l, ∅, l, o. This is the best single path, which is what a fast "greedy" decoder uses.

  1. Merge consecutive repeats: h, h becomes h. The result is h, ∅, e, l, ∅, l, o.
  2. Delete the blanks: h, e, l, l, o.
  3. Read the letters: "hello".

The blank in frame 6 is what keeps the two l's apart. Without it the merge in step 1 would have turned them into one l and produced "helo". Greedy decoding gives a good answer, but it returns the single most likely path, not the most probable label sequence. The full CTC sum in Step 3 would add in every other path that also reads "hello".

Try it: the frames read, in order, l, l, l, ∅, l, o, o, ∅. What text does this give under the two collapse rules? Answer: merge repeats to get l, ∅, l, o, ∅; delete the blanks to get l, l, o, which is "llo". The blank in position 4 is what keeps the second run of l's from merging into the first.

Common mistakes and tradeoffs

It is tempting to think the blank is a space between words. In fact the blank is a "nothing new here" symbol. It separates repeated letters and lets the network hold a frame without committing to a letter, because every frame must emit something.

It is tempting to think CTC tells you which frame produces which letter. In fact it does not pick an alignment. It sums over all of them, and the probability it gives is for the whole labelling. Greedy decoding recovers one good alignment, but that is a decoding choice, not part of the loss.

It is tempting to think any output length works. In fact a CTC model can only emit one symbol per frame, so the target needs at least as many frames as letters, plus one extra frame for every letter that repeats. A transcript longer than the audio is impossible to produce.

It is tempting to think CTC models the dependence between output letters. In fact each frame's output is treated as independent given the input. The model cannot learn that "q" is usually followed by "u" beyond what each frame's features already capture, which is why sequence-to-sequence models with attention are often used when outputs depend strongly on each other or when the output can be longer than the input.

When to use CTC: the input and output are both sequences in the same order, the output is no longer than the input, and you want fast training and decoding with no alignment labels. When not to use it: translation, where the output order and length differ from the input, or any task where a language model's knowledge of earlier outputs matters a lot. Alternatives include attention-based encoder-decoders and RNN-transducers, which relax the independence assumption. A bidirectional recurrent network is the usual encoder under CTC, so each frame's output can use context from both earlier and later audio.

History

Before CTC, training a sequence model on unaligned data needed a separate alignment step: a hidden Markov model or a hand-built segmentation told the network which frame belonged to which label, and the network was then trained frame by frame. The two stages were trained separately and could disagree.

In 2006 Alex Graves and colleagues introduced CTC at ICML, for labelling unsegmented sequences such as speech and handwriting, using recurrent networks. Its key contribution was the sum over alignments computed by a forward-backward style recursion, which removed the need for a separate alignment.

CTC caught on in speech recognition in the 2010s, when recurrent and then deep networks became strong enough to predict characters directly from audio. Deep Speech in 2014 showed a simple end-to-end CTC system could compete with the older pipelines, and this made the approach widely copied.

Recap

  • The network outputs one softmax distribution per frame, including a blank symbol.
  • A path picks one symbol per frame. Its probability is the product of the per-frame probabilities.
  • The collapse map merges repeats and deletes blanks, so many paths give the same label sequence.
  • The probability of a label sequence is the sum over all paths that collapse to it.
  • A forward recursion over the blank-interleaved label computes that sum in time proportional to frames times label length.
  • The loss is the negative log of that probability. Next read Automatic Speech Recognition, which uses CTC as one of its training losses.

Check yourself

QuestionWhat does the collapse map do to the path (a, a, ∅, a, b, b, ∅)?Show answer

First merge consecutive repeats: a, ∅, a, b, ∅. Then delete the blanks: a, a, b. The result is (a, a, b). The blank between the two a's is what keeps them as two letters rather than one.

QuestionWith two frames, P(∅) = 0.5 and P(a) = 0.5 in frame 1, and P(∅) = 0.2 and P(a) = 0.8 in frame 2, what is P(a)?Show answer

The paths that collapse to a are (∅, a), (a, ∅) and (a, a). Their probabilities are 0.5 × 0.8 = 0.4, 0.5 × 0.2 = 0.1 and 0.5 × 0.8 = 0.4. Adding them gives 0.9.

QuestionWhy does the target ll need a blank between its two l's, but the target lo does not?Show answer

A repeated symbol across consecutive frames is merged into one letter by the collapse map. So two separate l's need a blank between them, otherwise the path (l, l) collapses to a single l. Between l and o there is no repeat, so no blank is needed.

QuestionA speech team wants CTC to output a 12-word transcript from 8 frames of audio. Why will this not work?Show answer

Each frame emits at most one symbol, and a collapse can only shorten a path. Eight frames can give at most eight output symbols. CTC outputs characters, and a 12-word transcript has at least 12 characters, so the transcript cannot be produced at all. The team needs more frames, or a model such as an attention-based encoder-decoder whose output length is not tied to the input length.

Sources