scieee AI-readable full text Open interactive document viewer

Multi-Task learning for improving gender accuracy in neural machine translation

Ojeda Jiménez, Graciela

Abstract

Neural Machine Translation systems present different types of social biases, such as racial and gender, which are mainly associated with the bias present in the data used for training. Many studies in the field aim to reduce these biases, especially the gender one, which perpetuates stereotypes and prejudices. In this work, we want to mitigate gender bias by jointly learning the translation, the part-of-speech and the gender of the target language words. This approach has shown good results regarding gender accuracy with improvements up to 6.8 points with respect to the baseline, without significantly impacting the translation quality.

Full text

BACHELOR’S DEGREE THESIS IN ENGINEERING PHYSICS Multi-Task Learning for Improving Gender Accuracy in Neural Machine Translation Author: Graciela Ojeda Jiménez Supervisors: Carlos Escolano Peinado Christine Raouf Saad Basta Marta R. Costa-Jussà In fulfillment of the requirements for the bachelor’s degree in Engineering Physics in UNIVERSITAT POLITÈCNICA DE CATALUNYA Barcelona, June 2021 2 Abstract Neural Machine Translation systems present different types of social biases, such as racial and gender, which are mainly associated with the bias present in the data used for training. Many studies in the field aim to reduce these biases, especially the gender one, which perpetuates stereotypes and prejudices. In this work, we want to mitigate gender bias by jointly learning the translation, the part-of-speech and the gender of the target language words. This approach has shown good results regarding gender accuracy with improvements up to 6.8 points with respect to the baseline, without significantly impacting the translation quality. 3 Acknowledgements I would like to thank Marta R. Costa-Jussà for letting me participate in this project and for trusting me, giving me the opportunity to enter a completely unknown field for me and from which I have learned a lot, beyond the technical aspects of the work. Also, I would like to express my gratitude to Carlos Escolano and Christine Basta for accompanying me these months, for helping me whenever I needed it and for being so patient with me. You have taught me much more than you probably think and it has been a great pleasure working with you. I wish you the best in your professional careers. 4 Contents 1 Introduction ...................................................................................................... 5 1.1 Contributions ............................................................................................. 7 1.2 Thesis structure ......................................................................................... 7 2 Theoretical background ................................................................................... 8 2.1 The Transformer ....................................................................................... 8 2.2 The Loss Function ................................................................................... 11 2.3 The BLEU ............................................................................................... 12 2.4 Evaluating Gender Bias .......................................................................... 13 3 Methodology .................................................................................................. 15 3.1 Adding pos tag and gender classifiers .................................................... 16 3.2 Sharing the pos tag classifier between languages .................................. 19 4 Implementation .............................................................................................. 20 4.1 Fairseq .................................................................................................... 20 4.2 Datasets .................................................................................................. 20 4.3 The Baseline Model ................................................................................ 21 4.4 Stanza ..................................................................................................... 21 5 Results .......................................................................................................... 24 5.1 Bilingual case: English – Spanish ........................................................... 24 5.2 Translation quality ................................................................................... 25 5.3 Gender accuracy ..................................................................................... 26 5.4 Gender Bias vs. BLEU ............................................................................ 27 5.5 Examples ................................................................................................ 28 6 Conclusions ................................................................................................... 29 References ....................................................................................................... 30 List of Figures ................................................................................................... 33 List of Tables .................................................................................................... 34 5 Chapter 1 Introduction In recent years, the awareness of the scientific community about the bias present in Machine Translation (MT) systems has increased, especially in terms of gender bias. In the context of MT, gender bias refers to the mistranslation that occurs when the system tends to translate according to gender roles, even when there is no ambiguity. Much research has been done in this regard, trying to reduce this bias to create fairer translation systems. Some notable improvements have been made but there is still some way to go. Nowadays, we live in a more globalized and connected world, which leads society to use these types of tools to communicate with people who speak a different language. The fact that common translators present a gender bias that is sometimes too noticeable has a negative effect on society, helping to perpetuate certain stereotypes and prejudices. An example is the tendency of certain systems to generalize the different professions carried out by men and women [1]. This is especially more visible in cases where you are trying to translate from a gender-neutral language, such as English, to another with grammatical gender, such as Spanish. In the first type, nouns have no grammatical gender, while in the other type, nouns and also adjectives, verbs, etc., carry gender inflections. For example, when translating a noun from English to Spanish, the system has to associate an inflection to the noun in Spanish and normally, the system uses masculine or feminine inflections following stereotypes. This occurs both in cases where the context indicates gender explicitly and in those where it does not. In Figure 1 we see how even though the pronoun "her" refers to "The boss", which is a neutral word in English, the system translates "El jefe", which is the masculine inflection of the word "boss" in Spanish. On the other hand, without having any reference in the context of the sentence, the system translates "the secretary '' to "la secretaria'' which is the feminine inflection of the word. Figure 1. Example of a translation from English to Spanish made with Google Translate 6 Gender bias is mainly attributed to the bias that is already present in the data used to train MT systems [2, 3]. Furthermore, it has been shown that models trained on these data tend to further amplify this bias [4]. In this sense, research has been done aiming to avoid or reduce this amplification, such as fine-tuning techniques with gender-balanced dataset corpus [5] or annotating the source language words of the training data with the gender of the target language words [6]. These techniques have shown good results regarding gender accuracy. In line with the latter, our proposal is to make the system capable of predicting the part-of-speech 1 (pos) and the gender of the words of the target language, besides the translation. With this we hope that, by having more information about the output words, the system maintains the quality of the translation and is able to better predict the gender based on the context, without falling so much into the stereotype. We will start from a Language-Specific Encoder-Decoder [7] architecture based on the Transformer [8], which has already been shown to present a better gender accuracy with respect to Shared Encoder-Decoder architectures [9]. We will use this model as our baseline and on it we will make the necessary modifications to make the system predict pos tag and gender. The idea is that our new model is able to predict all three things at the same time: translation, pos tag and gender. However, in order to see which is the effect of each of the new classifiers we will also train them separately. As the pos tags of the words are in general common between the different languages, we will also try to train this classifier in a shared way between all the languages. These different configurations will be trained with the same dataset and for all possible combinations of English, Spanish, French and German. In order to evaluate the quality of the translation we will use the BLEU [10] score, which is the standard metric used in this field. As for the gender bias, we will use the WinoMT [11] test set, which has been established as the reference measure in terms of gender accuracy. In general, the different configurations proposed have not increased the quality of the translation on average, although there are some better results for certain pairs of languages such as French-English or English-German, for which the baseline is outperformed by 0.51 and 0.31 BLEU points, respectively. Even so, the results are not much worse than those of the baseline (in the worst case, the BLEU is 1.52% lower), so we can consider that the modifications made do not damage the quality too much. Regarding gender bias, configurations in which the pos tag classifier is not shared outperform the baseline model, the best being the one with both classifiers (pos 1 This refers to the grammatical category of words. The part of speech indicates the syntactic function that the word has in the sentence. Examples of pos tags: noun, verb, adjective... 7 tag and gender) with increases of up to 10.54% in accuracy. Those that do share the pos tag classifier generally result in poorer gender precision, giving results up to 2.07% worse. 1.1 Contributions This project proposes to adapt a multilingual machine translation system so that it is able to obtain the pos tag and gender of the words of the target language. With this additional information from the target language, the system will be able to better predict the gender of those words that may cause confusion to the system when translating from a gender-neutral language to another with grammatical gender. With this, we will obtain a multilingual system, with the advantages that it already presents per se [7], with an acceptable quality of translation and that is capable of performing fairer translations in terms of gender. 1.2 Thesis structure This thesis is structured as follows:  Chapter 2: Theoretical background. Explanation of the main characteristics of the architecture that we will use and the metrics that we will employ to evaluate our different models.  Chapter 3: Methodology. Modifications made on the base architecture to achieve our objective.  Chapter 4: Implementation. Different toolkits, packages and datasets used in this project.  Chapter 5: Results. Results of the different configurations and comparison.  Chapter 6: Conclusions. Discussion of the general results of the project and further work. 8 Chapter 2 Theoretical background 2.1 The Transformer In this section we proceed to introduce the Transformer architecture, which is based on attention mechanisms. We focus mostly on the parts of the Transformer that are more relevant to understand this project. For a more complete description you can refer to the original presentation of the Transformer [8] or this blog post [12] 2 , where you can find a very good illustration of it. First of all, we can see the Transformer as a black box, to which we give an input sentence and from which we get, as an output, the translated sentence. Taking a look inside it, the Transformer consists of one encoder and one decoder, each one composed of n layers. The structure of these layers is the same, but each one has its own weights. 2 Both the explanation of this section and the next one, and the corresponding figures, are inspired by this blog [12]. Figure 2. Illustration of the transformer as a black box Figure 3. Illustration of the internal structure of the Transformer, consisting of an encoder and a decoder 9 At the same time, each encoder layer is made up of two sub-layers, the Self- Attention and the Feed Forward Layer, while the decoder layers are composed of the same sublayers but with one extra sub-layer in between, the Encoder- Decoder Attention layer. When an input sentence is given to the encoder, the first step is turning each word into a vector, since the system cannot work with strings. An embedding layer is used to achieve that. Basically, this layer relates each word of the vocabulary with a vector, which is also trained as part of the network. Information about the sequence order is added to each word through positional encoding before they are introduced to the first encoder layer. Once in the first encoder layer, all the words of the input sentence pass through the Self-Attention layer. Basically, this layer allows the system to pay attention to all the words in the same sentence while it encodes a specific word. In this layer, Figure 4. Illustration of the different layers that make up the encoder and decoder when n equals 6, which is the common quantity Figure 5. Overview if we consider the case of having 2 encoders and decoders 16 3.1 Adding pos tag and gender classifiers Besides creating these Linear layers, we also have to add dictionaries that relate the possible pos tags and gender labels with a numerical index. We call these dictionaries the tgt_labels_dict and the tgt_gender_dict, respectively. Thanks to these dictionaries, after applying the softmax and argmax functions, the system is capable of getting the pos tag and the gender, just exactly as in the case of words. Furthermore, we need the target sentences with their respective pos tags and gender labels, so that we can compute the respective losses. In order to do that, we have to introduce some changes in the Language Pair Dataset. The basic idea is to obtain two other arrays like the one that contains the ids of the words, but in this case with the ids corresponding to each pos tag and gender label. Thereby, we can compare the predictions made by the Transformer with the correct labels in each case and calculate the corresponding loss. In order to obtain the information of the pos tags and gender, we use a previously trained pipeline that allows processing raw text and obtaining grammatical and morphological information for each word. At this point, we have two problems to address. The first one is that we need the target words to use the pipeline, but what we originally get from the Language Pair Dataset are the ids of the words. To solve that, we create a function to convert the ids into words. Basically, this function accesses the target dictionary through the ids to obtain the words, appends the words into an array and then converts this array into a string. The second problem we have to overcome is that the target sentences are preprocessed in such a way that some words are splitted into subwords with BPE Figure 11. Illustration of the system in the case of adding at the same time the pos tag and gender classifiers 17 symbols [17]. That means that from the function above mentioned we obtain something like this: “The mach@@ in@@ ist gave someone a las@@ er cu@@ tter demonstration.” The problem here is that we also need our output array to have the same dimension as the target sentence, so that we can calculate the loss. That means we need a pos tag and a gender label for each subword to keep the dimension. The strategy here is to join the subwords and remove the BPE symbol to form the complete words. Then, pass these words through the pipeline to get the tags and, finally, associate to each subword the tag corresponding to the word they form. For example, in the case of the sentence mentioned above, we will first get the following words: “machinist” “laser” “cutter” We will pass them through the pipeline and obtain their respective tags. If we look at the pos tags, in this case the three words are nouns. Then, we have to associate the pos tags to each subword. To do this, we create a function called match_tags. This function has as inputs an array with the words, an array with the words splitted in subwords but removing the BPE symbol, and the array of tags from the pipeline. It compares the words of the first two arrays component by component and, if they match, it adds the corresponding tag to a new array of tags. When they do not match because a word is separated into subwords, it joins them until the complete word is obtained. In this case, the same tag is added to the array of tags as many times as the number of subwords into which the word is divided. With this we obtain an array of tags with the appropriate dimension, but we need to work with ids instead of working with strings. At this point we use the tgt_labels_dict or the tgt_gender_dict to convert this array of strings into another with the corresponding ids. 18 Figure 12. Diagram illustrating the process of obtaining the ids of the pos tags 19 3.2 Sharing the pos tag classifier between languages In general, it can be considered that pos tags do not vary too much from one language to another. For example, the word "house" in English, referring to a building for human habitation, is a noun in all the languages we are working with. For this reason, we also want to see if sharing this classifier between all languages has any positive influence on the results, that is, if the Linear layer and its weights are the same in the decoders of all the languages. In order to do that, we have to define the Linear layer of the pos tags as a variable that we pass to the decoders of all languages as an input during training. This way, whatever the target language, the Linear layer is always the same. Gender tends to vary more when translating a word from one language to another, therefore there is no point in sharing the weights of this layer. Figure 13. Illustration of the system in the case of sharing the pos tag classifier Figure 14. Illustration of the system in the case of adding both classifiers, but sharing the pos tag classifier 20 Chapter 4 Implementation 4.1 Fairseq The Transformer implementation that is used in this work is the one provided by Fairseq [18]. This is a sequence modeling toolkit written in PyTorch that allows to train models in different text generation tasks, such as translation. Fairseq can work with 5 types of plug-ins:  Models: wrap all of the parameters to be learned and define the network architecture  Criterions: compute the loss function.  Tasks: initialize the Model/Criterion and help with the loading and iteration over Datasets, the computing of the loss and the storage of dictionaries.  Optimizers: use the gradients to tweak the parameters.  Learning Rate Schedulers: update the learning rate while training. In our baseline, the model is called Interlingua Transformer, the criterion is the Label Smoothed Cross Entropy, and the task is the Interlingua Nodistance Translation. During this project, we make some changes to these parts of the code in order to adapt the model to our goals. As for the optimizer and the learning rate scheduler we use Adam [19] and Inverse Square Root, respectively. Fairseq also provides several command-line tools for evaluating models, such as Generate and Interactive. Generate allows you to translate pre-processed and binarized data and get the BLEU score, while Interactive allows translating raw text. We will use the latter to obtain the translations we need for the gender bias assessment. 4.2 Datasets For all the different configurations presented in this project, we employ the EuroParl [20] corpus for training and Newstest2012 and Newstest2013 from WMT 3 for validation and test. All the different configurations are trained and evaluated for all the possible combinations of English, Spanish, French and German using these corpuses, which were given to us already binarized and pre-processed with Moses [21]. 3 http://www.statmt.org/ 21 4.3 The Baseline Model As a baseline model for this project we use the multilingual machine translation model presented in this paper [7]. This model allows training language-specific encoders-decoders without sharing any parameters at all. Its main advantage is that, when adding new languages to the system, it is not necessary to retrain it and the quality of the languages already in the system does not change. 4.4 Stanza In order to get the pos tags and the gender of the target sentences we use Stanza [22], a Python natural language analysis package. Stanza contains different tools for text analysis, such as the ones we are interested in: pos and morphological features tagging (this last one containing gender). These tools can be used in a pipeline and Stanza offers pretrained models for a wide range of languages, including the ones we are using in this project. This package is based on the Universal Dependencies (UD) formalism [23]. UD is a framework for consistent annotation of grammar (parts of speech, morphological features and syntactic dependencies) across different languages. According to Universal Dependencies, there is a fixed list of 17 possible pos tags for all languages (although it is possible that some tags will not be used in some languages) and the gender can take 4 different values depending on the language. For example, in English the gender can be masculine, feminine or neuter (e.g. the word “it”), while in French it can only be masculine or feminine. However, to generalize, we consider that for all languages the list of possible gender labels is also fixed, encompassing all the possibilities considered by UD. Figure 15. Overview of Stanza’s neural network NLP pipeline. Source: [22] 22 List of possible pos tags:  ADJ  ADP  ADV  AUX  CCONJ  DET  INTJ  NOUN  NUM  PART  PRON  PROPN  PUNCT  SCONJ  SYM  VERB  X List of possible gender:  Fem  Masc  Neut  Com Example using Stanza 4 4 We are interested in the information the pos processor provides, but in order to invoke this processor it is necessary to invoke also the Tokenize and the Multi-Word Token processors. [ { "id": 1, "text": "She", "upos": "PRON", "xpos": "PRP", "feats": "Case=Nom|Gender=Fem|Number=Sing|Person=3|PronType=Prs", "misc": "start_char=0|end_char=3" }, { "id": 2, "text": "told", "upos": "VERB", "xpos": "VBD", "feats": "Mood=Ind|Tense=Past|VerbForm=Fin", "misc": "start_char=4|end_char=8" }, { "id": 3, "text": "me", "upos": "PRON", "xpos": "PRP", "feats": "Case=Acc|Number=Sing|Person=1|PronType=Prs", "misc": "start_char=9|end_char=11" } ] [ { "id": 1, "text": "She", "upos": "PRON", "xpos": "PRP", "feats": "Case=Nom|Gender=Fem|Number=Sing|Person=3|PronType=Prs", 23 As we can see in this example, the pos tags of the target sentences are easily obtained by directly accessing the upos object. This is different in the case of gender, which is given by the feats object along with other characteristics of the word. These characteristics are separated by a slash and they only appear in case the word has information about it. However, our model will predict a gender label for each word in the output sentence even if the word is not actually gendered. For this reason, we have added the label “NoGender” to the list of possible gender tags. Therefore, in order to get the gender of the target sentences we have created a function that extracts that information from the feats object and sets “NoGender” as the label in the cases that require it. Taking this into account, the Linear layer of the pos tags will project the output of the last decoder into a vector of dimension 17, while that of the gender will do it into a vector of dimension 5 (the four possibilities considered by UD plus the “NoGender” label added by us). 24 Chapter 5 Results In this chapter we will evaluate the quality of the translation of the different models that we have proposed and also the gender bias that these produce. 5.1 Bilingual case: English – Spanish Although our baseline model is a multilingual model, during the training process of the different configurations we tried to also train all the models (except the shared ones) in the English-Spanish bilingual case. The bilingual case training takes a few days while the multilingual one takes a few weeks 5 . Doing this allowed us to verify that our models worked correctly and that the quality of the translation was not too affected by the changes added with respect to the baseline. Baseline Pos Gender Pos & Gender 28.38 28.42 28.71 27.78 Table 1. BLEU results of the different models for the English-Spanish bilingual case. Best result is marked in bold. Regarding the quality of the translation, we found that the BLEU score was around the same value in all the cases. In the model in which only the gender classifier was added, the result was positive with an improvement of 0.33 points compared to the baseline, while in the case of the pos tag classifier it was also better but insignificant. In the case of both classifiers, the result is worse compared to the baseline but the quality is not seriously harmed either, decreasing the BLEU score just by 2.11%. Acc ↑ ∆G ↓ ∆S ↓ M:F ↓ Baseline 58.6 11.5 14.6 2.47 Pos 52.8 20.5 12.4 3.46 Gender 57.6 12 19.2 2.53 Pos & Gender 52.2 21.2 13.1 3.51 Table 2. Evaluation of the gender bias produced by the different models in the English-Spanish bilingual case. Best results are marked in bold. As for the gender bias evaluation, the results are generally worse compared to the baseline. The only case in which we could consider an improvement is with 5 We cannot quantify the total training time as the remote machine we were working on stopped the training several times due to storage problems. When running it again, the training continued where it had stopped, but we lost the information regarding the total time. 25 respect to ∆S in the case of Pos and Pos & Gender, but this metric is not reliable when the M:F ratio is large [16], so we proceed to break it down. Pro-Acc Anti-Acc Baseline 72.4 57.8 Pos 64.7 52.3 Gender 73.4 54.2 Pos & Gender 64.2 51.1 Table 3. Accuracy results of the pro and anti-stereotypical sets in the English-Spanish bilingual case. Best results are marked in bold. On the one hand, in the cases in which ∆S is lower it is because the accuracy in the pro-stereotypical case worsens more than what the anti-stereotypical case worsens, but in any of these cases it improves with respect to the baseline. On the other hand, the gender classifier causes an improvement in accuracy over the pro-stereotypical set while also making it worse in the anti-stereotypical case like the rest of the models. This fact indicates that, contrary to what was wanted to be obtained, information about gender makes stereotypes perpetuate even more. This may be due to the gender bias already present in the datasets used for training and that the system has learned it better by having more information about it. All in all, despite the apparent improvement in ∆S, we can conclude that in the bilingual case none of the proposed models helps to reduce gender bias, as indicated by the rest of the metrics. 5.2 Translation quality In Table 4 we compare the BLEU results for all the different models and for all language pairs. On average, only the case with gender improves very slightly the quality of the translation. And for the rest, in the worst case the average BLEU worsens by less than 0.7%. Therefore, we can consider that quality is not significantly affected in any of the cases. Model en-es en-fr en-de es-en es-fr es-de fr-en fr-es fr-de de-en de-es de-fr Average Baseline 29.48 29.56 21.5 27.38 30.25 19.61 26.03 29.04 18.96 24.04 24.95 25.24 25.503 Pos 29.06 29.27 21.4 27.16 29.79 19.43 25.92 28.89 18.91 24.24 24.84 24.99 25.325 Gender 29.38 29.56 21.81 27.11 30.05 19.84 26.13 29.09 18.99 24.07 24.86 25.16 25.504 Pos&Gender 29.12 29.37 21.59 26.92 29.9 19.48 26.54 29.2 19.24 24.23 24.91 24.96 25.455 Sharing Pos 29.24 29.33 21.58 26.95 30.19 19.77 25.9 29.14 19.07 23.92 24.89 25.01 25.416 Sharing Pos + Gender 29.36 29.46 21.49 26.99 30.04 19.38 25.86 29.09 18.92 23.96 24.88 25.18 25.384 Table 4. BLEU results of the different models and the different language pairs. Best results are marked in bold 32 [22] P. Qi, Y. Zhang, Y. Zhang, J. Bolton and C. D. Manning, "Stanza: A Python Natural Language Processing Toolkit for Many Human Languages," arXiv preprint arXiv: 2003.07082, 2020. [23] " Universal Dependencies," [Online]. Available: https://universaldependencies.org/. [Accessed June 2021]. 33 List of Figures Figure 1. Example of a translation from English to Spanish made with Google Translate ............................................................................................................ 5 Figure 2. Illustration of the transformer as a black box ....................................... 8 Figure 3. Illustration of the internal structure of the Transformer, consisting of an encoder and a decoder ...................................................................................... 8 Figure 4. Illustration of the different layers that make up the encoder and decoder when n equals 6, which is the common quantity .................................. 9 Figure 5. Overview if we consider the case of having 2 encoders and decoders 9 Figure 6. First output produced by the decoder ................................................ 10 Figure 7. Second output produced by the decoder. This time, the first output has been fed into the decoder as input ................................................................... 11 Figure 8. Probability distributions of a target sentence and its prediction ......... 12 Figure 9. Illustration of the system in the case of adding a pos tag classifier ... 15 Figure 10. Illustration of the system in the case of adding a gender classifier .. 15 Figure 11. Illustration of the system in the case of adding at the same time the pos tag and gender classifiers .......................................................................... 16 Figure 12. Diagram illustrating the process of obtaining the ids of the pos tags18 Figure 13. Illustration of the system in the case of sharing the pos tag classifier ......................................................................................................................... 19 Figure 14. Illustration of the system in the case of adding both classifiers, but sharing the pos tag classifier ............................................................................ 19 Figure 15. Overview of Stanza’s neural network NLP pipeline. Source: [22] .... 21 Figure 16. Representation of the relationships between gender accuracy, ∆G, ∆S & BLEU. Pearson’s correlation between them. Probability density function of each metric calculated with Kernel density estimation. .................................... 27 34 List of Tables Table 1. BLEU results of the different models for the English-Spanish bilingual case. Best result is marked in bold. .................................................................. 24 Table 2. Evaluation of the gender bias produced by the different models in the English-Spanish bilingual case. Best results are marked in bold. ..................... 24 Table 3. Accuracy results of the pro and anti-stereotypical sets in the English- Spanish bilingual case. Best results are marked in bold. ................................. 25 Table 4. BLEU results of the different models and the different language pairs. Best results are marked in bold ........................................................................ 25 Table 5. Evaluation on WinoMT test set for the different models. Best results are marked in bold ............................................................................................ 26 Table 6. Accuracy results of the pro and anti-stereotypical in French with the baseline and the model with pos tag and gender classifiers. Best results are marked in bold .................................................................................................. 26 Table 7. Comparison between translations made by the baseline and by our model with the classifiers of pos tag and gender. Orange indicate neutral words, red masculine words and blue feminine words ................................................. 28