Skip to content
AI360XpertBeta

Encoder-Decoder ASR

The model hears the whole clip before it writes a single word. Then it spells out the transcript left to right, glancing back at the exact audio frames each word came from.

The encoder reads all four audio frames before a word is written, then cross-attention points the decoder at frame 3 with weight 0.387 to spell the next word
The encoder reads all four audio frames before a word is written, then cross-attention points the decoder at frame 3 with weight 0.387 to spell the next word

Why Does This Exist?

Sound arrives over time, but meaning often arrives late. If someone mumbles a name at the start of a sentence, you usually decode it from the words that follow. A recognizer that commits to each sound the instant it hears it can never do that. It's stuck with its first guess.

Frame-by-frame systems work exactly that way. They score each slice of audio on its own and merge the scores into text, the pipeline automatic speech recognition walks through. Treating frames as independent buys speed and live output, but it throws away the future. A mumbled word stays mumbled even when the next second of audio would have settled it.

Encoder-decoder ASR takes the opposite trade. An encoder reads the entire clip first and turns every frame into a vector that has seen what came before and after it. Only then does a decoder start writing, one token at a time, aiming back at the relevant frames through cross-attention. You pay with latency, because no word leaves before the clip ends, and you gain accuracy on anything ambiguous. The classic instance is Listen, Attend and Spell, and the same skeleton sits inside Whisper.

This page covers that skeleton: the two halves, the attention bridge between them, and one decoding step worked by hand. The frame pipeline it builds on lives in automatic speech recognition, and the search that picks the final transcript lives in beam search.

You need one prerequisite to follow along. Audio enters the model as frames: each frame covers a few tens of milliseconds of sound and is summarized as a vector of spectral energies, so a two-second clip becomes a sequence of about two hundred vectors. Everything the encoder and decoder do operates on that sequence, never on raw samples. The full pipeline from waveform to frames is in automatic speech recognition.

Think of It Like This

A voicemail you hear out before answering

A friend leaves you a rambling two-minute voicemail with three questions buried in it. You do not reply after the first ten seconds. You listen to the whole thing, then you answer point by point, replaying the fuzzy middle bit to check the name you half heard.

The full listen is the encoder. Answering point by point is the decoder. Hitting replay on one section while you compose your reply is cross-attention: your next sentence is guided by the part of the message it answers.

Where it stops: you actually understand your friend, so you can paraphrase, ask what they meant, or admit you could not hear them. The decoder understands nothing. It matches sound patterns to text patterns, it can't ask for clarification, and it delivers a wrong word with the same fluency as a right one.

How It Actually Works

Call the frame sequence X=(x1,…,xT)X = (x_1, \ldots, x_T), where each xtx_t is one spectral vector and TT is the number of frames. The encoder transforms it into H=(h1,…,hT)H = (h_1, \ldots, h_T), a sequence of dd-dimensional vectors where each hih_i has absorbed context from the whole clip. The decoder holds a state sts_t built from the tokens written so far, y1y_1 through yt−1y_{t-1}, and must choose the next token yty_t.

The encoder hears everything first

The encoder is typically a Transformer stack or a bidirectional recurrent network, often with a convolutional front end that subsamples the frames. Because each output vector is computed with access to past and future frames, h50h_{50} knows what happened at frame 10 and frame 90. That full context is the whole point: the representation of a mumbled sound carries the later sounds that disambiguate it.

Cross-attention aims before each word

To write token tt, the decoder forms a query qtq_t from its state sts_t, a summary of what it has written and what it expects next. It compares that query against every encoder vector, which serve as keys, and the comparison decides how much of each frame's content, serving as values, flows into the decision:

et,i=qt⋅hid,αt,i=exp⁡(et,i)∑jexp⁡(et,j),ct=∑iαt,ihie_{t,i} = \frac{q_t \cdot h_i}{\sqrt{d}}, \qquad \alpha_{t,i} = \frac{\exp(e_{t,i})}{\sum_j \exp(e_{t,j})}, \qquad c_t = \sum_i \alpha_{t,i} h_i

In plain words: et,ie_{t,i} is a match score between the query and frame ii, scaled by d\sqrt{d} (the vector width) so the numbers stay tame. The weights αt,i\alpha_{t,i} are those scores passed through a softmax, so they are positive and sum to 1 across frames. The context ctc_t is the weighted average the decoder actually reads. The next token distribution is p(yt∣y<t,X)=softmax(Wo[st;ct])p(y_t \mid y_{<t}, X) = \text{softmax}(W_o [s_t; c_t]), where WoW_o is a learned output matrix and [st;ct][s_t; c_t] just means the two vectors glued together.

Worked example: four frames, one query

