Transformer
The Transformer is a neural network architecture introduced in the 2017 paper "Attention Is All You Need." It revolutionized natural language processing by replacing recurrent neural networks (RNNs) with self-attention mechanisms that can process entire sequences in parallel.
The key innovation is the attention mechanism, which allows the model to weigh the importance of different parts of the input when producing each part of the output. Multi-head attention enables the model to attend to different types of relationships simultaneously.
Transformers consist of encoder and decoder blocks, each containing self-attention layers and feed-forward networks. Models like BERT use the encoder, GPT uses the decoder, and T5 uses both. The architecture scales well with data and compute, enabling the creation of increasingly powerful models.
The Transformer architecture powers virtually all modern AI: LLMs (GPT-4, Claude, Gemini), image models (Vision Transformers), speech models (Whisper), and multimodal models.
How it works
The Transformer was introduced in "Attention Is All You Need" (Vaswani et al., 2017) as an architecture "based solely on attention mechanisms, dispensing with recurrence and convolutions entirely". Because no step waits for the previous one, as in a recurrent network, training parallelises well across a sequence.
The core operation is scaled dot-product attention: Attention(Q, K, V) = softmax(QKᵀ / √d_k) V. Each token's query is compared with every token's key, the scores become weights through softmax, and the output is a weighted mix of the values. The paper divides by √d_k because large dot products push softmax into regions with extremely small gradients.
Multi-head attention runs several attention operations in parallel on different learned projections. The paper's base model used 6 encoder and 6 decoder layers, a model dimension of 512 and 8 heads of 64 dimensions each. Attention by itself ignores word order, so positional encodings built from sine and cosine functions are added to the token embeddings.
The original results were on translation: 28.4 BLEU on English-to-German and 41.8 BLEU on English-to-French, the latter after 3.5 days of training on eight GPUs. Later models kept the building blocks but often used only the decoder stack, which is the shape of most current large language models.
Example
Scaled dot-product attention in a few lines of NumPy, following the formula from the paper.
import numpy as np
def attention(Q, K, V):
d_k = Q.shape[-1]
scores = Q @ K.T / np.sqrt(d_k) # how much each token attends to each other token
weights = np.exp(scores - scores.max(-1, keepdims=True))
weights /= weights.sum(-1, keepdims=True) # softmax over each row
return weights @ V # weighted mix of value vectors
# 4 tokens, 8 dimensions each
x = np.random.rand(4, 8)
out = attention(x, x, x) # self-attention: Q, K and V from the same tokens
print(out.shape) # (4, 8) In a real model, Q, K and V come from three learned linear projections of x, several heads run at once, and decoder models mask the scores so a token cannot attend to later tokens.
Common confusion
Is a Transformer the same thing as an LLM?
No. The Transformer is an architecture; an LLM is a large model trained on text, and most LLMs use that architecture. Transformers are also used for translation, speech, images and embedding models that do not generate text.
Why does context length get expensive?
Self-attention compares every token with every other token, so the score matrix grows with the square of the sequence length. Doubling the context roughly quadruples that part of the work, which is one reason long prompts cost more.
Related tools & demos
- Neural Network Playground Build intuition for layers, weights and training.
- AI Tokenizer Visualizer See the tokens a Transformer actually operates on.
- Large Language Model The best-known application of the architecture.
- Vector Embeddings Transformer encoders also produce embeddings for search.
Further reading
- Attention Is All You Need (Vaswani et al.) arXiv 1706.03762
Want to learn more?
Explore more developer terms or read in-depth articles on the blog.
Browse all terms