Skip to content
datarekha

BERT, GPT, T5 — picking the right architecture

Three transformer families with different pretraining objectives and different jobs. How to choose between encoder-only, decoder-only, and encoder-decoder today.

Written and reviewed by Last reviewed

8 min read Advanced NLP & Transformers Lesson 28 of 44

What you'll learn

  • The difference between encoder-only, decoder-only, and encoder-decoder
  • Which family is best for which task
  • Why decoder-only models took over since 2023

Before you start

Three transformer families exist, each built around a different pretraining objective (the practice task a model learns from before you ever use it). The objective shapes everything: what tasks the model is good at, how you use it, and how big it tends to be. Knowing which family fits your problem saves you both money and embarrassment.

Two words you will see throughout. An encoder reads the whole input and turns it into numbers that capture its meaning. A decoder writes output one token at a time. The three families use an encoder alone, a decoder alone, or both.

The cheat sheet

FamilyArchitecturePretraining taskAttentionBest for
BERTEncoder-onlyMasked LM (hide some words, predict them)Bidirectional (looks both left and right)Classification, embeddings, retrieval
GPTDecoder-onlyNext-token predictionCausal (sees only earlier tokens)Generation, chat, instruction-following
T5Encoder-decoderSpan corruption (denoising)Encoder bidirectional, decoder causalTranslation, summarization, structured seq2seq
BERTbidirectional encoderEncoder stackattention maskevery token sees every tokenMLM: predict [MASK]GPTcausal decoderDecoder stackattention masktoken i sees tokens 0..iNTP: predict next tokenT5encoder decoderEncoderDecoderencoder and decoder masksenc bidec causalbi + causal + cross attnspan corruption: fill blanksfilled cell = token i attends to token j; empty = masked
Three architectures, three attention masks. The mask shape is the deepest difference between the families.

BERT — the encoder for understanding

BERT (2018) processes the whole input bidirectionally — every token sees every other token, in both directions. Pretraining task: “I’ll mask 15% of the tokens; predict them.” To do this well, the model has to deeply understand context in both directions.

"The [MASK] sat on the mat."
              ↓
"The cat sat on the mat."

BERT is not generative. Bidirectional attention means it sees the whole sequence at once, so it can’t autoregressively produce output (write one token at a time, each based on the ones before). BERT-style models excel at understanding tasks where you need a fixed-size representation of a piece of text:

  • Sentence classification (sentiment, spam, intent)
  • Token classification (named entity recognition)
  • Sentence-pair tasks (similarity, entailment)
  • Embeddings for retrieval — this is where they shine today

Embeddings is the killer app for encoder-only models. Every RAG system (retrieval-augmented generation, where a model looks up documents before answering) uses an encoder to turn documents and queries into embeddings: lists of numbers where similar meanings land close together. Models like BAAI/bge, intfloat/e5, nomic-ai/nomic-embed, and mixedbread-ai/mxbai-embed-large are BERT-family descendants optimized for retrieval — and they’re the workhorse of modern LLM applications.

GPT — the decoder for generation

GPT-style models use causal attention: token i can only see tokens 0..i. The pretraining task is “predict the next token, given all the previous ones.”

"The cat sat on the" -> "mat"

Causal attention is what makes generation natural — each new token is predicted from the previous ones, then appended, then the process repeats. Decoder-only models are the basis of:

  • Chat assistants (ChatGPT, Claude, Gemini)
  • Code generation (Copilot, Cursor)
  • Agentic systems
  • Anything that produces variable-length output

Examples today: GPT-4o, Claude Sonnet/Opus, Llama 3.1/3.2, Mistral Large, Qwen 2.5, DeepSeek V3. Every major frontier LLM today is decoder-only. This is the dominant paradigm, and it’s not close.

T5 — encoder-decoder for seq2seq

T5 (2019) has both an encoder (bidirectional) and a decoder (causal). Pretraining: corrupt spans in the input (blank out short runs of words), then train the decoder to produce the missing pieces. The decoder also uses cross-attention, meaning it looks back at the encoder’s reading of the input while it writes.

Input:  "The <X> sat on <Y>."
Output: "<X> cat <Y> the mat"

T5 shines on seq2seq (sequence-to-sequence: one piece of text in, a different piece of text out) tasks: translation, summarization, structured text-to-X. T5 family today: original T5, FLAN-T5, CodeT5+, mT5. They’re not extinct, but mostly displaced by big decoder-only models that handle the same tasks via prompting.

When to pick which

Use an encoder-only model when:

  • You need embeddings (retrieval, clustering, classification at scale)
  • You’re doing classification on short text and want a cheap, fast model
  • Your task has a fixed output shape, not free-form text

