03/08Positional Encoding
Implement the fixed sinusoidal positional encoding from the Transformer.
PE(pos,2i)=sin(100002i/dmodelpos) PE(pos,2i+1)=cos(100002i/dmodelpos)Here, pos is a zero-based sequence position, i indexes a sine-cosine pair, and d_model is the model width. Return a NumPy float64 array with shape (seq_length, d_model).
Examples
Input: seq_length = 4, d_model = 6
Output: [[0.0, 1.0, 0.0, 1.0, 0.0, 1.0], [0.8415, 0.5403, 0.0464, 0.9989, 0.0022, 1.0], [0.9093, -0.4161, 0.0927, 0.9957, 0.0043, 1.0], [0.1411, -0.99, 0.1388, 0.9903, 0.0065, 1.0]]
Explanation: Row 0 is all sin(0)=0 and cos(0)=1. Each subsequent row encodes a new position with higher-frequency oscillations in earlier columns and lower-frequency oscillations in later columns.
Input: seq_length = 3, d_model = 4
Output: [[0.0, 1.0, 0.0, 1.0], [0.8415, 0.5403, 0.01, 1.0], [0.9093, -0.4161, 0.02, 0.9998]]
Input: seq_length = 2, d_model = 8
Output: [[0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0], [0.8415, 0.5403, 0.0998, 0.995, 0.01, 1.0, 0.001, 1.0]]
Hint 1
Create positions with shape (seq_length, 1).
Hint 2
Compute one inverse frequency for each even column.
Hint 3
Assign sine and cosine results with 0::2 and 1::2 slices.
Requirements
- Use NumPy float64 computations.
- Place sine values in even columns and cosine values in odd columns.
- Use base 10000 and the specified exponent.
- Return the unrounded encoding matrix.
Constraints
- seq_length is positive.
- d_model is a positive even integer.
- Valid inputs are guaranteed.
Accepts: number
Accepts: number
03/08Positional Encoding
Implement the fixed sinusoidal positional encoding from the Transformer.
PE(pos,2i)=sin(100002i/dmodelpos) PE(pos,2i+1)=cos(100002i/dmodelpos)Here, pos is a zero-based sequence position, i indexes a sine-cosine pair, and d_model is the model width. Return a NumPy float64 array with shape (seq_length, d_model).
Examples
Input: seq_length = 4, d_model = 6
Output: [[0.0, 1.0, 0.0, 1.0, 0.0, 1.0], [0.8415, 0.5403, 0.0464, 0.9989, 0.0022, 1.0], [0.9093, -0.4161, 0.0927, 0.9957, 0.0043, 1.0], [0.1411, -0.99, 0.1388, 0.9903, 0.0065, 1.0]]
Explanation: Row 0 is all sin(0)=0 and cos(0)=1. Each subsequent row encodes a new position with higher-frequency oscillations in earlier columns and lower-frequency oscillations in later columns.
Input: seq_length = 3, d_model = 4
Output: [[0.0, 1.0, 0.0, 1.0], [0.8415, 0.5403, 0.01, 1.0], [0.9093, -0.4161, 0.02, 0.9998]]
Input: seq_length = 2, d_model = 8
Output: [[0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0], [0.8415, 0.5403, 0.0998, 0.995, 0.01, 1.0, 0.001, 1.0]]
Hint 1
Create positions with shape (seq_length, 1).
Hint 2
Compute one inverse frequency for each even column.
Hint 3
Assign sine and cosine results with 0::2 and 1::2 slices.
Requirements
- Use NumPy float64 computations.
- Place sine values in even columns and cosine values in odd columns.
- Use base 10000 and the specified exponent.
- Return the unrounded encoding matrix.
Constraints
- seq_length is positive.
- d_model is a positive even integer.
- Valid inputs are guaranteed.
Accepts: number
Accepts: number