4.1.6 · D1Transformer Architecture

Foundations — Positional encodings (sinusoidal)

1,803 words8 min readBack to topic

Before you can read the parent note comfortably, you need a small toolbox of pictures. This page builds every symbol it uses, in order, from nothing. Each block gives you: plain meaning → the picture → why the topic needs it.


1. A position is just a counting number

Picture: a row of boxes. "The" goes in box , "cat" in box , "sat" in box .

Why the topic needs it: the whole problem is that self-attention (see Self-Attention Mechanism) does not know box order — shuffle the boxes and it gives the same answer. So is the thing we must somehow feed in.


2. A vector is a short list of numbers

Picture: an arrow, or simpler — a vertical stack of little bars, one bar per number. A 4-long vector is 4 stacked bars.

Figure — Positional encodings (sinusoidal)

Why the topic needs it: every word becomes such a vector (a token embedding), and the positional encoding we build is also a vector of the same length . We must match lengths so we can add them.

Recall Why must PE have length

? Because we add it to the token embedding, and you can only add two lists of numbers slot-by-slot if they are the same length ::: PE and the embedding are both long so the sum is defined.


3. Adding two vectors

Picture: lay the two stacks of bars side by side; the result is a new stack where each bar is the two bars piled on top of each other.

Why the topic needs it: the parent note says PE is added (not glued on) to the embedding. Addition keeps the length at , so the next layer sees a vector of the size it expects.


4. Sine and cosine — the two waves

This is the heart of everything, so we go slowly.

Figure — Positional encodings (sinusoidal)

Why sine and cosine and not something else? The topic needs a function that:

  1. never blows up (a raw counter would make swamp everything);
  2. gives smoothly changing, distinguishable values.

A dot on a circle never leaves the circle, so its height and sideways position always stay between and . That boundedness is exactly property (3) the parent demands.

Recall What is the range of

and ? Both always lie in ::: .


5. Radians (the angle unit)

Picture: wrap a string of length around the rim of the unit circle — the angle it spans is exactly radian.

Why the topic needs it: every and in the formula takes its input in radians. In Worked Example 1, means " radians," a bit less than half a turn — that's why is small and is near .


6. Period, frequency , and wavelength

Figure — Positional encodings (sinusoidal)

Picture: two waves drawn over positions . A fast wave (big ) wiggles many times — good for telling neighbours apart. A slow wave (small ) barely moves — good for tracking coarse, long-range position.

Why the topic needs it: the whole trick is giving each embedding dimension a different , from fast to slow, so the vector reads position at many "zoom levels" at once.

Recall If frequency

is large, is the wavelength long or short? Short — a fast wave repeats quickly ::: , so big means small .


7. Powers, exponents, and

Picture: a ruler that is stretched — equal steps in the exponent become ever-bigger jumps in the value. This is a geometric (multiplicative) spacing.

Why the topic needs it: the frequencies are set by As the dimension index climbs, the exponent climbs from up toward , so the denominator climbs from up to , so shrinks from down to . That geometric sweep is how one vector spans "fast neighbour-detector" to "slow document-scale" frequencies. Check: at , so ; at the halfway exponent, so — exactly the numbers in the parent's Worked Example 1.


8. Index and the even/odd split ( and )

Picture: the stack of bars from Figure 1, coloured in pairs — each colour pair is one couple riding the same wave speed.

Why the topic needs it: keeping and of the same side by side is what stores the full 2-D circle address (Section 4). That pairing is what makes shifting position a clean rotation — the key relative-position property, and the seed idea behind Rotary Position Embeddings (RoPE).


9. The rotation matrix (a preview of the payoff)

Picture: the dot on the circle, spun forward by a fixed angle.

Why the topic needs it: stepping from position to multiplies each pair by exactly this matrix with . So "move steps" = "rotate by a fixed angle" — a linear rule the model can learn, giving consistent relative distances. (The parent's Worked Example 2 derives this.)


10. — reading the subscript

Why the topic needs it: it lets the formula name one exact number. Even picks the sine rule; odd picks the cosine rule — the split from Section 8.


How these pieces feed the topic

position pos

PE formula

vector and d_model

sin and cos on the circle

frequency omega and wavelength

powers 10000 base

even odd index i

PE vector length d_model

token embedding

add vectors

position aware input to attention

The parent topic sits at node F; everything on this page is one of the arrows leading in. Note how Token Embeddings enters only at the add step — PE never replaces the word's meaning, it rides alongside it.


Equipment checklist

Cover the right side and check you can answer each before reading the parent note.

  • What does count, and where does it start? ::: Which slot a word occupies; it starts at .
  • What is ? ::: The length (number of slots) of every vector the model uses.
  • How do you add two vectors? ::: Slot by slot; both must be the same length.
  • What are and geometrically? ::: The height and sideways position of a dot on the unit circle after turning angle .
  • What range do and always stay in? ::: .
  • What is a radian, and how many make a full turn? ::: An angle measured by arc length on the unit circle; make a full turn.
  • What links frequency and wavelength ? ::: — big means short .
  • What is and ? ::: and .
  • As dimension index grows, does grow or shrink? ::: It shrinks (waves get slower).
  • Why keep and of the same frequency together? ::: They give the full 2-D circle address, so a position shift becomes a clean rotation.
  • What does mean? ::: Slot of the encoding vector for position .