Bring the mental model from Scaled Dot-Product Attention & Transformer Layers; this page will reuse it instead of restarting from zero.
Attention & Transformers
Positional Encoding
How transformers represent order: sinusoidal encodings, learned embeddings, and relative-position methods like RoPE.
Concept Structure
Positional Encoding
Start with the picture, metaphor, or geometric mechanism.
Make the objects explicit and connect them with notation.
Mirror the equations with runnable implementation details.
Manipulate the mechanism and watch the idea respond.
Learning map
Positional EncodingConceptual Bridge
What should feel connected as you move through this page.
How transformers represent order: sinusoidal encodings, learned embeddings, and relative-position methods like RoPE.
The next edge should feel earned: use the demo prediction here before following Rotary Position Embeddings (RoPE).
01
Intuition
Build the mental picture first so the rest of the page has something to attach to.
Self-attention compares tokens by content (queries vs keys). But content alone does not tell you where a token is in the sequence.
If you shuffle the tokens in a sequence and keep their embeddings the same, plain unmasked self-attention has no built-in way to notice the shuffle. In other words, attention is permutation-equivariant unless we inject order information through positions or through an asymmetric mask such as a causal mask.
Positional encodings are the mechanism that turns "a bag of token vectors" into "an ordered sequence". There are many variants:
- Absolute position (sinusoidal or learned): add a position vector to each token.
- Relative position (RoPE, ALiBi, etc.): make attention scores depend on relative offsets.
RoPE is one modern relative-position method, but it is easier to appreciate once you understand the basic goal: the model needs a stable coordinate system for time/position.
02
Math
Translate the story into symbols, assumptions, and a derivation you can inspect.
The classic sinusoidal encoding (Vaswani et al.) defines, for model dimension and position :
with frequencies
Two useful facts:
- Shift structure: the dot product between two pure position vectors depends only on their offset:
- Additive absolute encoding: the simplest way to use PE is to add it to token embeddings:
Relative-position methods (like RoPE) instead modify the attention computation so that depends on directly.
The caveat is important: once position vectors are added to token embeddings, the full attention score contains content-content, content-position, and position-position terms. The identity above explains the position-only signal, not every term in the final attention logit.
03
Code
Keep the implementation aligned with the notation so the algorithm is legible.
import numpy as np
def sinusoidal_pe(T, d, base=10000.0):
assert d % 2 == 0
pos = np.arange(T)[:, None]
i = np.arange(d // 2)[None, :]
w = base ** (-2 * i / d)
angles = pos * w
pe = np.zeros((T, d))
pe[:, 0::2] = np.sin(angles)
pe[:, 1::2] = np.cos(angles)
return pe
pe = sinusoidal_pe(T=32, d=16)
dot = lambda p, q: float(pe[p] @ pe[q])
for delta in [0, 1, 5, 10]:
a = dot(0, delta)
b = dot(7, 7 + delta) # same relative offset
print("delta =", delta, "dot =", round(a, 3), "dot (shifted) =", round(b, 3))
04
Interactive Demo
Use direct manipulation to connect the explanation to a moving system.
Use the demo to predict the shift invariant before seeing the answer. Choose two positions and , shift both by the same amount, and ask what should happen to the pure sinusoidal position dot product.
The point is deliberately narrow: for a sine/cosine dimension pair, the position-only dot product follows the relative gap . The reveal then bridges to RoPE, where the same phase-gap intuition appears inside query-key attention rather than as an added vector.
Live Concept Demo
Explore Positional Encoding
The stage is code-native and interactive. Use it to test the explanation against the mechanism.
Manipulate one control and predict the visible change.
Commit to what Positional Encoding should make visible before reading the result.
After The First Pass
Turn the concept into an inspected object.
Once the invariant is visible in the intuition, math, code, and demo, use these panels to inspect the mechanism visually, check source support, practice the idea, and attach a grounded research question.
Mechanism Storyboard
See the idea move before the page explains it
How transformers represent order: sinusoidal encodings, learned embeddings, and relative-position methods like RoPE.
Start with the picture, metaphor, or geometric mechanism.
Before reading further, choose the kind of change Positional Encoding should make visible.
Visual Inquiry
Make the image answer a mathematical question
How transformers represent order: sinusoidal encodings, learned embeddings, and relative-position methods like RoPE.
Which visible object should carry the first intuition?
Pick the cue that should make Positional Encoding easier to reason about before the page gives the answer.
Source Grounding
Canonical references for the mechanism on this page.
Defines the sinusoidal positional encoding used in the original Transformer and motivates it through relative-offset structure.
Open sourceClaim Review
How transformers represent order: sinusoidal encodings, learned embeddings, and relative-position methods like RoPE.
Claims without a substantive review badge still need exact source-support review.
vaswani-2017-attention
Use equation, code, and demo objects to check whether the source support is operational.
Vaswani et al. define the sine/cosine PE formula, add PEs to token embeddings, and motivate sinusoidal PEs through fixed-offset linear structure. The dot-product identity is local math from that formula.
Sources: Attention Is All You NeedFull attention logits after adding token embeddings include content-position cross terms, not only p-q. Causal/asymmetric masks can also inject order-dependent structure.A bounded review summary is present; still check caveats and exact source scope.Checked Vaswani et al. section 3.5: it defines sine/cosine formulas at alternating dimensions, says each dimension is a sinusoid, gives geometrically spaced wavelengths, and motivates fixed-offset structure because PE(pos+k) can be represented linearly from PE(pos). The dot-product offset identity follows by sin a sin b + cos a cos b = cos(a-b) on each pair. Caveat preserves that full logits include token, cross, and mask terms.
Reviewer: codex; reviewed 2026-06-26Practice Loop
Try the idea before it explains itself
How transformers represent order: sinusoidal encodings, learned embeddings, and relative-position methods like RoPE.
Before touching the demo, predict one visible change that should happen in Positional Encoding.
Reveal when your model needs a nudge.
Reveal when your model needs a nudge.
Reveal when your model needs a nudge.
A concrete answer is on the canvas.
The answer names why the claim should hold.
It touches the page context or a neighboring idea.
Research Room
Attach the question to an exact object
Pick the concept, equation, source, code witness, claim, misconception, or demo state before asking for help. The handoff stays grounded to that object.Open the draft below to save one note and next action in this browser.
Positional Encoding
What is the smallest example that makes Positional Encoding click without losing the math?
Local action draftNo local draft saved yetExpand only when ready to capture one local next action
This draft stays locally in this browser for concept:attention-transformers/positional-encoding.
- Source ids to inspect: vaswani-2017-attention
- Definition, prerequisite, and contrast concept links
- The equation or code witness that makes the concept operational
- One demo state that shows the invariant instead of a slogan
- The learner can state the mechanism in their own words
- The learner can name the prerequisite that would repair confusion
- The learner can predict how the mechanism changes under one perturbation
I am working in Continuous Function's research reading room. Object: concept - Positional Encoding Object key: concept:attention-transformers/positional-encoding Context: Attention & Transformers Anchor id: concept/concept-notebook/attention-transformers/positional-encoding Open question: What is the smallest example that makes Positional Encoding click without losing the math? Evidence to inspect: - Source ids to inspect: vaswani-2017-attention - Definition, prerequisite, and contrast concept links - The equation or code witness that makes the concept operational - One demo state that shows the invariant instead of a slogan What would resolve this: - The learner can state the mechanism in their own words - The learner can name the prerequisite that would repair confusion - The learner can predict how the mechanism changes under one perturbation Answer as a careful research tutor: stay source-grounded, separate verified evidence from assumptions, name the relevant math objects, and end with one next action.
concept/concept-notebook/attention-transformers/positional-encoding
concept:attention-transformers/positional-encoding