What is the difference between BERT and GPT?

Both architectures are powered by Vaswani et al.'s Transformer, but their attention directionality, training objectives, and computational graphs solve entirely opposite operational problems. Explore their self-attention matrices and token pathways below.

Disambiguation & Flow Presets:
Attention Head:

BERT (Encoder-Only)

Bidirectional Representations from Transformers (Devlin et al., 2018)
BIDIRECTIONAL
Input Sequence + Special Tokens (Click to inspect focus) 8 tokens
Full Unmasked Attention Matrix (NxN)
Hover cell: token_i → token_j
Visibility: 100% (Past, Current & Future)
0.0
1.0

Objective: Masked LM (MLM) + NSP

BERT masks 15% of tokens with [MASK] and reconstructs them using context from both the left and right simultaneously.

Attention Mask
Unmasked (All 1s)
Target Word Meaning
Riverbank (Geog)

GPT (Decoder-Only)

Generative Pre-trained Transformer (Radford et al., 2018 / OpenAI)
CAUSAL / AUTOREGRESSIVE
Causal Sequence (Click to view causal horizon) 7 tokens
Lower-Triangular Causal Mask Matrix
Hover cell: token_i → token_j
Upper Triangle: Masked (-∞ / 0.0)
0.0
1.0

Objective: Causal Language Modeling (CLM)

GPT optimizes $P(w_t \mid w_1, \dots, w_{t-1})$. Attention to future tokens ($j > i$) is strictly forbidden via an upper-triangular $-\infty$ mask.

Causal Barrier
Strictly Left-to-Right
Next Predicted Token
" flowing" (68.4%)

Core Architectural & Practical Divergences

Dimension BERT (Encoder-only) GPT (Decoder-only)
Attention Visibility Bidirectional (All-to-all): Token at position $i$ attends simultaneously to positions $1 \dots N$. Can look ahead into future words. Causal / Autoregressive: Token at position $i$ only attends to positions $j \le i$. Upper triangle is hard-masked with $-\infty$.
Self-Attention Mask Identity / Unmasked: full dense $N \times N$ matrix. Lower Triangular: $M_{i,j} = 0$ if $j \le i$, else $-\infty$.
Pre-training Objective Masked Language Modeling (MLM) predicting 15% missing tokens + Next Sentence Prediction (NSP). Standard Next-Token Autoregressive Loss: $\max \sum_t \log P(x_t \mid x_{
Downstream Usage Requires an auxiliary task head (e.g., linear classification head for sentiment or token classification for NER). Fine-tuned end-to-end. Few-shot in-context learning, prompt engineering, and open-ended text completion without structural modifications.
Primary Strengths Semantic search embedding, re-ranking, named entity recognition (NER), extractive question answering (e.g. SQuAD). Generative conversations, coding, creative writing, step-by-step reasoning, mathematical problem solving.

Interactive Scenario Selector: Which model should you use?

Vector Search & Document Re-ranking
Produce dense embedding vectors representing an entire document or passage for cosine similarity lookups.
✓ BERT / Bi-Encoder Architecture
Multi-Turn Chatbot & Agent Reasoning
Generate coherent, multi-paragraph responses and follow instructions step-by-step.
✓ GPT / Causal Decoder Architecture
Token-Level Named Entity Recognition (NER)
Label individual words (Person, Organization, Location) where future tokens in the sentence resolve ambiguities.
✓ BERT / Token Classification Head
Code Generation & Autocomplete
Stream code token-by-token based on previous function signatures and comments.
✓ GPT / Autoregressive Sampling
Enjoy this tool? Build your own with Super