Transformers Explained: Self-Attention, BERT, GPT, and Positional Encoding
Query, key, and value vectors, scaled dot-product attention, multi-head attention, and how BERT and GPT use the same transformer block differently.
A transformer is a neural architecture that mixes information across a sequence using self-attention instead of recurrence. It is the backbone of BERT, GPT, T5, and most modern large language models. The famous 2017 paper titled the idea bluntly: Attention Is All You Need.
This guide explains queries, keys, and values, scaled dot-product attention, multi-head attention, positional encoding, and the encoder-versus-decoder split that distinguishes BERT from GPT.
The problem attention solves
An RNN (or LSTM) reads tokens left to right. Distant dependencies must survive a chain of hidden states. Attention lets every position look at every other position in one step. For sequence length , self-attention is in time and memory — expensive for very long context, but highly parallel on GPUs.
That parallelism, not just accuracy, is why transformers scaled.
Queries, keys, values
Each token embedding is linearly mapped to three vectors:
Query asks “what am I looking for?” Key answers “what do I contain?” Value is the content that will be mixed in if the match is strong.
Scaled dot-product attention for a stack of tokens is
The scale keeps dot products from saturating the softmax when is large. The softmax row for token is a probability distribution over tokens . The output is a weighted sum of value vectors.
In a sentence such as “The animal did not cross the street because it was tired,” attention can learn to send “it” toward “animal” rather than “street.”
Explore the pieces in the Transformer Calculator: self-attention, multi-head attention, and positional encoding are easier to remember when you can change head count and sequence length.
Multi-head attention
One set of learns one type of relation. Multi-head attention runs attention operations in parallel with different projections, then concatenates:
Heads can specialize: syntax, coreference, positional patterns, rare-token copying. More heads are not automatically better; they cost memory and can become redundant.
Positional encoding
Self-attention is permutation-invariant: without extra information, it cannot tell “dog bites man” from “man bites dog.” Transformers add positional encodings — sinusoidal functions in the original paper, learned embeddings in many later models, relative positions or RoPE in modern LLMs.
Absolute sinusoidal encodings use
They are added to token embeddings before the first layer.
Encoder, decoder, BERT, GPT
The original transformer has an encoder stack (bidirectional self-attention over the source) and a decoder stack (masked self-attention over the target plus cross-attention into the encoder).
- BERT is encoder-only. Every token may attend to every other token. Pretraining is masked language modeling. Good for classification and span prediction.
- GPT is decoder-only. Attention is causal: token may not look at . That matches next-token prediction.
- T5 / original transformer use encoder-decoder for translation and other sequence-to-sequence tasks.
The block around attention is standard: residual connection, layer normalization, and a two-layer MLP (feed-forward network) applied position-wise.
Transformers versus LSTMs
| LSTM | Transformer | |
|---|---|---|
| Path length for distant tokens | attention | |
| Parallelism over time | Limited | High |
| Inductive bias | Local recurrence | Almost none (plus position) |
| Long context cost | per step | attention |
| Typical NLP default (2026) | Specialized / streaming | Pretrained LLMs |
If you need the gated-memory story first, read LSTM explained. If you need the last-layer classifier math, see sigmoid and softmax.
Frequently asked questions
What is self-attention in one sentence?
Each token computes a weighted average of all tokens’ value vectors, with weights from how well its query matches their keys.
Why do we divide by ?
Unscaled dot products grow with dimension, pushing softmax toward one-hot vectors and tiny gradients. Scaling stabilizes training.
Do I need a transformer to classify images?
Not necessarily. CNNs remain strong. Vision transformers (ViT) patch an image into tokens and apply the same attention stack. The CNN guide is the right starting point for spatial filters.
Why is GPT called generative?
Causal decoding produces the next token from previous tokens, repeatedly. That is generation. BERT’s bidirectional encoder is not usually used that way without extra heads.
Next steps
Compute a attention matrix by hand for tiny vectors. Confirm that rows of sum to 1. Then use the Transformer Calculator to vary heads and sequence length. For the optimization that trains these blocks, keep gradient descent and loss functions nearby.
Continue reading
LSTM Explained: Forget Gate, Cell State, and Long-Term Memory in RNNs
Why vanilla RNNs forget, how LSTM gates protect a cell state, and when to use LSTM versus GRU for time series and sequence modeling.
Neural Networks Explained from First Principles: Layers, Activations, and Backpropagation
Learn how multilayer perceptrons actually compute: weighted sums, nonlinear activations, forward pass, loss, and the backpropagation algorithm that trains them.