Example application for the task of fine-tuning pretrained machine translation models on highly domain-specific translated sentences.
For this, likely translation pairs are first extracted from the original versions and the German book translations of the Harry Potter fantasy novel series using a Translated Sentence Mining approach. The extracted sentence translations are then used to fine-tune three baseline machine translation models (pre-trained model MarianMT for translation from English to German, Google's Text-To-Text Transfer Transformer T5 and Meta AI's No Language Left Behind machine translation model NLLB-200 in its distilled 600M variant).
Afterwards, some metrics are calculated to evaluate the performance boost from fine-tuning the models.
- Split the unaligned txt files for each book and its translation file into sentences using Lingtrain Aligners splitter and preprocessor
- Calculate language-independent sentence level embeddings for the split sentences using GoogleAI's Language-Agnostic BERT Sentence Embeddings (LaBSE) in the Sentence Transformers framework
- Match the best fitting translation pairs for all sentences using K-Nearest Neighbors search, mostly following Sentence Transformers example application for Translated Sentence Mining
- Filter the sentence pairs by a minimum similarity score
- Remove sentence pairs containing sentences shorter than 20 or longer than 200 characters
- Split the resulting corpus of ~54.000 likely parallel sentences randomly into a train, validation and test set (80%, 10%, 10%)
- Load the pre-trained models
Helsinki-NLP/opus-mt-en-de
(MariantMT),t5-large
(T5) andfacebook/nllb-200-distilled-600M
(NLLB-200) in huggingface - Fine-tune the models on the extracted parallel sentences using the train and evaluation set for 10 epochs each
- Use the non-fine-tuned MarianMT, T5 and NLLB-200 models to get machine translations for a sample from the test set
- Use the fine-tuned models to get machine translations for a sample from the test set
- Calculate BLEU, METEOR and BertScore between references and the target language translations for each the non-fine-tuned and the fine-tuned models
Model | BLEU | METEOR | BertScore1 |
---|---|---|---|
MarianMT (baseline) | 0.249 | 0.421 | 0.596 |
MarianMT (fine-tuned) | 0.379 | 0.541 | 0.709 |
T5 (baseline) | 0.173 | 0.297 | 0.446 |
T5 (fine-tuned) | 0.367 | 0.521 | 0.690 |
NLLB-200 (baseline) | 0.244 | 0.411 | 0.595 |
NLLB-200 (fine-tuned) | 0.367 | 0.531 | 0.703 |
1: setting the parameter rescale_with_baseline
to True
pytorch==2.3.1
cudatoolkit=12.1
pywin32
transformers
sentence_transformers
faiss-gpu
sacrebleu
datasets
bert-score
lingtrain-aligner
razdel
dateparser
python-dateutil
numpy
openpyxl
sentencepiece
sacremoses
nltk
All files in this repository which contain text from the books are cut off after the first 50 rows.
The trained model files model.safetensors
and optimizer.pt
for each model are omitted in this repository.