Interview Q&A / AI & Generative AI

IntermediateQuick answer1 min readUpdated 3 Oct 2026

Why do transformers need positional encodings?

SHORT ANSWER

Attention treats its input as an unordered set, so without position information "dog bites man" and "man bites dog" look alike. Positional encodings add order information to the token vectors so the model knows where each token sits.

Explain it simply

Imagine tearing a sentence into word cards and dropping them in a bag. Self-attention alone only sees the bag. It knows which words are present and how they relate by content, but not their order.

Order matters: "Priya called Rahul" is different from "Rahul called Priya". So the model adds a position signal to each token. Early transformers added fixed sine and cosine patterns; many models learn position embeddings or use schemes that encode relative distance between tokens inside the attention calculation.

Position methods also affect how well a model handles text longer than it saw during training, which is one reason context extension is a research topic.

Example

text
token embedding("Rahul")  +  position signal(1)   -> input for position 1
token embedding("called") +  position signal(2)   -> input for position 2

Same word at a different position gets a different input vector.

What to say in the interview

"Self-attention is permutation-invariant, so we inject order through absolute or relative positional encodings. Without them the model could not tell word order. The chosen scheme also influences how well it generalises to longer sequences."

#transformers#positional-encoding#attention

Related questions

Practise more AI & Generative AI questions

Test your recall with short answers, then return to the detailed explanations when you need context.

Open AI & Generative AI quick answers →

Get new questions like this by email

One short email when something new and useful lands. No spam, unsubscribe in one tap.

By subscribing you agree to the Privacy Policy.