Use a decoder-only model when:

  • You want generation, chat, instruction-following
  • You want one model that does many tasks
  • You want to use it via an API or open-weights (almost everything)

Use an encoder-decoder model when:

  • You’re doing translation or summarization at scale and need every drop of performance
  • You’re building on existing T5 checkpoints
  • Otherwise: just use a decoder-only model

Hugging Face naming, decoded

bert-base-uncased            # encoder-only, English, lowercase
google/flan-t5-base          # encoder-decoder, instruction-tuned
meta-llama/Llama-3.1-8B      # decoder-only, base
meta-llama/Llama-3.1-8B-Instruct  # decoder-only, chat-tuned

The convention: org/model-version-size-variant. “Instruct” or “Chat” in the name = RLHF/SFT-tuned (further trained with human feedback or example answers) for following instructions; without it, you get the raw pretrained model.

In one breath

  • Three families, three pretraining objectives, three attention masks — and the mask is the deepest difference.
  • BERT (encoder-only, bidirectional, masked-LM): every token sees every other, so it understands but can’t generate — the workhorse for embeddings, retrieval, and classification.
  • GPT (decoder-only, causal, next-token): token i sees only 0..i, which makes generation natural — chat, code, agents. Nearly every frontier chat LLM today is decoder-only, because one simple objective scales and prompting recovers the rest.
  • T5 (encoder-decoder, span-corruption): a bidirectional encoder plus a causal decoder with cross-attention — strongest on explicit seq-to-seq (translation, summarization), but mostly displaced by big decoder-only models. Rule of thumb: embeddings → encoder; generation → decoder; otherwise just use a decoder-only model — and don’t use an LLM where an embedding model belongs.

Quick check

Quick check

0/4
Q1You want to build a semantic search system over 10 million internal documents. Which model family do you start with?
Q2Why are virtually all frontier LLMs (Claude, GPT, Llama, Gemini) decoder-only rather than encoder-decoder?
Q3Your team must flag each incoming support email as 'urgent' or 'not urgent' at 50 million emails per day on a tight budget. The label is one of two fixed values. Which choice fits best?
Q4Which attention pattern lets a model be used for token-by-token text generation?

Sign in to track your progress

Completed lessons, your XP, level, and streak save to your account — it's free and takes a few seconds.

Practice this in an interview

All questions
How do attention masks and pretraining objectives influence the choice between BERT, GPT, and T5?

Attention masks determine whether a model builds bidirectional representations, generates causally from left to right, or combines source understanding with target generation. Pretraining objectives reinforce those patterns: masked language modeling suits representations, next-token prediction suits continuation, and sequence-to-sequence reconstruction suits source-to-target tasks.

What is the difference between encoder models like BERT and decoder models like GPT?

BERT is an encoder-only transformer that reads all tokens bidirectionally and is trained with masked language modelling — ideal for tasks requiring a rich contextual representation of an entire sequence, like classification or NER. GPT is a decoder-only transformer that attends only to previous tokens via a causal mask and is trained with next-token prediction — ideal for text generation. Encoder-decoder models like T5 combine both for tasks that map one sequence to another.

How does GloVe differ from Word2Vec in learning word embeddings?

GloVe (Global Vectors) builds a global co-occurrence matrix over the entire corpus and then factorizes it, directly encoding how often pairs of words co-occur. Word2Vec uses local context windows and a prediction objective, never explicitly seeing the global statistics. GloVe tends to capture linear substructures slightly better while Word2Vec handles rare words better with negative sampling.

How does Word2Vec work, and what is the difference between Skip-gram and CBOW?

Word2Vec trains a shallow neural network to predict context from a target word (Skip-gram) or a target word from its context (CBOW), learning dense vector representations as a by-product. Skip-gram works better for rare words; CBOW is faster and suits large corpora.

Why do dense word embeddings outperform one-hot vectors?

Dense embeddings usually outperform one-hot vectors because they represent words with learned, low-dimensional relationships rather than isolated IDs, allowing models to share statistical strength between similar words. The advantage is not automatic: one-hot can be preferable for small vocabularies, exact-identity tasks, or when embeddings are poorly trained or mismatched to the domain.

What is the key difference between Word2Vec embeddings and BERT's contextual embeddings?

Word2Vec assigns a single static vector to each word token regardless of surrounding words, so polysemous words like 'bank' always get the same representation. BERT generates a different vector for each token depending on its full sentential context, allowing it to disambiguate meaning on the fly.

Related lessons

Explore further