---
title: Greedy CTC decoding
url: https://gurkanozkan.ai/learn/coding/mle-greedy-ctc-decoding-24/
markdown: https://gurkanozkan.ai/learn/coding/mle-greedy-ctc-decoding-24.md
kind: coding
difficulty: Easy
topics:
  - Speech
  - CTC
  - Decoding
json: https://gurkanozkan.ai/api/learn/coding/mle-greedy-ctc-decoding-24.json
robots: noindex
---

# Greedy CTC decoding

`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

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

## Examples

Each call and the value it should give.

```python
>>> 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
```

## Hints

1. Argmax per frame (ties -> lowest index), collapse consecutive repeats, then remove blanks.
2. The collapse step is what makes blanks separate double letters — removing blanks first is wrong.
3. Return the collapsed alphabet string.

## Reference solution

```python
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.*
