Skip to content
DeepForgeby svx
All problems
HardNLPnlp-367

Sinusoidal Positional Encoding Vector

Problem

The sinusoidal positional encoding for position pos and even dimension d uses PE[2i] = sin(pos / 10000^(2i/d)) and PE[2i+1] = cos(pos / 10000^(2i/d)).

Given pos and an even dim d, return the encoding vector of length d.

Hint

Each frequency contributes a sine and a cosine pair.

Test cases

case 1
in: [0,4]
expected: [0,1,0,1]
case 2
in: [1,2]
expected: [0.8414709848078965,0.5403023058681398]
case 3
in: [2,6]
expected: [0.9092974268256817,-0.4161468365471424,0.09269850077872725,0.9956942241237399,0.0043088560467428125,0.9999907168366957]
Starter codePython
def sinusoidal_positional_encoding_vector(pos, dim):
    # Your code here
    pass

3 test cases · run your code against them in the editor