๐Ÿงญ Roadmap
Difficultyโญโญโญ / 5 โฑ Read time45-90 minutes attentionpositional-encoding

๐Ÿ“ Positional Encoding

Attention is powerful, but there is a problem:

attention by itself does not inherently know token order.

If you only compare token representations, the model can know:

  • which tokens are relevant
  • which tokens are similar

But it may not know:

  • which token came first
  • which token came later
  • how far apart tokens are

Why order matters

These sentences use many of the same words but mean different things:

  • "Dog bites man"
  • "Man bites dog"

Without positional information, word relationships become ambiguous.

Order carries meaning.

Why this became important in transformers

RNNs process tokens step by step, so order is built into the computation.

Transformers use attention and parallel processing, so they need a separate way to represent position.

That is where positional encoding comes in.

Big intuition

Positional encoding adds location information to token representations.

So a token is not represented as only:

  • what it is

but also:

  • where it is in the sequence

Conceptual view

Instead of:

token_embedding

the model uses something like:

token_embedding + position_information

That helps the model distinguish:

  • early tokens
  • later tokens
  • nearby relationships
  • long-distance structure

Why this helps attention

Now attention can use both:

  • token meaning
  • token position

So the model can learn patterns like:

  • nearby modifiers
  • long-range references
  • sentence structure
  • relative ordering

Simple example

Sentence:

"The cat sat on the mat."

If the model knows only token meaning, "cat" and "mat" are just tokens with embeddings.

If it also knows position, it can reason about:

  • where "cat" appears
  • where "sat" appears
  • how "on the mat" relates to the rest of the sentence

What comes next

Now you have the main building blocks:

  • attention
  • self-attention
  • QKV
  • multi-head attention
  • positional encoding

These all come together in:

Related links

#attention #positional-encoding #transformer #foundations