Study the paper
Attention Is All You Need
Lessons, visuals, quizzes, flashcards, and resources—organized in teaching order.
Injecting Order with Positional Encoding
Sinusoidal Positional Encoding (Even)
Sinusoidal Positional Encoding (Even)
Source equation
In sequence transduction models like the Transformer, which lack recurrence or convolution, positional encodings are added to the input embeddings to inject information about the order of tokens. This equation defines the sinusoidal positional encoding for even dimensions.
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}}})Deep dive
Walkthrough and Symbol Definitions
- : The positional encoding value at sequence position and even dimension index .
- : The position of the token in the sequence (0-indexed).
- : The index of the frequency dimension, where .
- : The total dimensionality of the model's hidden states and embeddings.
- : The standard trigonometric sine function.
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
Illustrative Example
Let's calculate the positional encoding value for:
- (which corresponds to the first even dimension, )
Step-by-step calculation:
- Calculate the denominator exponent: .
- Calculate the base value: .
- Compute the inner term: .
- Apply the sine function: .
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}}})- Positional encoding value at position pos and even dimension 2i · scalar
- The position of the token in the sequence · scalar
- The index variable used to compute the dimension index 2i · scalar
- The total dimensionality of the model embeddings · scalar