Loading...
Implement the sinusoidal positional encoding from "Attention Is All You Need".
For a sequence of length L and model dimension D, compute the positional encoding matrix PE where:
Input:
Output: The L x D positional encoding matrix, values rounded to 4 decimal places, one row per position.
2 4
[ 0.0000 1.0000 0.0000 1.0000] [ 0.8415 0.5403 0.0100 0.9999]
PE for a sequence of length L = 2 and model dimension D = 4.pos in the sequence, we compute the values of PE(pos, 2i) and PE(pos, 2i+1) using the given formulas: PE(pos,2i)=sin(pos/100002i/D) and PE(pos,2i+1)=cos(pos/100002i/D).pos = 0 and pos = 1, and i = 0 and i = 1, to get the values for the first and second positions:
pos = 0, we get PE(0,0)=sin(0/100000/4)=sin(0)=0, PE(0,1)=cos(0/100000/4)=cos(0)=1, PE(0,2)=sin(0/100001/4)=sin(0)=0, PE(0,3)=cos(0/100001/4)=cos(0)=1.pos = 1, we get PE(1,0)=sin(1/100000/4)=sin(1)≈0.8415, PE(1,1)=cos(1/100000/4)=cos(1)≈0.5403, PE(1,2)=sin(1/100001/4)≈sin(0.0100)≈0.0100, PE(1,3)=cos(1/100001/4)≈cos(0.0100)≈0.9999.