Medium6 min readUpdated 2026-08-12

What is positional encoding, and why is it needed in Transformers?

Positional encoding and Transformers: explain how Transformers represent token order and why that order information is needed. Learn the difference between sinusoidal, learned, and relative encodings and practical tradeoffs for language models.

Hand-drawn card showing positional encoding flow with boxes for tokens, positional vectors, addition, attention, and takeaway
TL;DR
  • Positional encoding explains why Transformers need explicit position signals: attention is order-agnostic by default, so we add position information to embeddings.
  • Common approaches are sinusoidal fixed encodings, learned position embeddings, and relative position methods that modify attention scores.
  • Sinusoidal gives extrapolation and relative comparisons, learned gives flexibility, relative encodings often improve long-range tasks. Key tradeoffs: fixed versus learned expressivity, generalization versus parameter cost, absolute versus relative position handling.

In this question, we will learn what positional encoding is and why it is needed in Transformers. We will see how common schemes work and when each choice helps or hurts model behavior.

We will cover the following:

  • The intuition
  • How it actually works
  • Absolute versus relative positional encodings
  • Implementation notes and a concrete worked example
  • Tradeoffs and failure modes
  • Questions the interviewer might ask

Direct answer: Positional encoding gives each token a signal about its position in the input so the attention mechanism, which does not by itself know order, can use sequence order. In practice we add or combine position vectors with token embeddings and sometimes alter attention scores to capture relative offsets. Different schemes trade generalization, parameter cost, and downstream performance.

The intuition (an analogy that makes it click)

Think of attention as a room of people who can talk to anyone but cannot see the seating arrangement. Token embeddings are what each person knows about their own topic. Positional encoding is the seat number tag we pin on each person so they know where they sit. With seat tags, people can say I am near you, I am before you, or I am at the start.

Without tags, a person could still recognize content but could not tell order. With tags, the same attention mechanism can prefer nearby seats, detect sequence patterns, and learn order-dependent functions like translation or syntax tasks.

How it actually works (the real mechanics, with one concrete worked example)

There are two common ways to provide positions.

  1. Additive position vectors. For each position pospos we create a vector pposp_{pos} and add it to the token embedding xposx_{pos} before attention: xpos=xpos+pposx'_{pos}=x_{pos}+p_{pos}. The pposp_{pos} are either learned parameters or fixed vectors.

  2. Relative position or attention bias. Instead of adding fixed absolute vectors, we modify attention scores so that the score from token at position ii to token at position jj depends on the relative offset jij-i. This can be a learned bias or a function of jij-i.

The classic sinusoidal positional encoding from Vaswani et al uses deterministic sinusoids so different dimensions represent different frequencies. For model dimension dmodeld_{model} and index ii the formulas are:

PEpos,2i=sin(pos100002i/dmodel),PEpos,2i+1=cos(pos100002i/dmodel)PE_{pos,2i} = \sin\left(\frac{pos}{10000^{2i / d_{model}}}\right),\quad PE_{pos,2i+1} = \cos\left(\frac{pos}{10000^{2i / d_{model}}}\right)

Those formulas create components that vary smoothly with pospos and span a range of wavelengths. A small concrete numerical illustration: set dmodel=4d_{model}=4. Then the denominators for the frequency terms are 100000=110000^{0}=1 for dimensions 0 and 1 and 100000.5=10010000^{0.5}=100 for dimensions 2 and 3. That means dimension pair 0/1 encodes high frequency changes, while 2/3 encodes much slower position variation.

aspectsinusoidallearnedrelative bias
parameters per position0+one vector per positionsmall number per relative offset
extrapolation to longer sequencesyesno unless special handlingoften yes for offsets seen during train
captures absolute positionyesyesmainly captures offsets

Absolute versus relative positional encodings

Absolute encodings attach a unique code to each index. They are simple and often work well. Learned absolute embeddings allow the model to shape position signals freely but need parameters proportional to maximum sequence length.

Relative encodings encode differences between positions. They are useful when the model should reason about distances or patterns regardless of absolute location. Relative methods have improved performance on tasks such as language modeling with long contexts and certain translation problems where local structure matters.

Examples of relative schemes include Transformer-XL style segment-level recurrence and Shaw et al. relative biases, and more recent attention formulations that add an offset-dependent bias to the attention score matrix.

Implementation notes and example step-by-step

If you implement a simple encoder block with additive positional encodings:

  1. Compute token embeddings XRL×dmodelX\in\mathbb{R}^{L\times d_{model}} for sequence length LL.
  2. Obtain positional encodings PRL×dmodelP\in\mathbb{R}^{L\times d_{model}} either via sinusoid formula or a learned lookup for each pos{0,...,L1}pos\in\{0,...,L-1\}.
  3. Form X=X+PX'=X+P and pass XX' to multi-head attention.

If you use relative attention biases, modify the attention logits QKTQK^T by adding a bias matrix BB where Bi,j=bjiB_{i,j}=b_{j-i} and bb is learned for a range of offsets. The attention probabilities become softmax(QKT+BQK^T+B).

A short worked numeric example with dmodel=4d_{model}=4 and positions pos=1,2pos=1,2 using sinusoidal terms would compute two 4-d vectors via the formulas above, then add them to the token embeddings before attention.

Tradeoffs and failure modes

Absolute learned embeddings give maximum flexibility but do not generalize to longer sequences than seen in training. Sinusoidal encodings generalize and give the model a smooth notion of position, but they may be less expressive for specialized position patterns. Relative encodings can capture distance-based patterns and often improve long-range modeling, but they complicate attention implementation and can add compute.

If you forget to supply any position signal, the Transformer cannot learn order-sensitive functions reliably. If you use learned absolute embeddings and then evaluate on longer sequences, the model will likely fail at tasks that require understanding positions beyond training length.

Questions the interviewer might ask

Some follow-up questions you might get:

Why do we add rather than concatenate positional encodings? Adding keeps the embedding dimension fixed and allows attention keys and queries to mix token and position information. Concatenation increases model size and changes how projection matrices interact with position information.

What are the benefits of sinusoidal encoding? Sinusoidal encodings have no extra parameters, provide multi-scale position signals, and let models generalize to longer sequences because the functions extend naturally beyond training positions.

When would learned position embeddings be better? When dataset-specific position patterns exist and the maximum sequence length is fixed and reasonable, learned embeddings let the model capture idiosyncratic position signals for that domain.

How does relative positional encoding affect attention computation? It changes attention logits with an offset-dependent bias. That means the softmax prefers certain relative distances and the model can learn distance-sensitive patterns without relying on absolute indices.

Can positional encoding encode hierarchical structure like parse trees? Not directly. Positional encodings give linear order information. To capture hierarchical structure you need additional architectural signals or training objectives that encourage hierarchical representations.

Some things to note:

  • If you plan to extrapolate to longer sequences, prefer sinusoidal or carefully designed relative methods.
  • Relative encodings often improve performance on long-context tasks but add implementation complexity.

What the interviewer is really testing

The interviewer wants to check that you understand Transformers are permutation-invariant in attention and require explicit position signals to perform sequence tasks. They also want to see you can compare common solutions, explain their tradeoffs, and describe implementation consequences like generalization and parameter cost.

Further reading in the curriculum

Go deeper on the fundamentals behind this question.

Related questions

#positional-encoding#transformers#nlp#sequence-modeling

How would you rate the quality of this article?

Prepare for your AI engineering interview

This is one of many detailed questions and explainers on StudyAIDesign. Browse the full set, work through the curriculum, and walk into your interview ready.

Follow along for new questions and explainers:Instagram