LearnCoding

Greedy CTC decoding

EasySpeech, CTC, Decoding

Problem

frame_probs is a (T, V) matrix of per-frame probabilities over alphabet (index blank is the CTC blank). Take the argmax per frame (ties go to the lowest index), collapse consecutive repeats, then remove blanks, and return the resulting string.

Signature

def ctc_greedy_decode(frame_probs, alphabet: list[str], blank: int = 0) -> str: ...

Examples

The visible tests, as an interactive Python session would show them: each call and the value it should give.

>>> ctc_greedy_decode([[0.1,0.9,0.0],[0.1,0.9,0.0],[0.9,0.1,0.0],[0.1,0.9,0.0]], ['_','a','b']) == 'aa'
True

>>> ctc_greedy_decode([[0.1,0.9,0.0],[0.1,0.9,0.0],[0.1,0.0,0.9]], ['_','a','b']) == 'ab'
True

Solve it in your own editor; nothing on this page runs code.

Hints

Argmax per frame (ties -> lowest index), collapse consecutive repeats, then remove blanks.

The collapse step is what makes blanks separate double letters — removing blanks first is wrong.

Return the collapsed alphabet string.

Reference solution

import numpy as np

def ctc_greedy_decode(frame_probs, alphabet, blank=0):
    best = [int(np.argmax(row)) for row in frame_probs]
    out = []; prev = None
    for b in best:
        if b != prev and b != blank:
            out.append(alphabet[b])
        prev = b
    return "".join(out)

What a strong solution covers

  • Collapses before removing blanks, which is why blanks separate double letters.
  • Linear time.
  • Discusses beam search with prefix merging, LM fusion, and streaming emission.

Common mistakes

  • removing blanks first (merges a _ a into one a)
  • collapsing non-adjacent repeats.

Study material compiled with AI: drafted by a local model (Inkling 2 Mini) from public sources, then checked against those sources by independent AI reviewers (Claude) in November 2026. Not reviewed line by line by a person.