Keep the width tiny, d=4d = 4, so d=2\sqrt{d} = 2. Suppose the encoder produced four frame vectors and the decoder query is q=[1,1,0,0]q = [1, 1, 0, 0]:

  • k1=[1,0,0,0]k_1 = [1, 0, 0, 0], k2=[0,1,0,0]k_2 = [0, 1, 0, 0], k3=[1,1,0,0]k_3 = [1, 1, 0, 0], k4=[0,0,0,0]k_4 = [0, 0, 0, 0] (a silent frame).

Dot products with qq give 11, 11, 22, 00. Divide by 22: scores s=[0.5,0.5,1.0,0.0]s = [0.5, 0.5, 1.0, 0.0]. Exponentiate: e0.5≈1.649e^{0.5} \approx 1.649 twice, e1.0≈2.718e^{1.0} \approx 2.718, e0=1.0e^{0} = 1.0, for a sum of about 7.0167.016. The weights are therefore

α=[0.235, 0.235, 0.387, 0.143]\alpha = [0.235,\ 0.235,\ 0.387,\ 0.143]

which sum to 1.01.0. Frame 3 wins with 0.3870.387 because it matches both active query entries. Taking values equal to keys in this toy, the context is c=0.235k1+0.235k2+0.387k3=[0.622,0.622,0,0]c = 0.235 k_1 + 0.235 k_2 + 0.387 k_3 = [0.622, 0.622, 0, 0]. The diagram above draws these exact numbers: four frames in, the winning weight on frame 3, and the context flowing down into the next word. The decoder then scores its vocabulary from [st;ct][s_t; c_t] and writes one token.

This design assumes the full utterance is available before writing starts, which is why it cannot stream. It also trains on gold-standard prefixes but tests on its own guesses, so one wrong word skews the query and the error compounds left to right. That's the price of the accuracy: use this shape when the whole clip exists and correctness matters, and reach for a streaming transducer when words must appear while the speaker is still talking. Two voices talking over each other break the one-transcript assumption entirely; splitting speakers first is the separate job of speaker diarization.

Code

The point of this snippet is the check, not the formula. It recomputes the worked example in plain Python so you can see the weights sum to 1 and the context land on [0.622,0.622,0,0][0.622, 0.622, 0, 0].

import math

def softmax(xs: list[float]) -> list[float]:    m = max(xs)  # shift by the max for safety; the result is unchanged    exps = [math.exp(x - m) for x in xs]    total = sum(exps)    return [e / total for e in exps]

scores: list[float] = [0.5, 0.5, 1.0, 0.0]weights = softmax(scores)print([round(w, 3) for w in weights])  # -> [0.235, 0.235, 0.387, 0.143]print(round(sum(weights), 3))  # -> 1.0
keys: list[list[float]] = [[1, 0, 0, 0], [0, 1, 0, 0], [1, 1, 0, 0], [0, 0, 0, 0]]context = [sum(w * k[d] for w, k in zip(weights, keys)) for d in range(4)]print([round(c, 3) for c in context])  # -> [0.622, 0.622, 0.0, 0.0]

Frame 3 carries the largest weight, so both active entries of the context come out near 0.6220.622 while the silent frame contributes nothing.

Watch Out For

Using it as a live captioner

You hook the model to a microphone and expect words to appear as the person speaks. Nothing shows up until the pause at the end of the sentence, because the encoder refuses to start until the clip is complete and the decoder refuses to start until the encoder is done.

That silence is the architecture, not a bug or a tuning knob. If your product needs partial results mid-sentence, this family is disqualified no matter how accurate it is. Switch to a streaming design such as a transducer with a bounded lookahead, or chunk the audio explicitly and accept the accuracy hit at chunk edges.

One wrong word derails the rest

Training feeds the decoder the true previous words, but at test time it feeds its own guesses. So the first mistake corrupts the query, the corrupted query aims attention at the wrong frames, and the transcript veers off and never recovers. The symptom is distinctive: a fluent transcript whose middle suddenly describes audio that was never spoken.

Don't try to fix this by retraining louder. Decode with a wider beam search so alternative prefixes survive one bad step, and check whether the derailment point coincides with noise or an accent the acoustic encoder never learned. A beam keeps candidates alive; it cannot rescue frames the encoder already misheard.

The Quick Version

  • An encoder-decoder ASR model reads the whole audio clip into context-rich vectors before its decoder writes a single token.
  • Cross-attention aims each output word at the frames it came from: match scores become weights that sum to 1, and the weighted average becomes the context.
  • Future context is the accuracy win: a frame's vector carries sounds that arrive after it, so later words can disambiguate earlier ones.
  • The price is latency plus compounding errors: nothing is emitted until the clip ends, and a wrong word skews every word after it.
  • Use it for transcribed recordings, captions prepared after the fact, and voice messages; never for live captioning, where a streaming design wins.