Complete the code to create a positional encoding tensor of zeros with shape (max_len, d_model).
pos_encoding = torch.zeros([1], d_model)We create a positional encoding tensor with shape (max_len, d_model) to hold positional information for each position up to max_len.
Complete the code to compute the position indices tensor for max_len positions.
position = torch.arange([1]).unsqueeze(1)
We use torch.arange(max_len) to get position indices from 0 to max_len-1, then unsqueeze to make it a column vector.
Fix the error in the code to compute the angle rates for positional encoding.
angle_rates = 1 / torch.pow(10000, (2 * ([1] // 2)) / d_model)
The variable i represents the dimension index used to calculate the angle rates for each dimension.
Fill both blanks to assign sine to even indices and cosine to odd indices in the positional encoding.
pos_encoding[:, [1]] = torch.sin(angle_rads[:, [2]])
Even indices start at 0 and step by 2, so 0::2 selects even dims for sine. The angle_rads uses the same indexing.
Complete the code to assign cosine to odd indices in the positional encoding.
pos_encoding[:, [1]] = torch.cos(angle_rads[:, [2]])
Odd indices start at 1 and step by 2, so 1::2 selects odd dims for cosine. Use the same slice for angle_rads.