Skip to content

CTC: learning to transcribe without frame labels

Speech gives about a hundred frames a second and nobody marks which frame says which letter. CTC adds a blank and sums over every alignment. Compute it by hand, and meet Deep Speech 2, a recognizer trained with it.

Applied ~14 min8 steps

What runs in CodeSplash

You will be able to

  • Explain the alignment problem CTC solves and the role of the blank
  • Apply the collapse rule to frame-level paths
  • Compute a CTC loss by listing paths, in Python and R
  • Describe Deep Speech 2 and the conditional independence CTC assumes

Read first

01

Frames outnumber letters

Read

An encoder turns audio into about a hundred frames a second, each with a probability for every symbol. The transcript "cat" has three letters and no timing. Which frames say "c"? Nobody labeled that, and hand-aligning thousands of hours isn't possible.

Connectionist temporal classificationConnectionist temporal classification (CTC)A loss that sums over every alignment of a label sequence to audio frames, using a blank symbol and merged repeats. (CTC) trains without the alignment. It adds one extra symbol, the blankCTC blankCTC's extra no-label symbol. It fills frames between labels and separates repeats, so l, blank, l reads as ll., meaning "no new letter here", and lets the model choose a symbol, or blank, in every frame. Any frame-level choice is a path.

Reference · AUD001 · Explained hereConnectionist temporal classification (CTC)Learns an unsegmented input-to-label alignment from paired speech and transcripts. It is a supervised sequence objective, not a language model or an unsupervised test.
02

The collapse rule

Read

A path turns into text by a fixed rule: merge neighboring repeats, then drop the blanks.

  • c c ∅ a a t → c a t
  • c ∅ a ∅ t t → c a t
  • l ∅ l → l l, but l l → l

Many paths read the same text. CTC's probability for a transcript adds up all of them:

p(y∣x)=∑π: B(π)=y  ∏t=1Tpt(πt∣x),L=−log⁡p(y∣x),p(y \mid x) = \sum_{\pi:\,B(\pi) = y} \; \prod_{t=1}^{T} p_t(\pi_t \mid x), \qquad \mathcal{L} = -\log p(y \mid x),

where BB is the collapse rule. Training raises the total, however it is split among paths.

DExplorable · CTC: blanks, repeats and the forward table

How does CTC score a transcript without knowing which frame holds which letter?

What is the fewest number of frames that can spell this transcript?

6
How sure?

Transcript

The text the frames must spell. Doubled letters (aa, bb, oo) need a blank between them.

8

How many frames the audio gives. CTC emits one symbol (a letter or the blank) per frame.

0.80

Probability the toy acoustic model gives the symbol it favors in each frame.

0.5

Random jitter added to the model's scores before the softmax.

Skip rule

CTC may skip a blank only between two different letters. The wrong rule also skips it between equal letters.

.13.63.82.90.13.86.10.90.81.16.10.03.77.05.09.05.05.20.08.07.10.08.80.06p(∅)p(a)p(b)1. ∅2. a3. ∅4. a5. ∅6. b7. ∅

Likeliest single path: a ∅ ∅ ∅ a ∅ b ∅. Greedy reading (top symbol per frame, collapsed): “aab”. Probabilities in each column of the lower table add up to 1.

Commit your prediction above to see the result

Make your prediction first; the figure appears when you commit.

Make your prediction to see the result.

03

Count the paths

Read

Three frames, three symbols (blank, a, b), target "ab". The reference's example lists all 27 paths and keeps those that collapse to "ab":

Python
import itertools
import numpy as np
P = np.array([[.1, .7, .2], [.2, .2, .6], [.6, .2, .2]])  # 3 frames x (blank, a, b)

def collapse(path):                       # merge repeats, then drop blanks (0)
    merged = [v for i, v in enumerate(path) if i == 0 or v != path[i - 1]]
    return tuple(v for v in merged if v != 0)

