Take the sinusoidal positional encoding with dmodel=4, ordered as (sin⁡,cos⁡) pairs, PE(pos,2i)=sin⁡ ⁣(pos/100002i/d) and PE(pos,2i+1)=cos⁡ ⁣(pos/100002i/d). What is the vector for position pos=2?

Sequence models and transformers, Positional encoding
Level L2 Elementary. Length Short. Nature Mat Mathematical.

What's being asked

Whether you can evaluate the encoding formula and see that different dimensions use different wavelengths.

Key idea

Pair i has angular frequency ωi=1/100002i/d, so the encoding is (sin⁡ω0pos,cos⁡ω0pos,sin⁡ω1pos,cos⁡ω1pos).

Steps

  1. With d=4 there are two pairs, i=0 and i=1.
  2. For i=0: ω0=100000=1. For i=1: ω1=1/100002/4=1/100=0.01.
  3. At pos=2 the angles are 2⋅1=2 radians and 2⋅0.01=0.02 radians.
  4. PE2=(sin⁡2,cos⁡2,sin⁡0.02,cos⁡0.02)=(0.9093, −0.4161, 0.0200, 0.9998).
  5. The first pair changes quickly with position (wavelength 2π); the second changes slowly (wavelength about 628). Together they act like digits of different speed.

Common mistakes

How long your answer should be

3–4 lines.

Last checked on 4 December 2026.

Read this

Previous in Sequence models and transformers: A scalar RNN has ht=tanh⁡(w ht−1+u xt) with w=0.5, u=1 and h0=0. For inputs x1=1 and x2=−1, what are h1, h2 and the gradient ∂h2/∂h1?
Next in Sequence models and transformers: A transformer layer-norm sees the feature vector x=(1,3,5,7) for one token, with γ=2, β=1 for every feature and ϵ=0. What is the output? How does the statistics axis of layer norm differ from batch norm?