Exercises — Positional encoding
Self-check questions, recall cards, exercises, and worked answers
← Back to the lesson: Positional encoding
Check your understanding
Formative exercises use this page in the Transformer collection.
- Two sentences contain the same words in a different order. Why are initial embeddings alone not enough to distinguish them?
Show the answer
The lookup assigns the same initial vector to every occurrence of a token, whatever its position. Without explicit positional information or a structural constraint, content-based attention does not carry the order on its own.
- Does RoPE add a position vector to token embeddings?
Show the answer
No. RoPE applies position-dependent rotations to the query and key vectors, after projection and before the score dot product. It is a relative mechanism, distinct from absolute encodings added to the input representations.
- Does the causal mask replace a positional encoding?
Show the answer
No. The causal mask is a visibility constraint: it restricts each position to its prefix, imposing an order on the computation, but it encodes neither absolute positions nor relative offsets. The two mechanisms are distinct and often combined.
- The sinusoidal formula can produce a vector for a position never seen during training. Can we conclude that the model will generalize correctly to that length?
Show the answer
No. The formula defines a signal for that position, but the model still has to learn how to use it. Generalization also depends on training sequence length, data, architecture, and any context-extension methods.
You have understood this step if you can explain why order must be taken into account, distinguish absolute encodings from relative mechanisms (of which RoPE is one example), separate positional signals from the causal visibility constraint, and explain why a computable signal does not guarantee extrapolation.
Compare two visible prefixes
Consider one layer of causal self-attention with no explicit positional mechanism. The input queries, keys, and values are fixed per token. Compare B’s output in ACB and CAB, where B stays in the last position. Can the causal mask alone distinguish this swap at B’s output?
Show the reasoning
B sees the same three tokens in both cases. The key-value pairs for A and C switch places, so their weights and contributions are permuted together. In this layer, B’s output is the same weighted sum. In a stack of layers, the states for A and C may already have changed in an earlier block, because each sees its own prefix there.
Recall cards
Try to answer before opening each card. They rehearse the distinctions to retain; the exercises above and below ask you to apply them.
Card 1 — Why does self-attention need an ordering signal?
Without a positional signal or an order-dependent structural constraint, content-based self-attention is permutation equivariant: it reorders its outputs with its inputs.
Card 2 — Where is an additive absolute encoding injected?
At the input: the position vector is added to the token embedding before the first Transformer block.
Card 3 — How do Shaw, T5, and ALiBi differ?
Shaw learns relative key/value representations; T5 adds a learned bucketed scalar bias to scores; ALiBi adds a fixed linear bias per head.
Card 4 — Which vectors does RoPE rotate?
Projected queries Q and keys K, before their dot product. Values V are not rotated.
Card 5 — What does NoPE rely on?
Implicit order from nested causal prefixes, without explicit absolute coordinates or relative distances; this does not guarantee extrapolation.