Sub topic 3
How Whisper Works: An Encoder-Decoder Transformer Trained on Weakly Supervised Data
This chapter assumes that you know the basic goal of automatic speech recognition and have seen audio represented as a waveform. It also assumes that you understand the basic idea of a neural network and have encountered tokens, attention, and transformers.
The overall pipeline
Whisper converts speech into text in several stages:
- It loads an audio waveform.
- It converts the waveform into a log-Mel spectrogram.
- An encoder reads the spectrogram and creates contextual audio representations.
- A decoder generates text tokens one at a time.
- Special tokens tell the model which language and task to use.
The model does not directly map individual sound samples to letters. It first creates a compact representation of the sound, then predicts a sequence of tokens from that representation.
The audio encoder
A spectrogram describes how the energy of different frequencies changes over time. Whisper uses a log-Mel spectrogram, which groups frequencies into Mel bands that roughly reflect how people perceive pitch. Taking the logarithm reduces the effect of very large energy differences.
The encoder applies convolutional layers and transformer layers to this spectrogram. Convolutional layers reduce the time-frequency input into useful local features. Transformer self-attention then lets each audio position use information from other positions. For example, a quiet sound may become easier to interpret when the surrounding speech is known.
The encoder output is not text. It is a sequence of vectors containing information about pronunciation, timing, background sound, and linguistic context.
The text decoder
The decoder is an autoregressive transformer. Autoregressive means that it generates one token, adds that token to its input, and predicts the next token. A token may be a word, part of a word, punctuation, or a special control symbol.
At each step, the decoder uses two kinds of attention:
- Masked self-attention lets it examine tokens that it has already generated, but not future tokens.
- Cross-attention lets it examine the encoder's audio representation.
This design lets the decoder combine language knowledge with evidence from the recording. It can use the earlier sentence to choose between words that sound similar.
During training, the decoder usually receives the correct previous tokens. During inference, it must rely on its own earlier predictions. This difference can cause errors to compound across a long passage.
Special tokens control the task
Whisper uses special tokens to represent metadata and actions. They can identify a language, select transcription or translation, and mark positions such as the beginning or end of a segment.
For transcription, the decoder is prompted with tokens that indicate the input language and the transcription task. For translation, the task token tells it to produce English even when the speech uses another language. These tokens are part of the sequence prediction problem rather than a separate classification system.
A common error is to treat language detection as infallible. The model estimates the language from the audio and its confidence can be wrong, especially with short clips, music, silence, or multilingual speech.
Weakly supervised training
Whisper was trained on a large collection of audio paired with text transcripts collected from the web. The data was weakly supervised because the transcripts were not all created or checked with the same careful labeling process. Some recordings contain noise, inaccurate text, unusual formatting, or incomplete alignment.
This scale gives Whisper broad coverage of accents, languages, topics, and recording conditions. However, noisy training data also teaches unwanted patterns. The model may repeat phrases, invent text during silence, or follow misleading context.
Common implementation errors
- Assuming the encoder directly outputs words. It outputs audio representations.
- Treating beam search as a guarantee of correctness. It only searches likely token sequences.
- Ignoring the selected language and task tokens.
- Comparing models with different audio segments or normalization rules.
- Interpreting a confident output as a calibrated probability of correctness.
Exercise
Why does Whisper need both an encoder and a decoder?
Show answer
The encoder converts the audio spectrogram into contextual representations, while the decoder uses those representations and previously generated tokens to produce the text sequence.