What is the difference between sinusoidal positional encodings and learned positional embeddings?
Last updated
Both give each absolute position a vector that is added to the token embedding. Sinusoidal encodings are a fixed, parameter-free function of position. Learned embeddings are a trainable lookup table with one row per position, up to a maximum length.
Sinusoidal (original Transformer):
Computed as
sin/cos(pos / 10000^(2i/d)): no parameters, deterministic.Can produce a vector for any position. In practice, models still degrade past the training length because they never learned to use those positions.
Has useful structure:
PE(pos+k)is a linear function (a rotation) ofPE(pos), which may help the model learn relative offsets.
Learned (BERT, GPT-2):
An
nn.Embedding(max_len, d)trained jointly with the model, so it can fit whatever positional patterns the data needs.Hard cap at
max_len: positions beyond it have no embedding. Rarely seen late positions may also be undertrained.Adds
max_len × dparameters.
In practice, the two perform about the same at in-distribution lengths (Vaswani et al. reported nearly identical results).
Both are absolute schemes. Modern LLMs mostly use relative methods (RoPE, ALiBi) instead.