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.
The mechanics: a trainable lookup table indexed by position
A learned positional embedding is a matrix , where is the maximum sequence length the model supports and 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:
In words: the input to the model at position is the token's content embedding plus that position's learned vector , and both and 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 has exactly rows, a model trained this way has no defined behavior for any position beyond — 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 . Position vectors through get abundant gradient signal (nearly every example touches them), while through 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 and has a positional embedding table of trainable parameters, scaling linearly with maximum length; doubling to 1024 doubles the table to 786,432. Sinusoidal encoding, by contrast, costs zero trainable parameters regardless of , since it's computed by formula rather than stored — a real, if usually minor, tradeoff between the two approaches.
Parameter count in a learned table grows linearly with (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 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
- For , , compute the number of parameters in the positional embedding table.
- Explain why position embeddings for late positions in a long-max-length model can end up poorly trained.
- 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 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)