Study the paper
Attention Is All You Need
Lessons, visuals, quizzes, flashcards, and resources—organized in teaching order.
Injecting Sequence Order via Positional Encodings
Sinusoidal Positional Encoding (Even Dimensions)
Sinusoidal Positional Encoding (Even Dimensions)
Source equation
Since the Transformer contains no recurrence or convolution, positional encodings are added to the input embeddings to inject information about the relative or absolute position of tokens in the sequence.
Sources
equation
PE(pos,2i)=sin(pos/100002i/dmodel)\displaystyle PE_{(pos,2i)}=sin(pos/10000^{2i/d_{\text{model}}})
\displaystyle PE_{(pos,2i)}=sin(pos/10000^{2i/d_{\text{model}}})Sources
equation
PE(pos,2i)=sin(pos/100002i/dmodel)\displaystyle PE_{(pos,2i)}=sin(pos/10000^{2i/d_{\text{model}}})
\displaystyle PE_{(pos,2i)}=sin(pos/10000^{2i/d_{\text{model}}})Implementation detail
Let's calculate a single positional encoding value deterministically:
- Token position
- Dimension index (meaning )
- Model dimension
-
Compute the denominator:
-
Compute the argument of the sine function:
-
Compute the sine value:
Sources
equation
PE(pos,2i)=sin(pos/100002i/dmodel)\displaystyle PE_{(pos,2i)}=sin(pos/10000^{2i/d_{\text{model}}})
\displaystyle PE_{(pos,2i)}=sin(pos/10000^{2i/d_{\text{model}}})- Position of the token in the sequence · scalar
- Index of the dimension · scalar
- Dimensionality of the model embeddings · scalar
- Positional encoding value at position pos and even dimension 2i · scalar