This project trains and runs a generative neural machine translation model built with PyTorch.
It uses a single autoregressive transformer that sees:
- source sentence tokens
- a separator token
<TRANS> - target sentence tokens
The default dataset setup is Bulgarian source -> English target (train.bg -> train.en).
- Prepares parallel corpora and builds a vocabulary
- Trains a transformer language model for translation
- Continues training from a saved checkpoint
- Translates new source sentences
- Evaluates with perplexity and corpus BLEU
run.py: CLI entry point (prepare,train,translate,perplexity,bleu, etc.)model.py: transformer model and decoding (sampling + beam search)utils.py: corpus loading/tokenization and progress barparameters.py: data paths and training/model hyperparametersflake.nix: Nix dev shell with Python + PyTorch + NLTK tooling
Expected files (see parameters.py):
en_bg_data/train.bgen_bg_data/train.enen_bg_data/dev.bgen_bg_data/dev.en
Format rules:
- One sentence per line
- Parallel files must be line-aligned
- Sentences are tokenized with
nltk.word_tokenize
nix developThis shell provides torch/numpy/nltk and sets NLTK_DATA.
Install at minimum:
torchnumpynltk
Also ensure NLTK punkt resources are available (the code calls nltk.download('punkt')).
Run commands from the project root.
- Prepare data and vocabulary:
python run.py prepare- Train:
python run.py train- Resume training from saved checkpoint:
python run.py extratrain- Translate a source file:
python run.py translate en_bg_data/dev.bg predictions.en- Evaluate perplexity on parallel files:
python run.py perplexity en_bg_data/dev.bg en_bg_data/dev.en- Evaluate BLEU (reference vs hypothesis files):
python run.py bleu en_bg_data/dev.en predictions.en- Generate continuation from a raw token prefix (debug utility):
python run.py generate "<S> example tokens <TRANS>"corpusData: pickled train/dev integerized corporawordsData: pickledword -> indexvocabularyNMTmodel: saved model weightsNMTmodel.optim: optimizer/checkpoint state
Edit parameters.py to change:
- dataset paths
- model size (
d_model,num_layers,num_heads) - optimization settings (
learning_rate,batch_size,max_epochs, etc.)