Sinusoidal Position Encodings

Fixed sine-cosine pairs change at several frequencies and add a repeatable vector at each index. Calculate four coordinates, interpret a fixed offset as pairwise rotations, and separate formula range from trained length generalization.

The original Transformer used a fixed position vector built from sine and cosine waves. The vector contains no learned entries. Different coordinate pairs change at different rates, giving each position a repeatable pattern across several frequencies.

For even coordinate 2k2k and the following odd coordinate 2k+12k+1,

PE(pos,2k)=sin(pos100002k/dmodel),PE(pos,2k)=\sin\left(\frac{pos}{10000^{2k/d_{model}}}\right), PE(pos,2k+1)=cos(pos100002k/dmodel).PE(pos,2k+1)=\cos\left(\frac{pos}{10000^{2k/d_{model}}}\right).

Position is usually counted from 0, but an implementation must state its index origin rather than assume it.

Calculate Four Coordinates

Let dmodel=4d_{model}=4. For k=0k=0, the denominator is 1. For k=1k=1, it is 100001/2=10010000^{1/2}=100. The position vector is therefore

PE(pos)=[sin(pos),cos(pos),sin(pos/100),cos(pos/100)].PE(pos)=[\sin(pos),\cos(pos),\sin(pos/100),\cos(pos/100)].

Rounded to six decimal places:

PositionCoordinate 0Coordinate 1Coordinate 2Coordinate 3
00101
10.8414710.5403020.0100000.999950
20.909297-0.4161470.0199990.999800

The first pair turns quickly. The second pair moves slowly over these three positions. At a realistic model width, many pairs span many rates.

Inspect sinusoidal frequencies

Change the positions, model width, or base and inspect each sine-cosine pair.

Command/Ctrl + Enter. Python runs in your browser.

Ready to run.

A Fixed Offset Is a Rotation Within Each Pair

For one frequency, write the pair as

u(pos)=[sin(ωpos),cos(ωpos)].u(pos)=[\sin(\omega pos),\cos(\omega pos)].

The angle-addition formulas express u(pos+Δ)u(pos+\Delta) as a fixed linear transformation of u(pos)u(pos) whose coefficients depend on ωΔ\omega\Delta. Each frequency pair therefore responds to the same positional offset through its own 2D rotation.

This property helps make relative offsets accessible to learned projections. It does not mean a trained network must infer every distance perfectly.

Add the Vector Once at the Input Boundary

In the original input construction,

xi=E[ti]+PE(i).x_i=E[t_i]+PE(i).

The position vector has width dmodeld_{model} and is added before the stack of Transformer blocks. Later layers can transform and combine the resulting features. Re-adding the same vector at every layer would define a different architecture.

Computable Is Not the Same as Generalized

The formula can calculate PE(pos)PE(pos) for an index larger than any index used in training. A learned table cannot index an absent row. This is a real implementation difference, but it does not by itself prove better behavior on longer sequences. The learned model may still rely on training-length patterns, attention distributions, or data statistics that fail outside their observed range.

Q1. Calculate a sinusoidal coordinate

For dmodel=4d_{model}=4, what is coordinate 2 of PE(2)PE(2)? Give the answer to at least four decimal places.

Compute it first, then check your number.

Hint
For coordinate 2, k=1k=1 and the denominator is 100002/4=10010000^{2/4}=100.
Solution
PE(2,2)=sin(2/100)=sin(0.02)0.019999PE(2,2)=\sin(2/100)=\sin(0.02)\approx0.019999.
Not attempted
Review

Not marked done.

Reference

Pause and reflect

In your own words, note what you understood, what remains unclear, or what you want to revisit. The note stays with this lesson.

0 of 1 exercises marked done

Review

Not marked done.