We can't find the internet
Attempting to reconnect
Something went wrong!
Attempting to reconnect
← Position Encodings step 1 of 5
Implement Positional Encoding
Sinusoidal positional encoding assigns a fixed vector to each sequence position. Adjacent feature pairs use sine and cosine of the same angle. Later pairs change more slowly as the position increases.
Your task
Implement positional_encoding(seq_len, d_model). Both inputs are positive
integers, and d_model is even. Return a floating-point array of shape
(seq_len, d_model). This task generates the encoding table only; it does
not take token embeddings or add the table to them.
Positions start at zero. For each position pos and pair index
i = 0, ..., d_model // 2 - 1, compute:
angle = pos / 10000**(2 * i / d_model)
PE[pos, 2 * i] = sin(angle)
PE[pos, 2 * i + 1] = cos(angle)
Angles are in radians. i identifies a sine/cosine pair, not a column:
columns 2*i and 2*i+1 share one frequency. Use ordinary division in
the exponent and ** for exponentiation in Python.
Example
For seq_len = 2 and d_model = 4, the two pair frequencies are 1 and
1/100. The output is approximately:
[[0.000000, 1.000000, 0.000000, 1.000000],
[0.841471, 0.540302, 0.010000, 0.999950]]
Position zero always alternates zero and one. The first pair at position one uses angle 1, while the second uses angle 0.01. This difference in frequency is what makes the columns vary at different rates.
Build a column of positions and a row of pair frequencies, then broadcast their product to form all angles at once. Interleave sine and cosine in the output rather than putting all sine columns before all cosine columns.
The visualization starts with a sinusoidal table. Rows are positions and columns are features; other tabs illustrate different ways to use positions.
Loading visualization…
Understand the ideas behind this exercise. The Problem tab contains the exact implementation requirements.
Give attention information about token order
Self-attention based only on token content has no absolute position input. Position information can be added to token vectors, added to attention scores, or introduced by transforming queries and keys.
Learned absolute embeddings provide a vector for each position. Sinusoidal encodings use sine and cosine pairs at different frequencies. When adding either to token embeddings, the feature widths must match. In a sinusoidal encoding, position determines the angle while the feature-pair index determines the frequency.
Describe the distance between query and key
Relative-position methods represent the relationship between two positions.
A learned score table, for example, can use their signed distance as an index.
For query position 3 and key position 1, key - query is -2, while
query - key is 2. Use the task's sign convention.
Clipping distances to a fixed range makes all more-distant pairs share an entry at the corresponding end of that range.
Rotate vectors or add a score bias
RoPE rotates pairs of query and key coordinates according to position. Rotations preserve vector lengths. Applying the same frequency scheme to queries and keys makes their dot product depend on the relative rotation between their positions. RoPE transforms the vectors rather than adding an embedding to them.
Some implementations pair adjacent features; others pair corresponding features from the two halves of a vector. Use the specified pairing, because it changes the result.
ALiBi adds a linear distance bias to attention scores, with a slope for each head. It affects preferences among keys without creating a position-embedding vector. Causal masking is still needed separately: a finite negative score bias does not give a future key exactly zero attention.
Check position zero, equal positions, and a small offset. With a cache during generation, use the new token's actual sequence position; restarting at zero on every step gives the wrong positional relationship to cached keys.
Your code, saved here
Sign in to access your accepted solutions and saved draft history. You can keep reading the lesson without an account.
Sign in to your accountStuck?
JAX reference solution
Sign in to track your attempts toward unlocking the reference solution.