Translation model
A 4.9M-parameter decoder-only Transformer trained from scratch to translate French to English, for Natural Language Processing with John Hewitt.
For Natural Language Processing with John Hewitt at Columbia, I built a decoder-only Transformer1 in PyTorch and trained it from scratch to translate French into English. It learned from 28,906 sentence pairs from Multi30k23, which contains short descriptions of photos written in English and translated into French. On the 1,062 test sentences it scores 34.94 BLEU. For reference, OPUS-MT4, a public French-to-English model built by the University of Helsinki’s language technology group and trained on far more data, scores 41.02 on the same sentences.
Type a French sentence to test the model in your browser, or select a test sentence to translate it.
English
Reference:
Select a test sentence to translate it
Model
OPUS-MT is an encoder-decoder model: one network reads the French and another writes the English. A decoder-only model, like GPT-25, is a single stack that predicts the next token. To use one for translation, each pair becomes one sequence, with <start> marking where the English begins:
Un chien court . <start> A dog runs . <end>
Write the French sentence as and the English so far as . To translate, I give the model followed by <start> and use greedy decoding6, taking the most likely next token each time:
Each new token is fed back in until the model writes <end>. It goes one token at a time because each word depends on the ones before it.
A causal mask7 stops each position from attending to the tokens after it, so in training the model can’t just read the next English word from its input. The loss, , counts only the English tokens and <end>. Predictions made while the model is still reading French are masked out, since it never has to predict French when translating.
def create_masked_target(inputs, targets, start_token_id, pad_token_id, ignore_index=-1):
english_started = (inputs == start_token_id).cumsum(dim=1) > 0
keep = english_started & (targets != pad_token_id) & (targets != start_token_id)
return targets.masked_fill(~keep, ignore_index)Model and training settings
| Tokenizer | |
|---|---|
| Type | BPE8, provided by the course, trained on about 1M French-English sentence pairs from WMT14 |
| Vocabulary | 32,000 tokens, shared by French and English |
| Model | |
| Type | Decoder-only Transformer |
| Blocks | 4, pre-norm |
| Attention heads | 4 |
| Embedding size | 128 |
| Feed-forward | GELU, 128 → 512 → 128 |
| Normalization | LayerNorm |
| Positions | Learned embeddings |
| Output layer | Tied to the input embeddings |
| Context | 79 tokens, the longest pair in the data after tokenization |
| Parameters | 4,899,712 (4,096,256 in the token embeddings) |
| Training | |
| Optimizer | AdamW, learning rate 1e-3, weight decay 0.1, betas 0.9 and 0.95 |
| Schedule | 500 warmup steps, then inverse square root |
| Batch size | 128 |
| Dropout | 0.1 |
| Gradient clipping | 1.0 |
| Epochs | 30 |
Training
After 30 epochs, validation loss was 1.408 and test BLEU was 34.50.
Then I ran three short continuations from that checkpoint, six epochs each: the same schedule, half the learning rate, and label smoothing9 of 0.05. Each run kept its epoch with the lowest validation loss, and I picked the lowest of those before running anything on the test set. The losses here are plain cross-entropy without smoothing, so the four runs compare fairly.
| Run | Best epoch | Validation loss | Validation BLEU |
|---|---|---|---|
| Starting point | 30 | 1.408 | 45.91 |
| Same schedule | 35 | 1.404 | 47.17 |
| Half learning rate | 35 | 1.391 | 47.52 |
| Label smoothing 0.05 | 33 | 1.381 | 46.91 |
Label smoothing had the lowest loss, and its test BLEU was 34.94, up 0.45. That gain is small. A paired bootstrap10 over the test sentences puts the 95% interval at −0.20 to 1.11 points, which includes zero, so I wouldn’t call it a real improvement. The half-learning-rate run had the best validation BLEU but not the lowest loss, so the two measures don’t always pick the same model. Also, the three runs weren’t independent restarts: each one reused the optimizer state left by the run before it, so the table can’t isolate the effect of label smoothing or the learning rate.
Results
BLEU11 scores a translation by how many of its words and short word sequences also appear in a human reference translation, computed here with sacreBLEU12. Higher is better, but even good human translations score well below 100, since there are many right ways to say the same thing. Both models used greedy decoding with up to 50 new tokens.
| Model | Training data | Test BLEU |
|---|---|---|
| This model | Multi30k, 28,906 pairs | 34.94 |
| OPUS-MT | OPUS, a much larger collection | 41.02 |
OPUS-MT differs in data, architecture and training all at once, so this comparison can’t say which of them accounts for the gap.
Short, common sentences come out well. Longer ones and rare words go wrong in a few typical ways: a made-up word, a dropped number, or stopping early. These are from the test set.
| French | Model | Reference |
|---|---|---|
| Trois filles se tiennent devant la fenêtre d'un bâtiment. | Three girls stand outside the window of a building. | Three girls are standing in front of a window of a building. |
| L'homme se gratte l'arrière du cou tout en cherchant un livre dans une librairie. | The man is scratting the back of the back while looking for a book at a bookstore. | The man is scratching the back of his neck while looking for a book in a book store. |
| 15 grands chiens jouent dans un jardin clôturé à côté d'une maison. | Large dogs play in a fenced yard next to a house. | 15 large dogs playing in a fenced yard beside a house. |
| Une photo en noir et blanc de deux personnes en train de regarder une magnifique toile. | A picture in black and white | Black and white photo of two people watching a beautiful painting |
Some rare words get swapped for wrong ones. On a validation sentence, Une femme jouant un morceau sur son violon became “A woman playing a piece of her purple on her purple.”
Attention
Nothing in the setup tells the model which French word goes with which English word, but one attention head (layer 3, head 1) learned to line them up. Each row below is the step that writes one English token, and each column is a French token. Stronger color means more weight. Words split into pieces show up as separate tokens, like bar and il for baril.
For 10 of the 12 English tokens, the strongest weight is on the French word being translated, even where the word order changes: baril bleu et blanc comes out as “blue and white barrel.” The two misses are “and,” which spreads its weight over the end of bleu and over baril, and the period, which looks at et.
Notebook on GitHub Model on Hugging Face
BLEU ranges from 0 to 100. As a rough guide, Google Cloud describes scores from 30 to 40 as understandable to good, but what a score means depends on the test set.13
References
-
Ashish Vaswani, Noam Shazeer, Niki Parmar, et al. 2017. Attention Is All You Need. In Advances in Neural Information Processing Systems (NeurIPS). ↩
-
Desmond Elliott, Stella Frank, Khalil Sima’an, et al. 2016. Multi30K: Multilingual English-German Image Descriptions. In Proceedings of the 5th Workshop on Vision and Language. ↩
-
Desmond Elliott, Stella Frank, Loïc Barrault, et al. 2017. Findings of the Second Shared Task on Multimodal Machine Translation and Multilingual Image Description. In Proceedings of the Second Conference on Machine Translation (WMT). ↩
-
Jörg Tiedemann and Santhosh Thottingal. 2020. OPUS-MT – Building open translation services for the World. In Proceedings of the 22nd Annual Conference of the European Association for Machine Translation (EAMT). ↩
-
Alec Radford, Jeffrey Wu, Rewon Child, et al. 2019. Language Models are Unsupervised Multitask Learners. OpenAI. ↩
-
John Hewitt. 2026. Lec 13: Methods in Pretraining, Finetuning, and Generation. COMS 4705 lecture notes, Columbia University. ↩
-
John Hewitt. 2026. Lecs 8,9: Self-Attention and Transformers. COMS 4705 lecture notes, Columbia University. ↩
-
Rico Sennrich, Barry Haddow, and Alexandra Birch. 2016. Neural Machine Translation of Rare Words with Subword Units. In Proceedings of the 54th Annual Meeting of the Association for Computational Linguistics (ACL). ↩
-
Christian Szegedy, Vincent Vanhoucke, Sergey Ioffe, et al. 2016. Rethinking the Inception Architecture for Computer Vision. In Proceedings of the IEEE Conference on Computer Vision and Pattern Recognition (CVPR). ↩
-
Philipp Koehn. 2004. Statistical Significance Tests for Machine Translation Evaluation. In Proceedings of the 2004 Conference on Empirical Methods in Natural Language Processing (EMNLP). ↩
-
Kishore Papineni, Salim Roukos, Todd Ward, et al. 2002. Bleu: a Method for Automatic Evaluation of Machine Translation. In Proceedings of the 40th Annual Meeting of the Association for Computational Linguistics (ACL). ↩
-
Matt Post. 2018. A Call for Clarity in Reporting BLEU Scores. In Proceedings of the Third Conference on Machine Translation: Research Papers (WMT). ↩
-
Google Cloud. The BLEU translation quality metric. Cloud Translation documentation. Accessed November 17, 2026. ↩