What architecture is BERT? Encoder-only Transformer
Grow through practice
No need to finish reading — test nowShort answer: BERT is an encoder-only Transformer. It uses only the Transformer encoder stack, with no decoder and no causal mask.
The confusion is understandable: the original Transformer is encoder-decoder, but BERT takes only the encoder half.
Three architecture families
| Architecture | Examples | Attention | Typical tasks |
|---|---|---|---|
| Encoder-only | BERT, RoBERTa | Bidirectional, no causal mask | Classification, extraction, embeddings, token labeling |
| Decoder-only | GPT, LLaMA | Causal mask, left context only | Autoregressive generation, chat |
| Encoder-decoder | T5, BART | Bidirectional encoder + causal decoder + cross-attention | Translation, summarization, seq2seq |
The name says it:
Bidirectional Encoder Representations from Transformers
- Bidirectional: every position can see the whole sequence.
- Encoder: only the encoder stack.
- Representations: learned representations, not direct generation.
What is inside BERT
BERT-base:
12 Transformer encoder layers
hidden size = 768
attention heads = 12
about 110M parameters
There is no decoder and no cross-attention. Each encoder layer is still:
self-attention → Add & Norm → FFN → Add & Norm
Encoder self-attention has no causal mask, so every token sees both left and right context. That is why BERT is good at understanding and not suited to left-to-right generation.
Pretraining: MLM + NSP
BERT is not pretrained with next-token prediction. It uses:
- MLM (Masked Language Model)
Mask 15% of tokens and predict them from context. Predicting masked tokens in the middle requires bidirectional understanding.
- NSP (Next Sentence Prediction)
Predict whether sentence B follows sentence A. Later work such as RoBERTa questioned NSP, but the original BERT used it.
Special tokens:
[CLS]: aggregate sequence representation, often used for classification;[SEP]: separates sentences;[MASK]: replaced token in MLM.
Why BERT cannot generate freely like GPT
BERT is trained with full context and has no “only look left” constraint.
Using it for autoregressive generation creates two problems:
- Training and inference attention conditions differ.
- Without a causal mask, the model can see future tokens.
So BERT is typically used for:
- sentence classification / sentiment;
- named entity recognition and token classification;
- semantic embeddings / retrieval;
- extractive question answering (predict start/end positions);
- as an encoder with a task head.
It is not the “give it a prompt and let it continue writing” model; that is decoder-only work such as GPT/LLaMA.
Relationship to GPT and T5
Original Transformer
├─ Encoder (bidirectional)
│ └─ BERT uses this half
├─ Decoder (causal + cross-attention)
│ └─ GPT uses the autoregressive decoder
└─ Encoder + Decoder
└─ T5 / BART use both
In one line:
BERT = Transformer encoder; GPT = Transformer decoder; T5 = encoder + decoder.
Common misconceptions
- “BERT is encoder-decoder.”
No. BERT has no decoder and no cross-attention.
- “BERT is bidirectional, so it can generate.”
Bidirectional understanding is not autoregressive generation. Generation needs a causal mask, which BERT lacks.
- “BERT can only classify.”
No. It also supports token labeling, extractive QA, sentence embeddings and retrieval; it just does not do free-form generation.
- “BERT is obsolete.”
Decoder-only models dominate generation, but BERT-style encoders remain common for classification, extraction, retrieval and low-latency embeddings.
Verification
- ✅ Architecture: BERT uses Transformer encoders (Devlin et al., 2019).
- ✅ Pretraining objectives: MLM + NSP, per the original paper.
- ✅ Comparison: GPT is decoder-only; T5/BART are encoder-decoder.
- ⚠️ Not measured: this article did not train or fine-tune BERT locally; the structural claims follow the paper and official implementation.
- ❓ Not covered: detailed differences between RoBERTa/ALBERT/DeBERTa.
One sentence
BERT is a read-only Transformer encoder: strong bidirectional understanding, no autoregressive generation.
Series: Understanding Attention and Transformers from Scratch Prerequisites: Attention as soft lookup · Multi-head and positional encoding · Where the FFN sits Test yourself: Transformer ranked quiz
Comments 0
No comments yet — be the first!