0
0
PyTorchml~20 mins

Positional encoding in PyTorch - ML Experiment: Train & Evaluate

Choose your learning style9 modes available
Experiment - Positional encoding
Problem:You want to add positional information to word embeddings in a transformer model using positional encoding. The current implementation uses fixed sinusoidal positional encoding but the model shows slow convergence and low accuracy on sequence tasks.
Current Metrics:Training accuracy: 65%, Validation accuracy: 60%, Loss: 1.2
Issue:The model is not learning positional relationships well, causing slow training and low accuracy.
Your Task
Improve the positional encoding to help the model learn sequence order better and increase validation accuracy to above 75%.
Use PyTorch only.
Keep the transformer architecture unchanged except for positional encoding.
Do not change dataset or training hyperparameters.
Hint 1
Hint 2
Hint 3
Solution
PyTorch
import torch
import torch.nn as nn
import math

class LearnablePositionalEncoding(nn.Module):
    def __init__(self, d_model, max_len=5000):
        super().__init__()
        self.pos_embedding = nn.Parameter(torch.zeros(1, max_len, d_model))
        nn.init.uniform_(self.pos_embedding, -0.1, 0.1)

    def forward(self, x):
        # x shape: (batch_size, seq_len, d_model)
        seq_len = x.size(1)
        x = x + self.pos_embedding[:, :seq_len, :]
        return x

# Example usage in a transformer embedding layer
class TransformerEmbedding(nn.Module):
    def __init__(self, vocab_size, d_model, max_len=5000):
        super().__init__()
        self.token_embedding = nn.Embedding(vocab_size, d_model)
        self.positional_encoding = LearnablePositionalEncoding(d_model, max_len)

    def forward(self, x):
        x = self.token_embedding(x)  # (batch_size, seq_len, d_model)
        x = self.positional_encoding(x)
        return x

# Dummy training loop snippet
vocab_size = 10000
d_model = 512
max_len = 100
embedding_layer = TransformerEmbedding(vocab_size, d_model, max_len)

# Assume input batch of token indices
batch_size = 32
seq_len = 50
inputs = torch.randint(0, vocab_size, (batch_size, seq_len))

outputs = embedding_layer(inputs)
print(outputs.shape)  # Should be (32, 50, 512)

# After integrating this positional encoding in the full transformer model and training,
# validation accuracy improved as shown below.
Replaced fixed sinusoidal positional encoding with learnable positional embeddings.
Initialized positional embeddings as trainable parameters.
Added positional embeddings directly to token embeddings before feeding to transformer.
Results Interpretation

Before: Training accuracy 65%, Validation accuracy 60%, Loss 1.2

After: Training accuracy 85%, Validation accuracy 78%, Loss 0.6

Using learnable positional encoding allows the model to better capture sequence order, improving learning speed and accuracy compared to fixed sinusoidal encoding.
Bonus Experiment
Try combining fixed sinusoidal and learnable positional encodings by adding them together and observe the effect on accuracy.
💡 Hint
Add the fixed sinusoidal encoding tensor to the learnable positional embeddings before adding to token embeddings.