Quant Memo
Core

Learned Positional Embeddings

Instead of hand-designing a wave pattern to represent position, a model can simply learn a separate trainable vector for every possible position, letting gradient descent discover whatever positional signal turns out to be useful.

Prerequisites: Sinusoidal Positional Encoding, Queries, Keys and Values

Sinusoidal positional encoding hand-designs a fixed pattern of waves to represent position, chosen by the engineers before training ever starts, on the theory that this particular mathematical pattern is a good one for a model to build on. But nothing guarantees the sine/cosine pattern is actually the best way to represent position for a given task — it's a reasonable, useful guess, not a proof of optimality. The alternative is to stop guessing: treat position the same way a token's identity is treated, as an index into a lookup table of vectors, and let the training process itself discover whatever positional representation minimizes the loss.

The analogy: assigned seats versus seats you personalize yourself

Sinusoidal encoding is like a venue that stamps every seat with a fixed, pre-designed pattern of colored lights before anyone arrives — reasonable and systematic, but decided in advance with no feedback from how people use the seats. Learned positional embeddings are like giving every seat its own blank nameplate that gets engraved gradually, based on how that seat is actually used — seat 1 might end up engraved very differently from seat 50, in whatever way experience shows is helpful, rather than a pattern chosen beforehand.

early positions late positions (near L_max)
Position vectors near the start of the table get abundant gradient signal in training; positions near $L_{\max}$, rarely seen in real sequences, stay close to random initialization.

The mechanics: a trainable lookup table indexed by position

A learned positional embedding is a matrix PRLmax×dP \in \mathbb{R}^{L_{\max} \times d}, where LmaxL_{\max} is the maximum sequence length the model supports and dd is the embedding dimension — one full trainable row per position, exactly analogous to a token embedding table but indexed by position instead of word identity:

xi=ei+Pix_i = e_i + P_i

In words: the input to the model at position ii is the token's content embedding eie_i plus that position's learned vector PiP_i, and both ee and PP are ordinary trainable parameters updated by gradient descent along with every other weight in the network — there is no hand-designed formula involved, only initialization (usually random) and whatever the training objective pushes the vectors toward. Because PP has exactly LmaxL_{\max} rows, a model trained this way has no defined behavior for any position beyond LmaxL_{\max} — unlike the sinusoidal formula, which is defined for any position at all, a learned table simply has no row to look up past the length it was built for.

Worked example 1: what gets learned, roughly

Suppose a model is trained on movie reviews averaging 40 words, with Lmax=512L_{\max} = 512. Position vectors P1P_1 through P40P_{40} get abundant gradient signal (nearly every example touches them), while P200P_{200} through P512P_{512} get almost none, since few training examples are that long. Those rarely-seen late positions often end up close to their random initialization, poorly trained, and a model processing a genuinely 500-word input at inference may perform noticeably worse there — a direct consequence of position embeddings being learned from data rather than fixed by formula.

Worked example 2: comparing parameter cost

A model with d=768d = 768 and Lmax=512L_{\max} = 512 has a positional embedding table of 512×768=393,216512 \times 768 = 393{,}216 trainable parameters, scaling linearly with maximum length; doubling LmaxL_{\max} to 1024 doubles the table to 786,432. Sinusoidal encoding, by contrast, costs zero trainable parameters regardless of LmaxL_{\max}, since it's computed by formula rather than stored — a real, if usually minor, tradeoff between the two approaches.

Function explorer
-224.4
x = 1.00f(x) = 1.000

Parameter count in a learned table grows linearly with LmaxL_{\max} (drag the exponent toward 1 on this curve to see that straight-line growth) — a cost sinusoidal encoding never pays, since it needs no stored table at all.

What this means in practice

Learned positional embeddings were the standard choice in models like BERT and GPT-2, and they can outperform sinusoidal encoding on the length range seen during training, since the model gets to specialize its position representation to the actual data distribution rather than a generic formula. Their inability to extrapolate cleanly past LmaxL_{\max} is exactly the gap that motivated relative-position schemes like ALiBi and Relative Position Bias and Rotary Position Embeddings (RoPE), both of which aim to keep learned-style flexibility while still generalizing to longer sequences than seen in training.

Learned positional embeddings replace a hand-designed formula with a trainable lookup table, one vector per position, updated by gradient descent like any other parameter — more flexible and often more accurate within the trained length range, but with no defined behavior beyond the maximum length the table was built for, unlike formula-based sinusoidal encoding.

Practice

  1. For d=512d=512, Lmax=1024L_{\max}=1024, compute the number of parameters in the positional embedding table.
  2. Explain why position embeddings for late positions in a long-max-length model can end up poorly trained.
  3. Give one concrete reason a production system serving inputs of highly variable length might prefer sinusoidal encoding over a learned table.

The common confusion is assuming a learned positional embedding table can simply be "extended" at inference time to handle sequences longer than LmaxL_{\max} by padding in new rows. There is nothing meaningful to pad in — those rows were never trained, so any values placed there (zeros, random, or copies of nearby rows) are guesses with no learned relationship to the rest of the model's parameters, and typically produce a sharp, unpredictable quality drop rather than a graceful decline. If a use case genuinely needs variable or unbounded length, that requirement should be decided before choosing a positional scheme, not patched in afterward.

Related concepts

Practice in interviews

Further reading

  • Devlin et al., BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding (2019)
  • Gehring et al., Convolutional Sequence to Sequence Learning (2017)
ShareTwitterLinkedIn