This project implements a sequence-to-sequence neural machine translation model using PyTorch and TorchText to translate German sentences to English. The model employs an Encoder-Decoder architecture with GRU (Gated Recurrent Units) and demonstrates the complete pipeline from data preprocessing to translation generation.
Multi30k Dataset - A multilingual image description dataset:
- Language pair: German (DE) → English (EN)
- Training split: German-English sentence pairs
- Validation split: Used for evaluation and translation examples
- Source: Modified Multi30k dataset from small_DL_repo
- Tokenization: Spacy tokenizers for both languages
- Parallel corpus: Aligned German-English sentence pairs
- Domain: Image descriptions and captions
- Vocabulary size: Dynamic based on token frequency
- Special tokens:
<unk>,<pad>,<bos>,<eos>
Encoder (German → Hidden State)
Encoder:
├── Embedding(vocab_de_size, embed_size=300)
├── Dropout(0.1)
└── GRU(embed_size=300, hidden_size=512)Decoder (Hidden State → English)
Decoder:
├── Embedding(vocab_en_size, embed_size=300)
├── ReLU activation
├── GRU(embed_size=300, hidden_size=512)
├── Linear(hidden_size=512, vocab_en_size)
└── LogSoftmax(dim=-1)- Embedding dimension: 300
- Hidden state size: 512
- Sequence processing: Teacher forcing during training
- Output: Log probabilities for English vocabulary
# Language-specific tokenizers
de_tokenizer = get_tokenizer('spacy', language='de_core_news_sm')
en_tokenizer = get_tokenizer('spacy', language='en_core_web_sm')# Builds vocabularies from tokenized text
vocab_de = build_vocab_from_iterator(yield_tokens(...), min_freq=1)
vocab_en = build_vocab_from_iterator(yield_tokens(...), min_freq=1)def collate_fn(batch):
# Tokenizes text
# Adds special tokens (<bos>, <eos>)
# Pads sequences to equal length
# Returns padded tensors- Batch size: 32
- Epochs: 20
- Loss function: NLLLoss (ignores padding tokens)
- Optimizers: Adam for both encoder and decoder
- Learning rate: 0.001
- Weight decay: 0.001
def train_one_epoch():
# 1. Encode source sentence
encoder_outputs, encoder_hidden = encoder(source_ids)
# 2. Initialize decoder with encoder's final state
decoder_hidden = encoder_hidden
# 3. Teacher forcing: use ground truth as input
predictions, _ = decoder(target_ids[:, :-1], decoder_hidden)
# 4. Compute loss against shifted targets
loss = loss_fn(predictions, target_ids[:, 1:])def eval_one_epoch():
# 1. Encode source sentence
# 2. Generate translation word by word
# 3. Use previous prediction as next input
# 4. Stop at <eos> token or max length- Tokenization: Language-specific processing
- Vocabulary management: Special token handling
- Sequence alignment: Padding and batching
- Training: Teacher forcing for stable learning
- Inference: Autoregressive generation
- Flexibility: Different strategies for training vs. evaluation
# Real translation examples shown during final epoch:
# Source (German): Original sentence
# Ground Truth (English): Reference translation
# Predicted (English): Model-generated translation- Padding tokens ignored in loss calculation
- Batch processing for efficiency
- Dynamic sequence lengths
Machine_Translation_Using_TorchText/
├── Machine_Translation_German_To_English_TorchText.ipynb
├── .gitignore
└── README.md
pip install torch==2.0.1 torchtext==0.15.2 torchdata spacy
python -m spacy download en_core_web_sm
python -m spacy download de_core_news_sm- Environment Setup: Install dependencies and language models
- Data Loading: Automatic download of Multi30k dataset
- Training: Execute notebook cells for complete pipeline
- Translation: View sample translations in final epoch
for e in range(n_epochs):
train_loss = train_one_epoch()
eval_loss = eval_one_epoch(e, n_epochs)
# Displays sample translations in final epoch# Autoregressive generation:
input_id = target[:, 0] # Start with <bos>
for j in range(max_length):
probs, hidden = decoder(input_id.unsqueeze(1), hidden)
_, input_id = torch.topk(probs, 1, dim=-1)
if input_id.item() == EOS_IDX:
break- Variable length handling: Dynamic sequence processing
- Context preservation: Hidden state transfer from encoder to decoder
- End-to-end training: Joint optimization of both components
# Special token management:
UNK_IDX, PAD_IDX, BOS_IDX, EOS_IDX = 0, 1, 2, 3
special_symbols = ['<unk>', '<pad>', '<bos>', '<eos>']- Loss masking: Ignores padding tokens
- Gradient optimization: Separate optimizers for components
- Progress monitoring: Real-time loss tracking
- Sample generation: Shows actual translations
- Comparison display: Source vs. reference vs. predicted
- Qualitative assessment: Visual inspection of results
- Training loss: Decreases consistently across epochs
- Evaluation loss: Validation performance tracking
- Translation quality: Improves with training progression
Source Sentence: [German text]
GT Sentence: [English reference]
Predicted Sentence: [Model translation]
-
Architecture Enhancements:
- Attention mechanisms (Bahdanau/Luong attention)
- Transformer architecture
- Bidirectional encoders
-
Training Optimizations:
- Learning rate scheduling
- Beam search decoding
- BLEU score evaluation
-
Data Improvements:
- Larger parallel corpora
- Data augmentation techniques
- Domain adaptation
-
Advanced Features:
- Subword tokenization (BPE)
- Copy mechanisms
- Coverage penalties
- PyTorch 2.0.1: Deep learning framework
- TorchText 0.15.2: Text processing utilities
- Spacy: Tokenization and NLP preprocessing
- TorchData: Data loading utilities
- Complete seq2seq implementation from scratch
- Bilingual text processing pipeline
- Teacher forcing training strategy
- Autoregressive inference
- Real translation examples generation
- Comprehensive vocabulary management
This project demonstrates:
- Sequence-to-Sequence Learning: Core NMT concepts
- RNN Architectures: GRU-based encoder-decoder
- Text Processing: TorchText library usage
- Multilingual NLP: Cross-language processing
- Training Strategies: Teacher forcing vs. autoregressive generation
The model learns to translate various German phrases to English:
- Simple sentences: Basic subject-verb-object structures
- Descriptive text: Image caption-style content
- Complex grammar: German grammatical structures to English