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 and the following odd coordinate ,
Position is usually counted from 0, but an implementation must state its index origin rather than assume it.
Calculate Four Coordinates
Let . For , the denominator is 1. For , it is . The position vector is therefore
Rounded to six decimal places:
| Position | Coordinate 0 | Coordinate 1 | Coordinate 2 | Coordinate 3 |
|---|---|---|---|---|
| 0 | 0 | 1 | 0 | 1 |
| 1 | 0.841471 | 0.540302 | 0.010000 | 0.999950 |
| 2 | 0.909297 | -0.416147 | 0.019999 | 0.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.
Ready to run.
A Fixed Offset Is a Rotation Within Each Pair
For one frequency, write the pair as
The angle-addition formulas express as a fixed linear transformation of whose coefficients depend on . 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,
The position vector has width 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 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 , what is coordinate 2 of ? Give the answer to at least four decimal places.
Compute it first, then check your number.
Hint
Solution
Reference
- Ashish Vaswani et al., Attention Is All You Need, 2017.