The 2017 transformer paper combines attention with feed-forward layers and position information. Token representations are stored in tensors; attention computes weighted combinations of those representations.
For a simplified example, one attention head attending to two allowed positions might assign weights 0.8 and 0.2. Its output is 0.8 × v₁ + 0.2 × v₂, where v₁ and v₂ are their value vectors. These illustrative weights show the weighted sum; actual attention weights depend on the query and keys.
An encoder can use tokens on both sides of a position. A text-generating model such as GPT-2, a generative pre-trained transformer, uses a causal decoder, which processes the current and earlier input positions. During training, the whole sentence may already be available: in “the cat sat on the mat,” the representation at “sat” must predict “on” without reading “on the mat.” A causal mask blocks those future connections.
Full self-attention compares pairs of positions, so its computation scales quadratically with sequence length when other dimensions stay fixed. State-space models offer another approach to sequence processing. A recurrent neural network is the older step-by-step alternative, and a vision transformer applies attention to image patches instead of words.
Liquid AI’s Maxime Labonne describes a hybrid built for on-device language models in the related episode. He says it combines some attention layers with short convolutions.
Try the weighted sum
One head, two allowed positions. Keep their value vectors fixed and change how much each contributes.
v₁ = (1, 2)
v₂ = (3, 0)
The weight on v₂ is 1 minus this weight. Use the arrow keys to adjust.
0.8 × (1, 2) + 0.2 × (3, 0)
Weighted sum
(1.4, 1.6)
These are illustrative vectors and weights. In scaled dot-product attention, query–key scores pass through softmax to produce the weights. This demo starts after that step; the coordinates have no assigned word meaning. See the original paper, section 3.2.1.
Hear it from the guest
“The architecture that we ended up with is a hybrid that has some attention layers.”
Quotes lightly edited to remove filler words.
Sources
- Vaswani et al.: Attention Is All You Need — Sections 3 and 4 explain attention, decoder masking, feed-forward layers, position information and quadratic full-attention computation.
- Hugging Face Transformers: GPT-2 — Documents a transformer decoder with a language-modeling head and next-token prediction, providing a concrete text-generation example.
Go deeper
- Hugging Face: How do Transformers work? course
Compare encoders, decoders and attention, then follow why the original decoder masks future target tokens during training.