Transformers and Attention
Transformers process relationships among tokens using attention. This architecture made it practical to train models on long sequences at large scale.

Learning objectives
What you will be able to do
- Explain transformers and attention accurately in your own words.
- Recognise how the concept appears in real AI products and professional work.
- Identify an important limitation, risk or evaluation requirement.
- Apply the concept through a practical activity and knowledge check.
Core explanation
Build the right mental model
Attention calculates how strongly each token should consider other tokens when building a contextual representation. The word 'bank' should relate differently to 'river' than to 'loan'; attention helps represent that context.
Multiple attention heads can learn different relationships, while feed-forward layers transform each position. Positional information preserves order. Stacking many blocks creates increasingly rich representations.
Attention values are useful engineering signals but should not automatically be treated as a complete human-readable explanation of a model’s reasoning.
From a static word representation to context
Consider “the bank approved the loan” and “the bank beside the river”. A useful representation of bank should change with the sentence. In a transformer, attention helps combine information from positions in the available sequence. The mechanism operates on numerical representations rather than a human drawing arrows between words, but the arrows are a helpful first sketch of which context might matter.
Each attention operation forms queries, keys and values through learned transformations. A query is compared with keys to obtain scores; normalised scores weight the value vectors. The result is a mixture of information, not a selected dictionary definition. Several heads can produce different mixtures. Further transformations and layers combine them, so one attention pattern is only a small part of the entire computation.
Order and visibility matter
A sequence needs information about position because “the teacher questioned the student” differs from “the student questioned the teacher”. Transformer designs add or incorporate positional information. For next-token generation, a causal mask prevents a position from using later tokens during that attention calculation. An encoder processing a complete input may use a different visibility pattern. Avoid assuming every transformer has an identical architecture.
The original Transformer paper introduced an encoder–decoder design for sequence tasks. Many later text generators use decoder-focused designs. The lesson is not that every modern model is an unchanged copy of the original. It is that attention-based representations, position and repeated transformations offer a useful way to reason about how sequence information is combined.
Avoid turning a mechanism into a psychological story
An attention weight is not a percentage of human-like understanding. A high weight does not by itself prove that a token caused the final decision, and a diagram does not reveal a complete explanation of the model’s reasoning. Research on attention and explanation warns against that shortcut. To investigate a behaviour, compare controlled input changes and outputs as well as internal signals.
Likewise, a sentence such as “the router dropped the packet because it was overloaded” has a likely interpretation, but an attention sketch cannot establish what any particular model did. Use unambiguous examples for teaching the mechanism and explicitly label hand-drawn weights as illustrations. This separates an accessible analogy from an unsupported claim about a deployed system.
Self-attention
Tokens build contextual representations by weighting relationships with other tokens.
Attention head
One learned pattern of relationships within an attention layer.
Position
Information representing token order in a sequence.
Transformer block
Attention and feed-forward components repeated through the network.
Worked example: calculate a tiny attention mixture
Suppose a toy attention step has already produced weights 0.6, 0.3 and 0.1 over three value vectors: (2, 0), (0, 4) and (1, 1). The weights total one. The weighted mixture is 0.6 × (2, 0) + 0.3 × (0, 4) + 0.1 × (1, 1) = (1.3, 1.3). Calculate each coordinate separately: 1.2 + 0 + 0.1 and 0 + 1.2 + 0.1.
This example begins after the score-normalisation stage and omits learned projections, scaling, masking and the rest of the block. It demonstrates a weighted combination, not a working language model. No coordinate is labelled “loan” or “river”, and the output is not a probability that a sentence is true.
If the weights change to 0.2, 0.7 and 0.1 while the values stay fixed, the mixture becomes (0.5, 2.9). Context-dependent weights can therefore change a representation without changing the underlying value vectors in this simplified calculation. Multiple heads and later layers extend the computation beyond this one mixture.
Apply your learning
Practical activity
- Spend 5 minutes writing two unambiguous sentences using the word “port”: one about a harbour and one about a network service. Underline the clues that distinguish them.
- Spend 7 minutes recalculating the toy mixture with weights 0.5, 0.25 and 0.25. Check that the weights total one.
- Spend 5 minutes sketching which earlier words are visible when generating the final word of a short sentence, using a causal setting.
- Spend 3 minutes writing two limitations of your sketch and calculation.
Compare with an explained example answer
The new mixture is 0.5 × (2, 0) + 0.25 × (0, 4) + 0.25 × (1, 1) = (1.25, 1.25). Suitable context clues are “ship” and “harbour” for the first sentence, and “TCP” or “service” for the second.
A causal prediction uses the prefix already available, not a future word that has not been generated. The arrows are a human explanation of possible relationships, not measured model attention. The calculation omits most of the transformer and does not demonstrate comprehension, accuracy or factual verification.
Write your answers in a learning journal. The value comes from connecting the concept to your own profession.
Knowledge check
Test your understanding
Q1What is being combined in the toy attention calculation?
Answer: Value vectors are multiplied by weights and added coordinate by coordinate. The result is a representation, not a factual-confidence score.
Q2Why include positional information?
Answer: Token identities alone do not distinguish every word order. Position lets the architecture represent sequence relationships.
Q3Can attention weights alone explain a model decision?
Answer: They reveal part of the computation, but are not a complete causal account. The rest of the model and controlled behavioural evidence also matter.
If you cannot explain the answer without reading it, revisit the core explanation and example before continuing.
Continue with authoritative sources
Important industry resources
These links lead to official organisations, industry laboratories or established open-source learning projects.