Skip to content

About

Transformer model for English-German translation

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Transformer Project - English-German Translation

A complete implementation of a Transformer model for English-German translation, trained on the WMT17 dataset.

📁 Project Structure

transformer_project/
├── modelling/           # Core model components
│   ├── transformer.py   # Main Transformer architecture
│   ├── attention.py     # Multi-head attention mechanism
│   ├── gpt2_tokenizer.py # BPE tokenizer implementation
│   └── ...
├── run/                 # Executable scripts (see below)
├── saved_models/        # Downloaded models & training outputs (not in git)
│   ├── tokenizer/       # Production tokenizer (32k vocab, 5M samples)
│   ├── tokenizer_quick/ # Quick test tokenizer
│   └── model.pt         # Best model (5M samples, 5 epochs)
├── dataset.py           # WMT17 dataset loader
└── utils.py             # Helper functions

Note: Pre-trained models are hosted on 🤗 Hugging Face

🚀 Quick Start

1. Installation

# Clone repository
git clone https://github.com/lennart02/transformer_project.git
cd transformer_project

# Option A: Install with Poetry 
poetry install

# Option B: Install with pip (simpler alternative)
pip install torch datasets tqdm tokenizers transformers pytest huggingface-hub

Note: This project supports both Poetry (for dependency management) and pip installation.

2. Download Pre-trained Models

Pre-trained models are hosted on 🤗 Hugging Face for easy access:

Option A: Automatic Download (Recommended)

# Models will be downloaded automatically on first use
python run/inference.py

Option B: Manual Download

# Install huggingface-cli if needed
pip install huggingface-hub

# Download models
huggingface-cli download Lennart02/transformer-de-en --local-dir saved_models/

# Or using Python
python -c "from huggingface_hub import snapshot_download; snapshot_download('Lennart02/transformer-de-en', local_dir='saved_models/')"

🔗 Model Repository: Lennart02/transformer-de-en

Why Hugging Face?

  • No Git LFS issues with large model files
  • Fast CDN-based downloads
  • Easy versioning and model sharing
  • saved_models/ is your workspace for experiments (ignored by git)

3. Quick Test

# Interactive translation (models download automatically on first use)
python run/inference.py

# With Poetry
poetry run python run/inference.py

# Test with 5 samples
python run/test_validation.py --num-samples 5

# Or manually download models first
python download_models.py

🛠️ Usage Guide

run/inference.py - Interactive Translation

Translate English sentences interactively using a trained model.

python run/inference.py
python run/inference.py --checkpoint saved_models/model_quick.pt

run/test_validation.py - Evaluate Model Quality

Test translation quality with BLEU scores and benchmarks.

python run/test_validation.py --num-samples 5
python run/test_validation.py --bleu --num-samples 100
python run/test_validation.py --benchmark

Key options: --checkpoint PATH, --num-samples N, --bleu, --benchmark


run/main.py - Train Model

Train a new Transformer model from scratch.

python run/main.py --quick              # Fast test (5 min, 1000 samples)
python run/main.py                      # Full training (1-2h, 5M samples)
python run/main.py --resume saved_models/model.pt

Key options: --quick, --resume PATH, --max-samples N, --epochs N, --batch-size N, --no-amp

Output: Saves to saved_models/model.pt and saved_models/model_best_with_AMP.pt


run/train_tokenizer.py - Train Custom Tokenizer

Train a BPE tokenizer on the WMT17 dataset (32k vocab, 5M samples).

python run/train_tokenizer.py

Note: Only needed if retraining. Pre-trained tokenizers are available on Hugging Face.


run/simple_overfit_test.py - Sanity Check

Verify the model can memorize 10 sentences (~5 min).

python run/simple_overfit_test.py

run/benchmark_devices.py - Performance Comparison

Benchmark training speed on CPU vs GPU (FP32 vs AMP).

python run/benchmark_devices.py --modes all
python run/benchmark_devices.py --modes gpu amp

📊 Pre-trained Models

Pre-trained models are available on 🤗 Hugging Face: Lennart02/transformer-de-en

model.pt

  • Training data: 5,000,000 WMT17 samples
  • Epochs: 5
  • Architecture: 6-layer encoder/decoder, 512d model, 8 attention heads
  • Performance: ~28% BLEU score on validation set
  • Training time: ~5 hours on GPU with AMP

tokenizer/

  • Vocabulary size: 32,000 tokens
  • Algorithm: Byte-Pair Encoding (BPE)
  • Languages: English + German (with proper umlaut support)

tokenizer_quick/

  • Smaller tokenizer for quick experiments (8,000 tokens)

Download: Models are automatically downloaded on first use, or manually via:

huggingface-cli download Lennart02/transformer-de-en --local-dir saved_models/

🧪 Testing

# Run all unit tests
pytest test/

📝 Model Architecture

  • Type: Transformer (Encoder-Decoder)
  • Encoder layers: 6
  • Decoder layers: 6
  • Model dimension: 512
  • Attention heads: 8
  • Feedforward dimension: 2048
  • Max sequence length: 128 tokens
  • Dropout: 0.1

🐛 Troubleshooting

"Checkpoint not found"

Download models from Hugging Face:

huggingface-cli download Lennart02/transformer-de-en --local-dir saved_models/

Or let them download automatically on first run:

python run/inference.py

"CUDA out of memory"

Reduce batch size in config or use AMP:

python run/main.py --batch-size 16

🔗 References

About

Transformer model for English-German translation

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages