Core

Positional encoding I: sinusoidal & learned

Because attention alone is permutation-equivariant, Transformers need an explicit signal that distinguishes position and order — the 2017 answers were sinusoidal stamps and learned tables.

Updated

01 · Concept

Concept

Everything built so far in this track has a blind spot, and it is easy to state concretely. Feed the attention stack the token vectors for “dog bites person” and then feed it the same three vectors reordered as “person bites dog.” Attention compares vectors by content: every query–key dot product between the same pair of vectors is identical in both cases, so the outputs are the same vectors, merely reordered the same way the inputs were. This permutation equivariance means the model literally cannot tell who bit whom. (The causal mask from lesson 4.4 restricts which pairs interact, which breaks the symmetry only partially — it says who is earlier, not how far apart or where.) Language needs order; attention alone does not see it.

Try the obvious first fix: append the position as one extra number, so token at position 7 gets a feature with value 7. It fails twice. The single scalar must survive projections designed for thousands of content features, so it is easily drowned out; and its magnitude grows without bound — position 100,000 has a feature 100,000 times larger than position 1, wrecking the carefully scaled statistics from lesson 4.3. The correction the 2017 paper chose: give position a full-width vector with bounded entries and structure at many scales, and add it to the token embedding before the first layer:

xt=etokent+pt.x_t = e_{token_t}+p_t.

Since both vectors have width dmodeld_{model}, position and content share the residual stream, and every projection in every head (lesson 4.5) can use combinations of both.

The sinusoidal scheme defines that vector by formula, pairing dimensions at geometrically spaced frequencies:

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

A tiny worked example with dmodel=4d_{model}=4 makes it tangible. For position pos=1pos = 1: dimension pair i=0i=0 uses frequency 1/100000=11/10000^{0} = 1, giving PE(1,0)=sin(1)0.84PE(1,0)=\sin(1)\approx 0.84 and PE(1,1)=cos(1)0.54PE(1,1)=\cos(1)\approx 0.54. Pair i=1i=1 uses frequency 1/100002/4=1/1001/10000^{2/4} = 1/100, giving PE(1,2)=sin(0.01)0.01PE(1,2)=\sin(0.01)\approx 0.01 and PE(1,3)=cos(0.01)1.00PE(1,3)=\cos(0.01)\approx 1.00. The fast pair has already swung noticeably at position 1; the slow pair has barely moved and will take hundreds of positions to change appreciably. Position 2 doubles each angle (sin(2)0.91\sin(2)\approx 0.91, sin(0.02)0.02\sin(0.02)\approx 0.02), and so on: every position gets a distinct multi-speed clock reading with all entries bounded in [1,1][-1,1]. Nearby positions have similar stamps; distant ones differ in the slow dimensions. Relative offsets are linearly recoverable from sine–cosine pairs, a property that quietly foreshadows the next lesson.

A learned absolute position embedding replaces the formula with a trainable table: one row per supported position, looked up exactly like a token embedding. BERT and the GPT lineage used this successfully — optimization discovers whatever positional geometry helps the training distribution, rather than accepting a predetermined one.

The tradeoff is not “principled versus learned.” A learned table has a declared maximum length and simply has no row beyond it; extending means resizing and interpolating rows that were never trained. The sinusoidal formula produces a value for any integer position — but a defined value is not a useful one.

Two operational details bite in practice. Addition does not force the network to keep content and position separable — they are entangled in one vector from layer one, and every head’s Q, K, and V inherit the mixture. And position indices are a contract: left-padding, packed documents, and boundary resets all change which integer a token receives, so a mismatch between training-time and inference-time numbering degrades output even when every shape is valid.

A useful way to compare schemes is to ask where position enters. Absolute schemes — both flavors in this lesson — stamp it on the input, tying information to an index (“this is token 37”) rather than to a distance (“this token is three back”), and leaving the network to infer offsets from two absolute stamps. Relative schemes move position into the attention computation itself, where offsets live naturally.

That is exactly the road the field took, and our course model is the evidence: Qwen3.8-27B uses neither of this lesson’s schemes. No sinusoidal stamp is added to its embeddings and no learned absolute position table exists in its weights; position enters through rotations applied to queries and keys inside its attention layers — RoPE, with a twist of partial application and multimodal sections. That mechanism is the next lesson. Treat sinusoidal and learned tables as the historical baseline that defined the problem — breaking permutation symmetry with a bounded, multi-scale signal — whose best-known solution now lives somewhere else in the block.

02 · Analogy

Analogy

Imagine loose dialogue cards from a play. The words identify what each actor says, but shuffling the cards changes the story. Sinusoidal encoding stamps every card with several clock hands turning at different speeds; nearby cards have related stamps, and long cycles distinguish broader locations. Learned embeddings instead give every seat in the script its own editable label. In both cases, content plus a position mark restores order.

03 · Teach it back

Teach it back

Explain why self-attention needs position information and compare sinusoidal with learned absolute position embeddings.

Minimum: 80 characters and 15 words. Your writing stays only in this browser.

Waiting for your explanation.

Compare with a model answer

Without position information, permuting token vectors permutes the attention outputs in the same way, so the layer cannot distinguish sequences that contain the same tokens in different orders. Sinusoidal encodings deterministically assign multiple sine and cosine frequencies to every position and can be computed beyond trained positions, though extrapolation is not guaranteed to behave well. Learned absolute embeddings optimize one vector per supported position but require a fixed-size table and provide no built-in rule outside it. Both add a position vector to the token embedding; modern models like Qwen3.8-27B use neither, encoding position inside attention with rotations instead.

04 · Check your understanding

Check your understanding

01What order property does content-only self-attention lack?
Answer and explanation

It cannot distinguish permutations without a positional signal — Attention applied to an unordered set of token vectors is permutation-equivariant; position information breaks that symmetry.

02If position vectors are added to embeddings, which attention tensors end up carrying position information (lesson 4.5)?
Answer and explanation

Queries, keys, and values, since all head projections read the same residual stream — Every head's Q, K, and V projection reads the residual stream where the position vector was added, so position leaks into all three — one reason later schemes chose to rotate only queries and keys instead.

03What is one structural difference between sinusoidal and learned absolute encodings?
Answer and explanation

Sinusoidal values follow a fixed formula; learned values come from a trained table — The sinusoidal scheme has no position-table parameters, while learned absolute embeddings optimize a vector for each indexed position.

Complete the teach-back and answer the quiz correctly to finish this lesson.

◎ · Evidence marker

Sources

  1. Ashish Vaswani et al. (2017). Attention Is All You Need.
  2. Jacob Devlin, Ming-Wei Chang, Kenton Lee, and Kristina Toutanova (2019). BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding.
  3. Qwen Team (2026). Qwen3.8-27B Model Card.