Positional Encoding in Transformers:
In the Transformer architecture, unlike recurrent neural networks (RNNs), there is no inherent notion of sequence order. That is, the model does not process input sequentially and thus lacks information about the position or order of tokens in a sequence (e.g., words in a sentence). To address this, positional encoding is introduced to give the model awareness of the relative or absolute position of tokens in the input.
🔍 What is Positional Encoding?
Positional encoding is a technique used to inject information about the position of each token in the input sequence into the model. It involves adding a vector (of the same dimension as the word embedding) to each token's embedding based on its position in the sequence.
These positional encodings can be either:
- Learned: The model learns the positional embeddings during training, similar to how it learns word embeddings.
- Fixed/static: Based on predefined mathematical functions — commonly sine and cosine functions of different frequencies, as used in the original Transformer paper ("Attention Is All You Need," Vaswani et al., 2017).
📐 Mathematical Definition (Fixed Positional Encoding)
For a given position pos (starting from 0) and dimension i, the positional encoding is defined as:
PE(pos,2i)=sin(100002i/dmodelpos
PE(pos,2i+1)=cos(10000
Where:
- dmodel is the dimensionality of the model (e.g., 512 or 768),
- i is the dimension index,
- Even positions use sine; odd positions use cosine.
This design allows the model to attend to relative positions effectively, since the functions allow the model to learn patterns based on relative distances between positions.
⚠️ Why is Positional Encoding Important?
-
Sequence Order Awareness:
- Since Transformers process all tokens in parallel, they lack the sequential structure present in RNNs.
- Positional encoding adds essential order information to the otherwise position-agnostic word embeddings.
-
Improved Attention Mechanism:
- By incorporating positional info, the attention mechanism can better understand relationships like "subject before verb" or "modifier before noun."
-
Generalization Across Lengths:
- Fixed positional encodings using sine/cosine functions allow the model to generalize to sequence lengths not seen during training.
-
Preservation of Input Structure:
- Language heavily relies on word order for meaning. Without positional encoding, the model would treat sentences like “The cat chased the dog” and “The dog chased the cat” as equivalent, which is incorrect.
✅ Summary
| Feature | Description |
|---|
| Purpose | Provide position information to the model |
| Type | Learned or fixed (e.g., sine/cosine-based) |
| Dimension | Same as token embeddings |
| Impact | Enables understanding of sequence structure and improves model performance |
🧠 Final Thought
Positional encoding is a critical component of the Transformer architecture. It compensates for the absence of recurrence or convolution by explicitly encoding the position of tokens, allowing the model to effectively capture sequential dependencies and syntactic structures in the data.