Ismail Ben Alla

English-to-Arabic Translator

Transformer Machine Translation

NLP · Oct 2026 · Completed

An encoder-decoder Transformer written from scratch that learns English-to-Arabic translation from any parallel corpus.

Built withPyTorchCustom TransformersHugging Face TokenizersBeam searchHugging Face DatasetssacreBLEUpytest

English-to-Arabic Translator — overview

Overview

Machine translation is the task the Transformer was designed for, so I wrote the encoder-decoder model from Attention Is All You Need in plain PyTorch, about 150 lines for the model itself, and built everything around it that a translation system needs: data preparation, a tokenizer, training, decoding and evaluation. English to Arabic is the default pair, but the language codes are only file suffixes.

The model uses pre-norm residual blocks, a shared BPE vocabulary with tied input and output embeddings, and fused scaled-dot-product attention with source padding and causal target masks. The tokenizer is trained on the data and handles any script, so Arabic stays in its natural order with no reversal tricks. Training has label smoothing, a warm-up schedule, length-bucketed batches, gradient accumulation, resume and self-contained checkpoints; decoding is batched greedy or beam search with a length penalty; evaluation reports BLEU and chrF with sacreBLEU, plus checkpoint averaging.

I verified the pipeline end to end on a built-in toy dataset: 2,000 synthetic pairs from a 16-word English lexicon translated word by word into Arabic. A 0.95 M-parameter model trained for 1,500 CPU steps reaches BLEU 98.4 and chrF 99.0 on 100 unseen pairs. That proves the code works; it says nothing about real translation quality, which needs a corpus such as OPUS-100 and a GPU.

The question

Can a compact, readable Transformer plus the full pipeline around it (data cleaning, tokenizer, training, decoding, scoring) go from raw parallel text to translations without any black box?

Key features

How it works

  1. Prepare

    Sentences are cleaned (length, length-ratio and duplicate filters), shuffled, split, and used to train one BPE vocabulary for both languages.

  2. Embed

    Token ids go through one embedding table shared by the encoder, the decoder and the output projection, scaled by the square root of the width and added to sinusoidal positions.

  3. Encode and decode

    Pre-norm encoder layers build a memory of the source; decoder layers use causal self-attention over the target so far and cross-attention over that memory.

  4. Train

    Label-smoothed cross-entropy on the next target token, Adam with the Noam schedule, batches grouped by length to a token budget, and the best validation loss saved as best.pt.

  5. Translate and score

    Greedy decoding for speed or beam search with a length penalty for quality; en2ar-evaluate scores the test split with corpus BLEU and chrF.

Examples

Input

en2ar-translate --checkpoint checkpoints/toy/best.pt --text "three red bird" --text "blue cat" --text "seven green dog one"

Output

ثلاثة أحمر طائر أزرق قطة سبعة أخضر كلب واحد

Real output of the toy model. The toy task is word-by-word, so the output is the expected literal translation.

Input

en2ar-train --data-dir data/toy --preset small --max-steps 1500 --warmup-steps 200 --eval-every 500 --tokens-per-batch 1000 --device cpu && en2ar-evaluate --checkpoint checkpoints/toy/best.pt --beam-size 1

Output

val loss 0.092 (perplexity 1.10) at step 1000; test BLEU 98.4, chrF 99.0 greedy; BLEU 95.1, chrF 95.9 with beam size 4

The toy run from the README: 0.95 M parameters, a 155-token vocabulary, 40 minutes on a busy CPU. Beam search is slightly below greedy here because the length penalty is tuned for real sentences.

Tech stack

What I learned

Limitations

Keywords

machine translation · neural machine translation · NMT · English to Arabic · Arabic NLP · Transformer · encoder-decoder · attention · from scratch · BPE · byte-pair encoding · beam search · label smoothing · BLEU · chrF · sacreBLEU · OPUS-100 · PyTorch · sequence-to-sequence