paths = [p for p in itertools.product(range(3), repeat=3) if collapse(p) == (1, 2)]
prob = sum(np.prod([P[t, j] for t, j in enumerate(p)]) for p in paths)
print(len(paths), -np.log(prob))          # paths that read "ab", CTC loss
R
P <- matrix(c(.1, .7, .2, .2, .2, .6, .6, .2, .2), nrow = 3, byrow = TRUE)  # blank, a, b
collapse <- function(path) {              # merge repeats, then drop blanks (1)
  merged <- path[c(TRUE, diff(path) != 0)]
  merged[merged != 1]
}
paths <- as.matrix(expand.grid(1:3, 1:3, 1:3))
keep <- apply(paths, 1, function(p) identical(as.integer(collapse(p)), c(2L, 3L)))
prob <- sum(apply(paths[keep, , drop = FALSE], 1, function(p) prod(P[cbind(1:3, p)])))
print(c(sum(keep), -log(prob)))           # paths that read "ab", CTC loss

Five paths qualify, and the loss is 0.926. Listing paths grows exponentially with length; real systems use the forward recursion of the next lesson.

04

Reading the output

Read

At inference the simplest reading is greedy decodingGreedy CTC decodingTake the likeliest symbol in each frame, merge repeats, drop blanks. Fast, but not always the likeliest transcript.: take the likeliest symbol in each frame, then collapse. It is fast, but the likeliest single path isn't always the likeliest transcript, since a transcript's probability is spread over many paths. Beam search keeps several candidates and can add a language model's score.

CTC makes one strong assumption: given the audio, each frame's choice is independent of the others. The model can't learn spelling or grammar from its own earlier outputs, so CTC recognizers lean on a strong encoder or an outside language model.

05

Deep Speech 2

Read

Deep Speech 2 showed how far CTC goes with scale: convolutions over the spectrogram, a stack of recurrent layers, a CTC head, and many thousands of hours of transcribed speech, with careful batching and a beam search that adds an n-gram language model. Its objective on the toy above is the same loss, 0.926: the architecture and the data changed, not the objective.

Reference · AUD004 · Explained hereDeep Speech 2A supervised end-to-end recognizer using convolutional acoustic processing and recurrent layers trained with CTC. Its contribution is a scalable speech pipeline rather than a separate hypothesis test;

Deep Speech 2 is a milestone, not a model to run today: newer encoders replaced it, and no maintained official checkpoint exists.

06

Where it stands in CodeSplash

Read
EntryIn CodeSplash
CTC (AUD001)Explained here. Speech to text in CodeSplash uses Whisper, not CTC
Deep Speech 2 (AUD004)Explained here; superseded, with no maintained official checkpoint

Both reference programs are component checks, verified in Python and R: a loss on a fixed toy table, not a trained model.

07

Check: CTC

Checkpoint

Check yourself

Which frame path reads as "hello" under the CTC collapse rule (∅ is the blank)?

Show the answer

Answer: b

Repeats merge unless a blank separates them. l ∅ l keeps both letters; l l collapses to one.

Check yourself

What does the CTC loss add up?

Show the answer

Answer: b

CTC marginalizes over alignments: every path that reads as the target counts, so no frame labels are needed.

Check yourself

Why do CTC recognizers often add an outside language model at decoding time?

Show the answer

Answer: b

The conditional independence assumption keeps CTC simple and fast, but spelling and word order must come from the encoder or an added language model.

Check yourself

What did Deep Speech 2 change compared with a small CTC recognizer?

Show the answer

Answer: b

Deep Speech 2's contribution was a scalable pipeline. The loss it trains is plain CTC.

Check yourself

A colleague wants to use Deep Speech 2 for a new transcription project. What's the honest advice?

Show the answer

Answer: b

Learn from it, then pick a maintained model. CTC lives on inside newer encoders and aligners.

08

Recap

Recap
  • CTC adds a blank and sums over every frame path that collapses to the transcript.
  • Collapse: merge repeats, then drop blanks; a blank keeps doubled letters.
  • Greedy decoding is fast; beam search with a language model reads better.
  • Deep Speech 2 scaled CTC with data; it is explained here, not run.

Concepts in this lesson