BERT vs GPT: Encoder vs Decoder

Two kinds of Transformer: one built to understand text, one built to write it.

🟡 intermediate5 min readnlpmust-know
BERT and GPT are both Transformers, built for different jobs. BERT reads a whole sentence at once, looking both left and right, and is trained by filling in hidden words. That makes it strong at understanding text: classifying it, searching it, or finding answers inside it. GPT reads strictly left to right and is trained to predict the next word, which makes it strong at writing new text. Most chatbots today are GPT-style, while search systems often still use BERT-style models.

The Great Architectural Divide

The 2017 Transformer paper introduced both an Encoder and a Decoder.

Researchers quickly discovered that splitting the architecture into specialized halves yielded powerful model families:

  BERT (Encoder Only)                               GPT (Decoder Only)
  Bidirectional Context (Past + Future)             Causal Left-to-Right Context (Past Only)
  [ Word 1 ] ◄───► [ Word 2 ] ◄───► [ Word 3 ]      [ Word 1 ] ──► [ Word 2 ] ──► [ Word 3 ]
  Best for: Understanding, Search, NER             Best for: Text Generation, Chat, Code

1. BERT (Bidirectional Encoder Representations from Transformers)

Developed by Google in 2018.

Uses Bidirectional Self Attention where every token can attend to all other tokens in the sentence simultaneously (both left and right context).

Pretraining Objective: Masked Language Modeling (MLM)

Randomly masks 15 percent of input tokens with a [MASK] token, training the model to fill in the blanks:

Input: "The cat sat on the [MASK] because it was tired."

Output Target: "mat"

2. GPT (Generative Pretrained Transformer)

Developed by OpenAI in 2018.

Uses Causal Masked Self Attention where token tt can only attend to past tokens 1…t1 \dots t.

Pretraining Objective: Next Token Prediction (Causal LM)

Predicts the next token given preceding text:

Input: "The cat sat on the"

Output Target: "mat"

Comparison Summary Table

  1. BERT: Encoder Only. Bidirectional context. Pretraining task is Masked LM. Best for Search, Classification, NER.
  2. GPT: Decoder Only. Unidirectional Causal context. Pretraining task is Next Token Prediction. Best for Generation, Chat, Reasoning.
  3. T5: Encoder Decoder. Full seq2seq context. Pretraining task is Span Corruption. Best for Translation, Summarization.

Why Decoder Only Architecture Won

In 2018, researchers assumed Encoders were better for understanding and Decoders were only for generation.

By 2024, Decoder Only models (LLaMA 3, GPT 4, Claude 3) proved that scaling Next Token Prediction on Causal Decoders unlocks both deep understanding AND generation in a single unified architecture!

Say this out loud

BERT is an Encoder Only model trained with Masked Language Modeling to look at past and future context simultaneously, excelling at text classification and search embeddings. GPT is a Decoder Only model trained with Causal Next Token Prediction to process text left to right, excelling at text generation, chat, and reasoning.

Followups to expect

  1. What is RoBERTa? An optimized version of BERT that removed the Next Sentence Prediction task, trained longer on larger batch sizes with dynamic masking, outperforming original BERT.
  2. What is RoFormers / DeBERTa? Disentangled Attention models that separate word content embeddings from relative position embeddings, improving BERT style encoder performance.

Check yourself

Question 1 of 3

What is the main architectural difference between BERT and GPT?

More in NLP & Transformers

See all →
The Attention Mechanism5 minTransformer Architecture5 minTokenization & BPE5 min