Join Our 5-Week ML/AI Engineer Interview Bootcamp 🚀 led by ML Tech Leads at FAANGs
Implement sinusoidal positional encoding so you can add position information to token embeddings in an NLP model. You’ll generate a matrix where each position has a deterministic encoding based on sine and cosine waves.
The encoding is defined as:
where (pos) is the token index (starting at 0), (i) is the dimension index, and (d) is the model dimension.
Implement the function
Rules:
pos = 0..seq_len-1.sin, odd dimensions use cos.Output:
| Argument | Type |
|---|---|
| d_model | int |
| seq_len | int |
| Return Name | Type |
|---|---|
| value | np.ndarray |
Return NumPy array
Use NumPy only; no torch/tensorflow
Vectorize with broadcasting; avoid nested loops
Create a (seq_len, 1) array of positions with np.arange(seq_len)[:, None] so it broadcasts across dimensions.
Compute the scaling term only for even dimensions: i = np.arange(0, d_model, 2) and use np.exp(-np.log(10000.0) * i / d_model) to match 1 / 10000^(2i/d).
Fill a (seq_len, d_model) matrix by slicing: pe[:, 0::2] = sin(pos * div_term) and pe[:, 1::2] = cos(pos * div_term), then return pe.