Topic 08 of 62 · Inside the Transformer block
Positional encoding
EasySelf-attention ignores word order. How did the original Transformer tell the model where each token sits?
1 · Start with the intuition
Attention builds each output from weighted sums over the other tokens, and nothing in that calculation depends on where a token sits. Shuffle the input and the outputs shuffle in the same way, so “dog bites man” and “man bites dog” would look like the same collection of words. The original fix is simple: before the first layer, add a position vector to each token embedding. The paper builds those vectors from sine and cosine waves of many different wavelengths, so every position gets its own pattern of values.
2 · How the mechanism works
Mechanism step 1 of 3 · no question to answer
Build one vector per position
For each position, fill a vector of width d_model with sines in the even slots and cosines in the odd slots, using slower waves for later slots.
Next, we will look at: Add it to the embedding. Move on when this operation makes sense.
Visualizing the behavior
Position is added to the embedding before the first layer
Sinusoidal position vectors for d_model = 4
Derived| position ↓ · slot → | sin, fast | cos, fast | sin, slow | cos, slow |
|---|---|---|---|---|
| pos 0 | 0.00 | 1.00 | 0.00 | 1.00 |
| pos 1 | 0.84 | 0.54 | 0.01 | 1.00 |
| pos 2 | 0.91 | -0.42 | 0.02 | 1.00 |
| pos 3 | 0.14 | -0.99 | 0.03 | 1.00 |
3 · The governing equation
PE(pos, 2i) = sin(pos / 10000^(2i / d_model)) PE(pos, 2i+1) = cos(pos / 10000^(2i / d_model)) x_pos = embedding(token) + PE(pos)
Notation: pos is the position index starting at 0, and i counts the sine–cosine pairs from 0 to d_model/2 − 1. Wavelengths grow geometrically from 2π to 10000 · 2π.
These encodings are fixed: they are computed, not trained. The paper’s stated hypothesis was that they make it easy to attend by relative position, because for a fixed offset k, PE(pos + k) is a linear function of PE(pos). The authors also tried learned position embeddings and reported nearly identical results, choosing sinusoids because they might extend to sequences longer than those seen in training. Many current LLMs use other schemes such as RoPE, which acts inside attention instead of at the input.
4 · Put concrete numbers on it
Worked numbers (from stated assumptions)5 · Common misconception to avoid
Saying the position vector is concatenated to the embedding, or that the original encodings are learned. They are added, and in the original model they are fixed.
6 · Self-check & interview synthesis
Explain it in your own words first
Before revealing the reference answer, try answering the opening question aloud in four beats: what it is → how the math works → what trade-off it makes → a concrete number or failure mode.
Follow-up question interviewers often ask next
MediumHow does RoPE differ from this scheme?
Show follow-up reasoning
RoPE does not add anything to the input. It rotates the query and key vectors inside every attention layer by an angle that depends on position, so the attention score depends on the distance between two tokens.