Effect of Positional Encoding (PE)
Transformers process tokens in parallel, so unlike RNNs/CNNs they have no
inherent notion of order. Positional encoding injects sequence order
information so the model can reason about who comes before whom.
What happens without positional encoding?
Permutation invariance: Self-attention treats inputs as a set, not a
sequence.
Sentences like
“the dog bit the man” vs “the man bit the dog”
become indistinguishable if tokens are the same but reordered.
The model can learn content similarity but not order-dependent
meaning.
Tasks that rely on sequence order (language, time series, trajectories)
break.
What positional encoding adds
Positional encoding adds a position-dependent vector to each token
embedding:
x t =et + pt
This enables the model to:
Distinguish absolute position (start vs end of sequence)
Infer relative position (token A is before token B)
Learn directionality and ordering patterns
Common types of positional encoding
1. Sinusoidal (fixed)
Uses sine/cosine waves at different frequencies
Deterministic, no extra parameters
Enables extrapolation to longer sequences
Original Transformer choice
2. Learned positional embeddings
Trainable vectors per position
Often better empirical performance
Limited to max training length
3. Relative positional encoding
Encodes distance between tokens instead of absolute index
Improves generalization for long contexts
Common in modern LLMs and robotics/planning models
Intuition (interview-friendly)
Positional encoding breaks the permutation symmetry of self-attention,
allowing the Transformer to model ordered structure instead of treating
tokens as an unordered set.
Why this matters in practice
Language modeling: grammar, syntax, causality
Time series: trends, delays, temporal dependencies
Trajectory & behavior modeling (e.g., robotics, autonomy): motion
history, intent prediction
Without PE → attention answers “what is similar?”
With PE → attention answers “what is similar and where/when?”
One-liner for interviews
Self-attention alone is permutation-invariant; positional encoding injects
order information so Transformers can model sequences instead of bags of
tokens.
If you want, I can also:
Compare absolute vs relative PE in attention equations
Show why sin/cos lets the model infer relative distance
Connect this directly to trajectory prediction or behavior planning
(very relevant for Nuro)