Back to Home
Inside a Transformer
🟡 Intermediate · 6–8 min

Word Order & Positional Encoding

The same words in a different order can mean something completely different. A transformer needs to know where each word sits, that's what positional encoding is for.

Overview

Order carries meaning

A transformer looks at all the tokens together, but on their own the tokens don't say who did what. "Dog bites man" and "Man bites dog" use the exact same words, yet they mean opposite things. The only difference is the order.

Playground

Swap the words, watch the meaning flip

The positions stay the same (1, 2, 3). Only the words move. Press the button and see how the meaning changes.

The words are the same, but the meaning changes because the order changes.

What is positional encoding?

Adding a "where" to every token

Positional encoding is extra position information added to each token. It helps the transformer know which token came first, second, third, and so on.

🔤
Token meaning
what the word is
+
📍
Position info
where the word is
=
Better input
the transformer understands
Comparison

With vs without position

Without position
Dogbitesman

Loose words. The order is unclear.

With position
1 Dog
2 bites
3 man

Ordered sentence. The meaning is clearer.

🔑
Key takeaways
  • • Word order changes meaning.
  • • Tokens alone are not enough.
  • • Transformers need position information.
  • • Positional encoding adds order information.
  • • This helps the model understand the sentence better.