SlowGuess/NAACL_2021_OCR huggingface dataset:
Lin Pan†, Chung-Wei Hang†, Haode Qi†, Abhishek Shah†, Saloni Potdar†, Mo Yu‡  
†IBM Watson  
‡MIT-IBM Watson AI Lab
{pan1,hangc}@us.ibm.com，{Haode.Qi，Abhishek.Shah1}@ibm.com，{potdars，yum}@us.ibm.com
We propose a simple method to align multilingual contextual embeddings as a postpretraining step for improved cross-lingual transferability of the pretrained language models. Using parallel data, our method aligns embeddings on the word level through the recently proposed Translation Language Modeling objective as well as on the sentence level via contrastive learning and random input shuffling. We also perform sentence-level code-switching with English when finetuning on downstream tasks. On XNLI, our best model (initialized from mBERT) improves over mBERT by  $4.7\%$  in the zero-shot setting and achieves comparable result to XLM for translate-train while using less than  $18\%$  of the same parallel data and  $31\%$  fewer model parameters. On MLQA, our model outperforms XLM- $\mathbf{R}_{\mathrm{Base}}$ , which has  $57\%$  more parameters than ours.
Building on the success of monolingual pretrained language models (LM) such as BERT (Devlin et al., 2019) and RoBERTa (Liu et al., 2019), their multilingual counterparts mBERT (Devlin et al., 2019) and XLM-R (Conneau et al., 2020) are trained using the same objectives—Masked Language Modeling (MLM) and in the case of mBERT, Next Sentence Prediction (NSP). MLM is applied to monolingual text that covers over 100 languages. Despite the absence of parallel data and explicit alignment signals, these models transfer surprisingly well from high resource languages, such as English, to other languages. On the Natural Language Inference (NLI) task XNLI (Conneau et al., 2018), a text classification model trained on English training data can be directly applied to the other 14 languages and achieve respectable performance. Having a single model that can serve over 100 languages also has important business applications.
Recent work improves upon these pretrained models by adding cross-lingual tasks leveraging parallel data that always involve English. Conneau and Lample (2019) pretrain a new Transformer-based (Vaswani et al., 2017) model from scratch with an MLM objective on monolingual data, and a Translation Language Modeling (TLM) objective on parallel data. Cao et al. (2020) align mBERT embeddings in a post-hoc manner: They first apply a statistical toolkit, FastAlign (Dyer et al., 2013), to create word alignments on parallel sentences. Then, mBERT is tuned via minimizing the mean squared error between the embeddings of English words and those of the corresponding words in other languages. Such post-hoc approach suffers from the limitations of word-alignment toolkits: (1) the noises from FastAlign can lead to error propagation to the rest of the pipeline; (2) FastAlign mainly creates the alignments with word-level translation and usually overlooks the contextual semantic compositions. As a result, the tuned mBERT is biased to shallow cross-lingual correspondence. Importantly, both approaches only involve word-level alignment tasks.
In this work, we focus on self-supervised, alignment-oriented training tasks using minimum parallel data to improve mBERT's cross-lingual transferability. We propose a Post-Pretraining Alignment (PPA) method consisting of both word-level and sentence-level alignment, as well as a finetuning technique on downstream tasks that take pairs of text as input, such as NLI and Question Answering (QA). Specifically, we use a slightly different version of TLM as our word-level alignment task and contrastive learning (Hadsell et al., 2006) on mBERT's [CLS] tokens to align sentence-level representations. Both tasks are self-supervised and do not require pre-alignment tools such as FastAlign. Our sentence-level alignment is implemented using MoCo (He et al., 2020), an instance discrimination-based method of contrastive learn
Proceedings of the 2021 Conference of the North American Chapter of the Association for Computational Linguistics: Human Language Technologies, pages 210-219 June 6-11, 2021. ©2021 Association for Computational Linguistics
Figure 1: Model structure for our Post-Pretraining Alignment method using parallel data. We use MoCo to implement our sentence-level objective and TLM for our word-level objective. The model is trained in a multi-task manner with both objectives.
ing that was recently proposed for self-supervised representation learning in computer vision. Lastly, when finetuning on NLI and QA tasks for non-English languages, we perform sentence-level code-switching with English as a form of both alignment and data augmentation. We conduct controlled experiments on XNLI and MLQA (Lewis et al., 2020), leveraging varying amounts of parallel data during alignment. We then conduct an ablation study that shows the effectiveness of our method. On XNLI, our aligned mBERT improves over the original mBERT by  $4.7\%$  for zero-shot transfer, and outperforms Cao et al. (2020) while using the same amount of parallel data from the same source. For translate-train, where translation of English training data is available in the target language, our model achieves comparable performance to XLM while using far fewer resources. On MLQA, we get  $2.3\%$  improvement over mBERT and outperform XLM- $\mathbf{R}_{\mathrm{Base}}$  for zero-shot transfer.
This section introduces our proposed Post-Pretraining Alignment (PPA) method. We first describe the MoCo contrastive learning framework and how we use it for sentence-level alignment. Next, we describe the finer-grained word-level alignment with TLM. Finally, when training data in the target language is available, we incorporate sentence-level code-switching as a form of both alignment and data augmentation to complement
PPA. Figure 1 shows our overall model structure.
Background: Contrastive Learning Instance discrimination-based contrastive learning aims to bring two views of the same source image closer to each other in the representation space while encouraging views of different source images to be dissimilar through a contrastive loss. Recent advances in this area, such as SimCLR (Chen et al., 2020) and MoCo (He et al., 2020) have bridged the gap in performance between self-supervised representation learning and fully-supervised methods on the ImageNet (Deng et al., 2009) dataset. As a key feature for both methods, a large number of negative examples per instance are necessary for the models to learn such good representations. SimCLR uses in-batch negative example sampling, thus requiring a large batch size, whereas MoCo stores negative examples in a queue and casts the contrastive learning task as dictionary (query-key) lookup. In what follows, we first describe MoCo and then how we use it for sentence-level alignment.
Concretely, MoCo employs a dual-encoder architecture. Given two views  $v_{1}$  and  $v_{2}$  of the same image,  $v_{1}$  is encoded by the query encoder  $f_{q}$  and  $v_{2}$  by the momentum encoder  $f_{k}$ .  $v_{1}$  and  $v_{2}$  form a positive pair. Negative examples are views of different source images, and are stored in a queue  $\in K$ , which is randomly initialized.  $K$  is usually a large number (e.g.,  $K = 65$ , 536 for ImageNet). Negative pairs are formed by comparing  $v_{1}$  with each item in the queue. Similarity between pairs is measured by dot product. MoCo uses the InfoNCE loss (van den Oord et al., 2019) to bring positive pairs closer to each other and push negative pairs apart. After a batch of view pairs are processed, those encoded by the momentum encoder are added to the queue as negative examples for future queries. During training, the query encoder is updated by the optimizer while the momentum encoder is updated by the exponential moving average of the query encoder's parameters to maintain queue consistency:
$$
\theta_ {k} = m \theta_ {k} + (1 - m) \theta_ {q} \tag {1}
$$
where  $\theta_q$  and  $\theta_k$  are model parameters of  $f_{q}$  and  $f_{k}$ , respectively.  $m$  is the momentum coefficient.
2.1 Sentence-Level Alignment Objective
Our sentence-level alignment falls under the general problem of bringing two views of inputs from the same source closer in the representation
space while keeping those from different sources dissimilar through a contrastive loss. From a crosslingual alignment perspective, we treat an English sequence  $S_{i}^{en}$  and its translation  $S_{i}^{tr}$  in another language  $tr \in L$  as two manifestations of the same semantics. At the same time, sentences that are not translations of each other should be further apart in the representation space. Given parallel corpora consisting of  $\{(S_{1}^{en}, S_{1}^{tr}), \ldots, (S_{N}^{en}, S_{N}^{tr})\}$ , we align sentence representations in all the different languages together using MoCo.
We use the pretrained mBERT model to initialize both the query and momentum encoders. mBERT is made of 12 Transformer blocks, 12 attention heads, and hidden size  $d_h = 768$ . For input, instead of feeding the query encoder with English examples and the momentum encoder with translation examples or vice versa, we propose a random input shuffling approach. Specifically, we randomly shuffle the order of  $S_i^{en}$  and  $S_i^{tr}$  when feeding the two encoders, so that the query encoder sees both English and translation examples. We observe that this is a crucial step towards learning good multilingual representations using our method. The final hidden state  $h \in \mathbb{R}^{1 \times d_h}$  of the [CLS] token, normalized with  $L_2$  norm, is treated as the sentence representation<sup>1</sup>. Following Chen et al. (2020), we add a non-linear projection layer on top of  $h$ :
$$
z = W _ {2} \operatorname {R e} L U \left(W _ {1} h\right), \tag {2}
$$
where  $W_{1}\in \mathbb{R}^{d_{h}\times d_{h}}$ ,  $W_{2}\in \mathbb{R}^{d_{k}\times d_{h}}$ , and  $d_{k}$  is set to 300. The model is trained using the InfoNCE loss:
$$
\mathcal {L} _ {\mathrm {M o C o}} = - \log \frac {\exp \left(z _ {q} \cdot z _ {k +} / \tau\right)}{\sum_ {k = 1} ^ {K} \exp \left(z _ {q} \cdot z _ {k} / \tau\right)}, \tag {3}
$$
where  $\tau$  is a temperature parameter. In our implementation, we use a relatively small batch size of 128, resulting in more frequent parameter updates than if a large batch size were used. Items enqueued early on can thus become outdated with a large queue, so we scale down the queue size to  $K = 32,000$  to prevent the queue from becoming stale.
2.2 Word-Level Alignment Objective
We use TLM for word-level alignment. TLM is an extension of MLM that operates on bilingual data
parallel sentences are concatenated and MLM is applied to the combined bilingual sequence. Different from Conneau and Lample (2019), we do not reset positional embeddings when forming the bilingual sequence, and we also do not use language embeddings. In addition, the order of  $S_{i}^{en}$  and  $S_{i}^{tr}$  during concatenation is determined by the random input shuffling from the sentence-level alignment step and we add a [SEP] token between  $S_{i}^{en}$  and  $S_{i}^{tr}$ .
We randomly mask  $15\%$  of the WordPiece tokens in each combined sequence. Masking is done by using a special [MASK] token  $80\%$  of the times, a random token in the vocabulary  $10\%$  of the times, and unchanged for the remaining  $10\%$ . TLM is performed using the query encoder of MoCo. Our final PPA model is trained in a multi-task manner with both sentence-level objective and TLM:
$$
\mathcal {L} = \mathcal {L} _ {\mathrm {M o C o}} + \mathcal {L} _ {\mathrm {T L M}}, \tag {4}
$$
2.3 Finetuning on Downstream Tasks
After an alignment model is trained with PPA, we extract the query encoder from MoCo and finetune it on downstream tasks for evaluation. We follow the standard way of finetuning BERT-like models for sequence classification and QA tasks: (1) on XNLI, we concatenate the premise with the hypothesis, and add a [SEP] token in between. A softmax classifier is added on top of the final hidden state of the [CLS] token; (2) on MLQA, we concatenate the question with the context, and add a [SEP] token in between. We add two linear layers on top of mBERT followed by softmax over the context tokens to predict answer start and end positions, respectively.
We conduct experiments in two settings: 1. Zero-shot cross-lingual transfer, where training data is available in English but not in target languages. 2. Translate-train, where the English training set is (machine) translated to all the target languages. For the latter setting, we perform data augmentation with code-switched inputs, when training on languages other than English. For example, a Spanish question  $q_{es}$  and context  $c_{es}$  pair can be augmented to two question-context pairs  $(q_{es}, c_{en})$  and  $(q_{en}, c_{es})$  with code-switching, resulting in 2x training data<sup>2</sup>. The same goes for XNLI with premises and hypotheses. The code-switching is always between English, and a target language. During training, we
2The original question-context pair  $(q_{es}, c_{es})$  is not used for training as it did not help improve model performance in our experiments.
Alternatively, we also experimented with mean-pooling of the last layer's embeddings as the sentence representation, but it performed slightly worse than using the [CLS] token.
ensure the two augmented pairs appear in the same batch.
3.1 Parallel Data for Post-Pretraining
Parallel Data All parallel data we use involve English as the source language. Specifically, we collect en-fr, en-es, en-de parallel pairs from Europarl, en-ar, en-zh from MultiUN (Ziemski et al., 2016), en-hi from IITB (Kunchukuttan et al., 2018), and en-bg from both Europarl and EUbookshop. All datasets were downloaded from the OPUS<sup>3</sup> website (Tiedemann, 2012). In our experiments, we vary the number of parallel sentence pairs for PPA. For each language, we take the first 250k, 600k, and 2M English-translation parallel sentence pairs except for those too short (where either sentence has less than 10 WordPiece tokens), or too long (where both sentences concatenated together have more than 128 WordPiece tokens). Table 1 shows the actual number of parallel pairs in each of our 250k, 600k, and 2M settings.
3.2 Evaluation Benchmarks
XNLI is an evaluation dataset for cross-lingual NLI that covers 15 languages. The dataset is human-translated from the development and test sets of the English MultiNLI dataset (Williams et al., 2018). Given a sentence pair of premise and hypothesis, the task is to classify their relationship as entailment, contradiction, and neutral. For zero-shot cross-lingual transfer, we train on the English MultiNLI training set, and apply the model to the test sets of the other languages. For translate-train, we train on translation data that come with the dataset  $^{4}$ .
MLQA is an evaluation dataset for QA that covers seven languages. The dataset is derived from a three step process. (1) Parallel sentence mining from Wikipedia of the languages. (2) English question annotation and answer span annotation on English context. (3) Professional translation of English questions to the other languages as well as answer span annotation. MLQA has two evaluation tasks: (a) Cross-lingual transfer (XLT), where the question and context are in the same language. (b) Generalized cross-lingual transfer (G-XLT), where the question and context are in different languages.
We focus on XLT in this work. For zero-shot cross-lingual transfer, we train on the English SQuAD v1.1 (Rajpurkar et al., 2016) training set. For translate-train, we train on translation data provided in Hu et al. (2020)
3.3 Training Details
For both PPA and finetuning on downstream tasks, we use the AdamW optimizer with 0.01 weight decay and a linear learning rate scheduler. For PPA, we use a batch size of 128, mBERT max sequence length 128 and learning rate warmup for the first  $10\%$  of the total iterations, peaking at 0.00003. The MoCo momentum is set to 0.999, queue size 32000 and temperature 0.05. Our PPA models are trained for 10 epochs, except for the 2M setting where 5 epochs are trained. On XNLI, we use a batch size of 32, mBERT max sequence length 128 and finetune the PPA model for 2 epochs. Learning rate peaks at 0.00005 and warmup is done to the first 1000 iterations. On MLQA, mBERT max sequence length is set to 386 and peak learning rate 0.00003. The other parameters are the same as XNLI. Our experiments are run on a single 32 GB V100 GPU, except for PPA training that involves either MLM or TLM, where two such GPUs are used. We also use mixed-precision training to save on GPU memory and speed up experiments.
We report results on the test set of XNLI and MLQA and we do hyperparameter searching on the development set. All the experiments for translate-train were done using the code-switching technique introduced in Section 2.
XNLI Table 2 shows results on XNLI measured by accuracy. Devlin et al. (2019) only provide results on a few languages $^6$ , so we use the mBERT results from Hu et al. (2020) as our baseline for zero-shot cross-lingual transfer, and Wu and Dredze (2019) for translate-train. Our best model, trained with 2M parallel sentences per language improves over mBERT baseline by  $4.7\%$  for zero-shot transfer, and  $3.2\%$  for translate-train.
Compared to Cao et al. (2020), which use 250k parallel sentences per language from the same sources as we do for post-pretraining alignment,
<sup>3</sup>http://opus.nlpl.eu/
4https://cims.nyu.edu/\~sbowman/xnli/
5https://github.com/google-research/extreme
$^{6}$ https://github.com/google-research/bert/blob/master/multilingual.md
Resource	fr	es	de	bg	ar	zh	hi	total
Original data
MultiUN	14.2M	12.2M	-	-	10.6M	10.5M	-
Europarl	2.1M	2.0M	2.0M	0.4M	-	-	-
EUbookshop	-	-	9.6M	0.2M	-	-	-
IITB	-	-	-	-	-	-	1.6M
Considered in this paper
MultiUN	-	-	-	-	10.6M	10.5M	-
Europarl	2.1M	2.0M	2.0M	0.4M	-	-	-
EUbookshop	-	-	-	0.2M	-	-	-
IITB	-	-	-	-	-	-	1.6M
Total	2.1M	2.0M	2.0M	0.6M	10.6M	10.5M	1.6M
Used for our post-pretraining alignment (PPA)
Ours (250k)	250k	250k	250k	250k	250k	250k	250k	1.8M
Ours (600k)	600k	600k	600k	467k	600k	600k	600k	4.1M
Ours (2M)	1.8M	1.7M	1.7M	467k	2.0M	2.0M	0.8M	10.5M
Used by other approaches
Cao et al. (2020)a	250k	250k	250k	250k	250k	250k	250k	1.8M
Artetxe and Schwenk (2019)b	-	-	-	-	-	-	-	223M
XLM (Conneau and Lample, 2019)c	14.2M	12.2M	9.6M	0.2M	10.6M	10.5M	1.6M	58.9M
${}^{a}$  Cao et al. (2020) uses the same  ${250}\mathrm{k}$  parallel corpora as our  ${250}\mathrm{k}$  setting,thus giving an apple-to-apple comparison.
$^b$  Artetxe and Schwenk (2019)'s number includes a total of 93 languages.
cWe only list the number of parallel sentences XLM uses for the languages we consider.
Table 1: Parallel data statistics. All parallel data involve English as source language. We use Europarl for en-fr, enes, and en-de, both Europarl and EUbookshop for en-bg, MultiUN for en-ar, en-zh, and IITB for en-hi. Our 250k setting uses an equal amount of data from the same source as Cao et al. (2020). Our 2M setting uses approximately  $63\%$  and  $17.8\%$  of the parallel data Artetxe and Schwenk (2019) and Conneau and Lample (2019) use, respectively.
our 250k model does better for all languages considered and we do not rely on the word-to-word pre-alignment step using FastAlign, which is prone to error propagation to the rest of the pipeline.
Compared to XLM, our 250k, 600k and 2M settings represent  $3.1\%$ ,  $7\%$  and  $17.8\%$  of the parallel data used by XLM, respectively (see Table 1). The XLM model also has  $45\%$  more parameters than ours as Table 3 shows. Furthermore, XLM trained with MLM only is already significantly better than mBERT even though the source of its training data is the same as mBERT from Wikipedia. One reason could be that XLM contains  $45\%$  more model parameters than mBERT as model depth and capacity are shown to be key to cross-lingual success (K et al., 2020). Additionally, Wu and Dredze (2019) hypothesize that limiting pretraining to the languages used by downstream tasks may be beneficial since XLM models are pretrained on the 15 XNLI languages only. Our 2M model bridges the gap between mBERT and XLM from  $7.5\%$  to  $2.8\%$  for zero-shot transfer. Note that, for bg, our total processed pool of en-bg data consists of 456k parallel sentences, so there is no difference in en-bg data between our 600k and 2M settings. For translate-train, our model achieves comparable performance
to XLM with the further help of code-switching during finetuning.
Our alignment-oriented method is, to a large degree, upper-bounded by the English performance, since all our parallel data involve English and all the other languages are implicitly aligning with English through our PPA objectives. Our 2M model is able to improve the English performance to 82.4 from the mBERT baseline, but it is still lower than XLM (MLM), and much lower than XLM (MLM+TLM). We hypothesize that more high-quality monolingual data and model capacity are needed to further improve our English performance, thereby helping other languages better align with it.
MLQA Table 4 shows results on MLQA measured by F1 score. We notice the mBERT baseline from the original MLQA paper is significantly lower than that from Hu et al. (2020), so we use the latter as our baseline. Our 2M model outperforms the baseline by  $2.3\%$  for zero-shot and is also  $0.2\%$  better than XLM- $\mathbf{R}_{\mathrm{Base}}$ , which uses  $57\%$  more model parameters than mBERT as Table 3 shows. For translate-train, our 250k model is  $1.3\%$  better than the baseline.
Comparing our model performance using vary
Model	en	fr	es	de	bg	ar	zh	hi	avg
Zero-shot cross-lingual transfer
mBERT (Devlin et al., 2019)	81.4	-	74.3	70.5	-	62.1	63.8	-	-
mBERT from (Hu et al., 2020)	80.8	73.4	73.5	70.0	68.0	64.3	67.8	58.9	69.6
Cao et al. (2020)	80.1	74.5	75.5	73.1	73.4	-	-	-	-
Artetxe and Schwenk (2019)	73.9	71.9	72.9	72.6	74.2	71.4	71.4	65.5	71.7
Ours (250k)	82.4	75.5	76.2	73.3	74.6	68.2	71.7	62.8	73.1
Ours (600k)	82.4	76.7	76.4	74.0	74.1	69.1	72.3	66.9	74.0
Ours (2M)	82.8	76.6	76.7	74.2	73.8	70.3	72.8	66.9	74.3
XLM (MLM)	83.2	76.5	76.3	74.2	74.0	68.5	71.9	65.7	73.8
XLM (MLM + TLM)	85.0	78.7	78.9	77.8	77.4	73.1	76.5	69.6	77.1
Translate-train
mBERT (Devlin et al., 2019)	81.9	-	77.8	75.9	-	70.7	76.6	-	-
mBERT from (Wu and Dredze, 2019)	82.1	76.9	78.5	74.8	75.4	70.8	76.2	65.3	75.0
Ours (250k)	82.4	78.8	79.0	78.7	78.4	74.0	77.9	69.6	77.4
Ours (600k)	82.4	79.7	79.7	77.9	79.0	75.2	77.8	71.5	77.9
Ours (2M)	82.8	79.7	80.6	78.6	78.8	75.2	78.0	72.0	78.2
XLM (Conneau and Lample, 2019)	85.0	80.2	80.8	80.3	79.3	76.5	78.6	72.3	79.1
Table 2: XNLI accuracy scores for each language. After alignment, our best model improves over mBERT by  $4.7\%$  for zero-shot transfer, and achieves comparable performance to XLM for translate-train. Artetxe and Schwenk (2019) use 223M parallel sentences covering 93 languages. XLM uses 58.9M parallel sentences (for the seven languages we consider) with  $40\%$  more parameters. Our approach (250k, 600k, and 2M per language) uses a total of 1.8M, 4.1M, and 10.5M parallel sentences, respectively.
Model	#langs	L	Hm	Hff	A	V	#params
mBERT	104	12	768	3072	12	110k	172M
XLM	15	12	1024	4096	8	95k	250M
XLM-RBase	100	12	768	3072	12	250k	270M
Ours	104	12	768	3072	12	110k	172M
Table 3: Model architecture and sizes from Conneau et al. (2020).  $L$  is the number of Transformer layers,  ${H}_{m}$  is the hidden size,  ${H}_{ff}$  is the dimension of the feed-forward layer,  $A$  is the number of attention heads, and  $V$  is the vocabulary size.
ing amounts of parallel data, we observe that 600k per language is our sweet spot considering the trade-off between resource and performance. Going up to 2M helps on XNLI, but less significantly compared to the gain going from 250k to 600k. On MLQA, surprisingly, 250k slightly outperforms the other two for translate-train.