Exercises — Positional encodings (sinusoidal)
This page is a self-test. Read each problem, try it on paper, THEN open the collapsible solution. Every symbol used here is built in the parent note Positional encodings (sinusoidal); if a term feels new, the callout defining it is linked.
Notation reminder (so line one is followable):
- = the integer position of a token, counting from . The very first token is . Throughout this page we always call the position — never any other letter.
- = how many numbers make up one token's vector (its dimension).
- = the "dimension pair index". Dimensions come in pairs : the even one uses , the odd one uses . So dimension belongs to pair , dimension belongs to pair , dimension to pair , and so on.
- = the angular frequency of pair — literally "how fast the sine wiggles as grows." Big = fast wiggle, small = slow wiggle. We index by the pair , not by the dimension number. So the pair living in dimensions has ; the pair in dimensions has .
- = the wavelength — how many positions before the wave repeats.
All angles below are in radians (the natural unit where a full circle is ).

Level 1 — Recognition
L1.1 — Which function goes where?
For , list which trig function (sine or cosine) each of the 6 output dimensions uses.
Recall Solution
The rule: even index → sine, odd index → cosine. Index parity, not the pair index , decides.
- Dim (even) → , pair
- Dim (odd) → , pair
- Dim (even) → , pair
- Dim (odd) → , pair
- Dim (even) → , pair
- Dim (odd) → , pair
Three pairs, each a couple sharing one frequency . That shared frequency is what lets the pair trace a point on a circle — the geometry that gives us the rotation defined above.
L1.2 — Read off a frequency
For , what is (the frequency of dimension pair , i.e. dimensions )? What is its wavelength ?
Recall Solution
This slow wave takes ~628 positions to repeat — it encodes coarse position.
Level 2 — Application
L2.1 — Compute a full encoding vector
For , compute to 4 decimals.
Recall Solution
Pairs: . Frequencies straight from the definition :
Why 4 decimals is safe here: never exceed magnitude and their slope is at most , so a rounding error of in the angle can move the output by at most — well inside the 4th decimal. The values are stable to report.
L2.2 — The origin position
Compute for any . What is special about it?
Recall Solution
At every sine/cosine argument is :
- Even dims:
- Odd dims:
So — an alternating zero/one pattern, independent of 's frequencies. Geometrically every pair starts at angle , i.e. pointing along the positive- axis — this is the yellow "start" dot at in figure s02.
L2.3 — Add PE to an embedding
Token "sat" has embedding at , . Give the position-aware vector.
Recall Solution
Add component-wise:
Level 3 — Analysis
L3.1 — The rotation matrix for a shift
For , pair (, dimensions ), write the matrix that maps to , with numbers.
Recall Solution
Using the shift matrix defined at the top of this page with and : Why exactly this form: moving one step adds to the pair's angle (see the definition callout and figure s03), and rotating a circle-point by a fixed angle is by definition multiplication by . Why determinant : by the Pythagorean identity — a pure rotation neither stretches nor shrinks, so no information about magnitude is lost, only the angle turns.
L3.2 — Verify the shift on real numbers
Using from L2.1, apply to its second pair and check you get 's second pair.
Recall Solution
Top: Bottom: Direct compute: , . ✅ They match — the rotation IS the position shift.
L3.3 — Which dimension changes faster?
Between and , does dimension (pair ) or dimension (pair ) change more, for ? Explain via frequency.
Recall Solution
Recall the pairing: dimension lives in pair with ; dimension lives in pair with .
- Dim 0:
- Dim 2:
Dimension 0 changes ~84× more. Reason: is the highest frequency (fast wiggle), so a one-step move sweeps a big angle; barely moves. High-frequency dims track fine local order; low-frequency dims track coarse global position — exactly the "zoom levels" the parent note describes.
Level 4 — Synthesis
L4.1 — Design a base for a target wavelength
You want the longest wavelength (highest pair index , with so ) to be about positions instead of . What base (replacing ) achieves ?
Recall Solution
Set equal to : So a base of roughly stretches the longest wavelength to positions. Bigger base → longer wavelengths → better long-range coverage but coarser resolution.
L4.2 — Two positions with identical low-frequency component
For , dimension (pair ) uses , wavelength . Which integer position first gives about the same value as (i.e. back near , rising)?
Recall Solution
with the same upward slope repeats after one full period positions. Since must be an integer, the nearest is : This shows why ONE frequency is ambiguous — position and look alike in that single dimension. The other dimensions (with different frequencies) break the tie, giving every position a unique full vector.
Level 5 — Mastery
L5.1 — Dot product between two encodings depends only on distance
For a single pair , show that (the pair's 2 components dotted) depends only on the distance , not on absolute positions. Compute it for , , .
Recall Solution
The pair's two components at position form the vector (same we've used all page — no new letters). Dot the vectors at and : using . The result is where — absolute positions cancelled; only the gap survives. This is the seed of relative attention: similarity between positions is a clean function of their distance. Numeric: , .
L5.2 — What happens when passes ? (the wrap-around edge case)
For pair (), the argument is just radians. Track the sign of and as grows through . Which quadrant is each in, and when does the pair complete one full loop?
Recall Solution
The angle is radians. A full circle is , so the pair completes one loop when crosses and then keeps going (the angle just keeps adding — sine/cosine wrap automatically, this is why values stay bounded no matter how large gets).
- : angle → , on the positive- axis.
- : angle rad (between and ) → Quadrant II: (), ().
- : angle rad (between and ) → Quadrant III: (), ().
- : angle rad (between and ) → Quadrant IV: (), ().
- : angle rad ; subtract one loop rad → back to Quadrant I: (), ().
The point has walked all the way around and started a new lap. Key takeaway: the fast dimension revisits every quadrant quickly, so its sign alone never pins down absolute position — you need the slow dimensions (which are still deep in Quadrant I at these small angles) to say which lap you're on.
L5.3 — Full pipeline reasoning
"The cat sat", . Embedding of "cat" is at . Give its position-aware vector, then state (no computation) what changes if you move "cat" to — which dimensions move a lot, which barely?
Recall Solution
. Add to embedding: At : the high-frequency dims (, angle rad full loops) have wrapped around many times — they look "randomly" placed on the fast circle, while the low-frequency dims (, angle rad) move only to — a modest, orderly change still in Quadrant I. Fast dims resolve exact neighbourhood; slow dims track that we're "far into the document."
L5.4 — Relate to RoPE and relative encodings
In one sentence each, connect this page's rotation-matrix result to Rotary Position Embeddings (RoPE) and to Relative Positional Encodings.
Recall Solution
- RoPE: instead of adding a sinusoid then hoping a linear layer learns the rotation , RoPE directly rotates the query and key vectors by , baking the relative-position rotation into attention by construction — see Rotary Position Embeddings (RoPE).
- Relative encodings: L5.1's "dot product depends only on distance" is precisely the property Relative Positional Encodings make explicit, encoding rather than absolute positions.
Recall Self-test checklist
Even index means which function? ::: Sine. formula? ::: . What geometric object is ? ::: A rotation matrix (by angle ). pattern? ::: Alternating . Pair dot product equals? ::: . Why do sin/cos values stay bounded as grows huge? ::: The angle just wraps around the circle every .
Prerequisites revisited: Token Embeddings, Self-Attention Mechanism, Multi-Head Attention, Learned Positional Embeddings, Fourier Transform.