Model Pipeline - Positional encoding
This pipeline shows how positional encoding adds position information to input data so a model can understand order, especially in sequences like sentences.
This pipeline shows how positional encoding adds position information to input data so a model can understand order, especially in sequences like sentences.
Loss
1.2 |****
1.0 |***
0.8 |**
0.6 |*
0.4 |
1 2 3 4 5 Epochs| Epoch | Loss ↓ | Accuracy ↑ | Observation |
|---|---|---|---|
| 1 | 1.2 | 0.45 | Model starts learning with random weights and positional info |
| 2 | 0.9 | 0.60 | Loss decreases as model uses positional encoding to understand order |
| 3 | 0.7 | 0.72 | Model improves predictions by combining token meaning and position |
| 4 | 0.55 | 0.80 | Clear improvement showing positional encoding helps sequence tasks |
| 5 | 0.45 | 0.85 | Training converges with positional info aiding context understanding |
pos and dimension i using sine and cosine functions in PyTorch?pos_encoding?
import torch
import math
def get_positional_encoding(seq_len, d_model):
pos_encoding = torch.zeros(seq_len, d_model)
for pos in range(seq_len):
for i in range(0, d_model, 2):
pos_encoding[pos, i] = math.sin(pos / (10000 ** (i / d_model)))
if i + 1 < d_model:
pos_encoding[pos, i + 1] = math.cos(pos / (10000 ** (i / d_model)))
return pos_encoding
pos_encoding = get_positional_encoding(50, 512)import torch
import math
def positional_encoding(seq_len, d_model):
pos_enc = torch.zeros(seq_len, d_model)
for pos in range(seq_len):
for i in range(d_model):
pos_enc[pos, i] = math.sin(pos / 10000 ** (i / d_model))
return pos_enc
pos_enc = positional_encoding(10, 16)(batch_size, seq_len, d_model) in PyTorch. Which approach correctly applies positional encoding to each sequence in the batch?Pass the test and this pattern moves unknown