Encoder vs decoder: why BERT reads and GPT writes
This is the long version of a post on LinkedIn.
BERT and GPT look like opposites. One powers search ranking and spam filters, the other writes essays and code. Yet both are stacks of the same transformer block: attention, a small feed-forward network, shortcuts and normalisation. The difference comes down to one rule about which words each word is allowed to look at.
The shared starting point: the transformer
The original transformer from 2017 was built for translation and had two halves. The encoder read the whole source sentence and built a rich representation of it. The decoder then wrote the translation one token at a time, looking at what it had written so far and at the encoder's output.
A year later, two teams each kept one half and threw the other away:
- BERT (Google, 2018) kept the encoder stack.
- GPT (OpenAI, 2018) kept the decoder stack, minus the part that looks at an encoder.
Inside, the blocks are nearly identical. What differs is the attention mask.
One rule: who can see whom
In self-attention, every token computes a score against every other token and then mixes in information from the ones that score highest. A mask simply sets some of those scores to minus infinity before the softmax, so those links get zero weight.
Encoder (bidirectional). No mask. In "The bank by the river flooded", the word "bank" can look at "river" even though "river" comes later. That is exactly how you decide it means a riverbank and not a place that holds money.
Decoder (causal). A triangular mask. Token 4 may look at tokens 1 to 4 and nothing after. When the model is predicting the next word, the future does not exist yet, so allowing it to peek during training would be cheating.
Reading is half of it. Queries, keys and values shows this step by step in about 3 minutes. Sign up free to track what you’ve learned.
How each is trained follows from the mask
The mask decides what the training game can be.
BERT: fill in the blanks. Because every token can see the whole sentence, you cannot ask BERT to predict the next word; it would just look at it. Instead BERT hides about 15% of the tokens and learns to guess them from both sides: "The cat [MASK] on the mat" → "sat". This is called a masked language model. The original paper also added a second task, guessing whether two sentences followed each other, though later work (RoBERTa) found that dropping it did no harm.
GPT: predict the next token. With the causal mask, every position in a sentence becomes a training example at once: from "The" predict "cat", from "The cat" predict "sat", and so on. The model learns from every token in the text, and the same skill is what produces text later: predict a token, append it, repeat.
A useful side effect: masked-language training only learns from the hidden 15% of tokens in each pass, while next-token training learns from all of them. That is one reason decoder training scales so well to huge amounts of text.
What each is good at
| Encoder-only (BERT family) | Decoder-only (GPT family) | |
|---|---|---|
| Sees | the whole input at once | only earlier tokens |
| Trained to | fill in masked words | predict the next token |
| Natural output | a vector per token, or per sentence | the next token, repeated |
| Strong at | classification, tagging, search, embeddings | writing, chat, code, reasoning in text |
| Typical size in practice | small to medium | medium to very large |
Encoders are still everywhere, quietly. When a search box matches "money back" to a page about refunds, or a support tool routes a ticket to the right team, an encoder is often doing the work. They are small enough to run cheaply on every query, and the bidirectional view makes their sentence representations very good. Most embedding models used for vector search and RAG are encoder-style.
Decoders took over generation because writing is inherently left to right, and because next-token prediction turned out to teach a surprising amount: grammar, facts, style and multi-step patterns all help predict what comes next. Scale that up and fine-tune it to follow instructions, and you get a chat assistant.
The third option: encoder-decoder
The original two-half design never went away. T5 (Google, 2019) framed every task as "text in, text out" and kept both halves: the encoder reads the input with full bidirectional attention, and the decoder writes the output causally while also attending to the encoder. That suits tasks with a clear input and output, such as translation and summarisation, and models of this kind still power many translation systems.
So the family tree has three branches:
- Encoder-only: read and understand (BERT and its descendants).
- Decoder-only: read and continue (GPT-style models and most chat assistants).
- Encoder-decoder: read one thing, write another (the original transformer, T5).
Reading is half of it. The transformer block shows this step by step in about 3 minutes. Sign up free to track what you’ve learned.
Why decoder-only won the spotlight
If encoders understand text so well, why are almost all of today's large assistants decoder-only? A few reasons that the papers and practice point to:
- One model, any task. A decoder can answer, summarise, classify or translate just by being prompted, because everything can be phrased as "continue this text". An encoder needs a task-specific head on top.
- Training uses every token. As above, next-token prediction squeezes more learning out of each sentence.
- Generation is built in. Chat, code and step-by-step answers all need text output, and the causal mask is exactly what makes token-by-token generation possible.
- Simpler to scale. One stack, one objective, one mask. That simplicity matters when training runs cost millions.
None of this makes encoders obsolete. If you need to classify a million support tickets a day, embed a document collection, or rank search results fast, a small encoder is usually cheaper and just as accurate as asking a large decoder.
How to choose, in one minute
- Need a label, score or embedding for each input? Start with an encoder model.
- Need the model to write something? Use a decoder model.
- Have a fixed input to output mapping such as translation, and want a smaller specialised model? Consider an encoder-decoder.
The same blocks, three ways of wiring the mask, and very different jobs. Once you see the mask, the whole family makes sense.
Questions people ask
Is GPT an encoder or a decoder?
GPT is decoder-only. Each token can attend only to the tokens before it, which is what lets the model generate text one token at a time.
Can BERT generate text?
Not naturally. BERT is trained to fill in hidden words using context from both sides, so it has no built-in way to write a sentence left to right. It is used for understanding tasks such as classification, search and tagging.
Which should I use for search or classification?
Encoder models (BERT and its descendants) are usually smaller, faster and very strong for classification, embeddings and search. Decoder models are the choice when you need the model to write an answer.
See it move. Learn the ideas behind this post with animated lessons:
Start the lessonSources
- Vaswani et al. (2017), Attention Is All You Need
- Devlin et al. (2018), BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding
- Radford et al. (2018), Improving Language Understanding by Generative Pre-Training (GPT-1)
- Liu et al. (2019), RoBERTa: A Robustly Optimized BERT Pretraining Approach
- Raffel et al. (2019), Exploring the Limits of Transfer Learning with a Unified Text-to-Text Transformer (T5)