Explain transformer positional encoding using sine and cosine functions. Show that for any fixed offset, the encoding can be represented as a linear function via rotation matrix property.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
This is a great question about transformer positional encoding. Let me explain this systematically.
Transformer Positional Encoding
1. Motivation
In transformers, unlike RNNs, there's no inherent notion of sequence order because attention is computed in parallel over all positions. We need to encode position information into the embeddings.
2. The Sine-Cosine Positional Encoding
The standard positional encoding proposed by Vaswani et al. (2017) uses:
d_model is the total dimensionality of the positional encoding
Let me rewrite this more clearly. If we denote:
θd=100002d/d
Then for position pos and dimension index pair (d), we have:
Even dimension (2d): PEpos,2d=sin(pos⋅θd
3. Matrix Rotation Property
Now, the key insight is that for any fixed offset Δ (like going from position pos to position pos+Δ), the positional encodings can be related through a rotation matrix.
Let's consider a 2D subspace corresponding to dimensions 2d and 2d+1:
PEpos=(
PEpos+Δ=
Using angle addition formulas, I can express the encoding at position pos+Δ as a rotation of the encoding at position pos. The sine and cosine of the sum decompose into products involving both the original position and the offset, which yields a rotation matrix applied to the original positional vector. This means the relative positional information is encoded as a pure rotation in the embedding space. (\alpha + \beta) = \sin(\alpha)\cos(\beta) + \cos(\alpha)\sin(\beta)\cos(\alpha + \beta) = \cos(\alpha)\cos(\beta) - \sin(\alpha)\sin(\beta)$$
Applying these with α=pos⋅θd and β=Δ⋅, I can express the positional encoding at position in terms of the original position and the offset. The sine component becomes , while the cosine component is .
This naturally factors into a rotation matrix applied to the original encoding:
Verifying the matrix multiplication confirms this works: the first row yields sin((pos+Δ)⋅θd) and the second row gives .
This is precisely a 2D rotation matrix by angle Δ⋅θd. For the full model with multiple dimensions, each pair of coordinates gets rotated by its own angle Δ⋅θd, so the complete transformation becomes a block diagonal structure of rotation matrices applied to the original positional encoding.
However, the interleaving of sine and cosine values in the actual encoding complicates the matrix representation. If I carefully track the vector structure where positions alternate between sine and cosine for each frequency—sin(pos⋅θ0), cos(pos⋅θ, , , and so on—the transformation becomes more intricate than a simple block diagonal application.
The shift by Δ positions can be expressed as a matrix operation where each pair of sine-cosine components undergoes a 2D rotation by angle Δ⋅θi. This means the encoding at position pos+Δ relates to the encoding at position through a series of composed rotations, one for each frequency band. The key insight is that the offset alone determines how much each frequency component rotates, making relative positions the fundamental determinant of the transformation rather than absolute positions.
Since rotations are distance-preserving operations, the geometric relationships between encoded positions remain invariant. The model leverages this by assigning different rotation rates to different frequency bands—lower frequencies rotate slowly across positions while higher frequencies cycle rapidly—enabling it to simultaneously capture both local and distant contextual relationships.