## Projecte Aina’s German-Catalan machine translation model

## Table of Contents
- [Model Description](#model-description)
- [Intended Uses and Limitations](#intended-use)
- [How to Use](#how-to-use)
- [Training](#training)
  - [Training data](#training-data)
  - [Training procedure](#training-procedure)
	- [Data Preparation](#data-preparation)
	- [Tokenization](#tokenization)
	- [Hyperparameters](#hyperparameters)
- [Evaluation](#evaluation)
   - [Variable and Metrics](#variable-and-metrics)
   - [Evaluation Results](#evaluation-results)
- [Additional Information](#additional-information)
  - [Author](#author)
  - [Contact Information](#contact-information)
  - [Copyright](#copyright)
  - [Licensing Information](#licensing-information)
  - [Funding](#funding)
  - [Disclaimer](#disclaimer)
 
## Model description

This model was trained from scratch using the [Fairseq toolkit](https://fairseq.readthedocs.io/en/latest/) on a combination of Catalan-German datasets, which after filtering and cleaning comprised 6.258.272 sentence pairs. The model was evaluated on the Flores and NTREX evaluation datasets. 

## Intended uses and limitations

You can use this model for machine translation from German to Catalan.

## How to use

### Usage
Required libraries:

```bash
pip install ctranslate2 pyonmttok
```

Translate a sentence using python
```python
import ctranslate2
import pyonmttok
from huggingface_hub import snapshot_download
model_dir = snapshot_download(repo_id="projecte-aina/mt-aina-de-ca", revision="main")

tokenizer=pyonmttok.Tokenizer(mode="none", sp_model_path = model_dir + "/spm.model")
tokenized=tokenizer.tokenize("Willkommen beim Projekt Aina")

translator = ctranslate2.Translator(model_dir)
translated = translator.translate_batch([tokenized[0]])
print(tokenizer.detokenize(translated[0][0]['tokens']))
```

## Training

### Training data

The model was trained on a combination of the following datasets:

| Dataset       	| Sentences  	| Sentences after Cleaning|
|-------------------|----------------|-------------------|
| Multi CCAligned | 1.478.152 | 1.027.481 |
| WikiMatrix  	| 180.322 	| 125.811 	|
| GNOME	| 12.333|	1.241|
| KDE4    	| 165.439   	|  105.098 	|
| QED           	| 63.041 	|   49.181 	|
| TED2020 v1    	| 46.680 	| 38.428 |
| OpenSubtitles	| 303.329	| 171.376	|
| GlobalVoices| 4.636 	|	3.578|
| Tatoeba | 732 | 655 |
| Books | 4.445 | 2049 |
| Europarl | 1.734.643 | 1.734.643 |
| Tilde | 3.434.091 | 3.434.091 |
| **Total**     	| **7.427.843** | **6.258.272** |

All corpora except Europarl and Tilde were collected from [Opus](https://opus.nlpl.eu/). The Europarl and Tilde corpora are a synthetic parallel corpus created from the original Spanish-Catalan corpora by [SoftCatalà](https://github.com/Softcatala).


### Training procedure

### Data preparation

 All datasets are deduplicated and filtered to remove any sentence pairs with a cosine similarity of less than 0.75. This is done using sentence embeddings calculated using [LaBSE](https://huggingface.co/sentence-transformers/LaBSE). The filtered datasets are then concatenated to form a final corpus of 6.258.272 and before training the punctuation is normalized using a modified version of the join-single-file.py script from [SoftCatalà](https://github.com/Softcatala/nmt-models/blob/master/data-processing-tools/join-single-file.py)


#### Tokenization

 All data is tokenized using sentencepiece, with a 50 thousand token sentencepiece model  learned from the combination of all filtered training data. This model is included.  

#### Hyperparameters

The model is based on the Transformer-XLarge proposed by [Subramanian et al.](https://aclanthology.org/2021.wmt-1.18.pdf)
The following hyperparameters were set on the Fairseq toolkit:

| Hyperparameter                 	| Value                        	|
|------------------------------------|----------------------------------|
| Architecture                   	| transformer_vaswani_wmt_en_de_big |
| Embedding size                 	| 1024                         	|
| Feedforward size               	| 4096                         	|
| Number of heads                	| 16                           	|
| Encoder layers                 	| 24                           	|
| Decoder layers                 	| 6                            	|
| Normalize before attention     	| True                         	|
| --share-decoder-input-output-embed | True                         	|
| --share-all-embeddings         	| True                         	|
| Effective batch size           	| 48.000                       	|
| Optimizer                      	| adam                         	|
| Adam betas                     	| (0.9, 0.980)                 	|
| Clip norm                      	| 0.0                          	|
| Learning rate                  	| 5e-4                         	|
| Lr. schedurer                  	| inverse sqrt                 	|
| Warmup updates                 	| 8000                         	|
| Dropout                        	| 0.1                          	|
| Label smoothing                	| 0.1                          	|

The model was trained for a total of 29.000 updates. Weights were saved every 1000 updates and reported results are the average of the last 2 checkpoints.

## Evaluation

### Variable and metrics

We use the BLEU score for evaluation on the [Flores-101](https://github.com/facebookresearch/flores) and [NTREX](https://github.com/MicrosoftTranslator/NTREX) test sets

### Evaluation results

Below are the evaluation results on the machine translation from German to Catalan compared to [Softcatalà](https://www.softcatala.org/) and [Google Translate](https://translate.google.es/?hl=es):

| Test set         	| SoftCatalà | Google Translate |mt-aina-de-ca|
|----------------------|------------|------------------|---------------|
| Flores 101 dev   	| 29,0     	| **35,1**     	| 29,8     	|
| Flores 101 devtest   |29,3   	| **35,4**     	| 30,1     	|
| NTREX | 25,8 | **31,3** | 24,5 |
| Average          	| 28,0   	| **33,9**     	| 28,1      	|

## Additional information

### Author
Language Technologies Unit (LangTech) at the Barcelona Supercomputing Center

### Contact information
For further information, please send an email to langtech@bsc.es.

### Copyright
Copyright Language Technologies Unit at Barcelona Supercomputing Center (2023)

### Licensing information
This work is licensed under a [Apache License, Version 2.0](https://www.apache.org/licenses/LICENSE-2.0)

### Funding
This work has been promoted and financed by the Generalitat de Catalunya through the [Aina project] (https://projecteaina.cat/).

### Disclaimer

<details>
<summary>Click to expand</summary>

The models published in this repository are intended for a generalist purpose and are available to third parties. These models may have bias and/or any other undesirable distortions.
When third parties, deploy or provide systems and/or services to other parties using any of these models (or using systems based on these models) or become users of the models, they should note that it is their responsibility to mitigate the risks arising from their use and, in any event, to comply with applicable regulations, including regulations regarding the use of Artificial Intelligence.
In no event shall the owner and creator of the models (BSC – Barcelona Supercomputing Center) be liable for any results arising from the use made by third parties of these models.
</details>
{"license": "apache-2.0"}
transformers license:apache-2.0 endpoints_compatible region:us