# An Improved Transformer-Based Neural Machine Translation Strategy: Interacting-Head Attention

Authors: Dongxing Li; Zuying Luo
Journal: Computational Intelligence and Neuroscience
 (2022)

---

## Abstract

Transformer-based models have gained significant advances in neural machine translation (NMT). The main component of the transformer is the multihead attention layer. In theory, more heads enhance the expressive power of the NMT model. But this is not always the case in practice. On the one hand, the computations of each head attention are conducted in the same subspace, without considering the different subspaces of all the tokens. On the other hand, the low-rank bottleneck may occur, when the number of heads surpasses a threshold. To address the low-rank bottleneck, the two mainstream methods make the head size equal to the sequence length and complicate the distribution of self-attention heads. However, these methods are challenged by the variable sequence length in the corpus and the sheer number of parameters to be learned. Therefore, this paper proposes the interacting-head attention mechanism, which induces deeper and wider interactions across the attention heads by low-dimension computations in different subspaces of all the tokens, and chooses the appropriate number of heads to avoid low-rank bottleneck. The proposed model was tested on machine translation tasks of IWSLT2016 DE-EN, WMT17 EN-DE, and WMT17 EN-CS. Compared to the original multihead attention, our model improved the performance by 2.78 BLEU/0.85 WER/2.90 METEOR/2.65 ROUGE_L/0.29 CIDEr/2.97 YiSi and 2.43 BLEU/1.38 WER/3.05 METEOR/2.70 ROUGE_L/0.30 CIDEr/3.59 YiSi on the evaluation set and the test set, respectively, for IWSLT2016 DE-EN, 2.31 BLEU/5.94 WER/1.46 METEOR/1.35 ROUGE_L/0.07 CIDEr/0.33 YiSi and 1.62 BLEU/6.04 WER/1.39 METEOR/0.11 CIDEr/0.87 YiSi on the evaluation set and newstest2014, respectively, for WMT17 EN-DE, and 3.87 BLEU/3.05 WER/9.22 METEOR/3.81 ROUGE_L/0.36 CIDEr/4.14 YiSi and 4.62 BLEU/2.41 WER/9.82 METEOR/4.82 ROUGE_L/0.44 CIDEr/5.25 YiSi on the evaluation set and newstest2014, respectively, for WMT17 EN-CS.


---

## Body

## 1. Introduction

Bahdanau et al. were the first to introduce the attention mechanism to neural machine translation (NMT) along with recurrent neural networks (RNNs): the mechanism weighs the importance of each source token to produce the target token. By contrast, the traditional way predicts each target token in each time step, using the fixed-length context vector [1]. Kalchbrenner et al. [2] and Gehring et al. [3, 4] combined the attention mechanism with models based on the convolutional neural network (CNN) for NMT. Recently, the transformer-based models became fashionable solutions to sequence-to-sequence (seq2seq) problems like NMT [5–7], for they outperform RNN-based models and CNN-based models [1–4, 8, 9]. In every transformer-based model, the encoder-to-decoder structure is adopted to encode the source sequence into a series of hidden representations as the context vector, and the target sequence is then generated based on the context vector [5]. The encoder and decoder are connected through an attention layer.The transformer-based models, which solely depend on the attention mechanism, outshine the models grounded on RNNs and CNN, thanks to the use of the self-attention network (SAN). In practice, to further improve the expressive power, the transformer-based models employ the multihead self-attention mechanism. Each head projects the input into a lower-dimensional subspace and computes the corresponding attention relationship in that subspace. This projection size for each head is commonly referred to as the head size [10].However, this multihead attention mechanism has two problems. On the one hand, in theory, more heads make a model more expressive in natural language preprocessing (NLP). However, some scholars demonstrated that more heads do not necessarily lead to better performance. The low-rank bottleneck may arise, once the number of heads surpasses a certain threshold [10]. Namely, more heads generate redundant head information, increase the computational complexity of the model, cause feature redundancy, and reduce performance. Voita et al. [11] and Michel et al. [12] proved that only a small part of the heads is truly important for NMT, especially those in the encoder block. Important heads such as morphology, syntax, and low-frequency words serve multiple functions, while other heads only convey repeated and incomplete information. On the other hand, each head is independent without considering the mutual relationship of all heads. The calculation of each head attention is performed only in the same subspace but not in different subspaces. The multihead self-attention mechanism only concatenates all the results at the end.To avoid the low-rank bottleneck brought by more heads, Bhojanapalli et al. [10] brought the parameters of the low-dimensional space close to the attention matrix by increasing the key size to the sequence length for a subhead. Shazeer et al. [13] argued that, when the dimension of the subhead reaches the extreme level, the dot product between the query and key does not fit the informational matching function. To address this issue, the talking-head attention emerged. Under this mechanism, the attention can attend to any query and key, regardless of the number and dimensions of the subheads, by learning the linear projection matrices before and after the softmax function. However, both attention mechanisms are also conducted in the same subspace. Besides, the former approach may not improve the machine translation performance, resulting from the varied ranges of sequence lengths. For talking-head attention, more parameters have to be learned, as the attention head distribution becomes more complex.Therefore, it is necessary to resolve the maximum number of heads for avoiding the low-rank bottleneck and make full use of the interactive information of all heads. To attend to all subqueries and subkeys and prevent the low-rank bottleneck, this paper proposes the interacting-head attention mechanism, based on the following intuitions: (1) when there are relatively few heads, the attention relationship between the head sizes among different subspaces increases with the head size; (2) when there are relatively many heads, the attention relationship between the head sizes among different subspaces decreases with the head size and may be ignored in the most extreme case; (3) the right number of heads must be selected, because it is computationally intensive to calculate the head attention of all tokens in all spaces. The proposed interacting-head attention mechanism enables the head size to talk in the same subspaces and interact with each other in different subspaces. Furthermore, a suitable threshold was defined for the number of heads to control the training time and decoding time, while avoiding low-rank bottleneck and ensuring the head size.Our model was compared to three baseline multihead attention models on three evaluation datasets. The comparison proves that the interacting-head attention mechanism improves the translation performance and enhances the expressive power. On the dataset IWSLT2016 DE-EN, our model outperformed the original multihead attention model by 2.78 BLEU/0.85 WER/2.90 METEOR/2.65 ROUGE_L/0.29 CIDEr/2.97 YiSi and 2.43 BLEU/1.38 WER/3.05 METEOR/2.70 ROUGE_L/0.30 CIDEr/3.59 YiSi for the evaluation set and the test set, respectively. On the dataset WMT EN-DE, our model outperformed the original model by 2.31 BLEU/5.94 WER/1.46 METEOR/1.35 ROUGE_L/0.07 CIDEr/0.33 YiSi and 1.62 BLEU/6.04 WER/1.39 METEOR/0.11 CIDEr/0.87 YiSi, 1.21 BLEU/6.63 WER/1.42 METEOR/0.51ROUGE_L/0.18 CIDEr/0.52 YiSi, 1.39 BLEU/4.64 WER/0.98 METEOR/5.59 ROUGE_L/0.24 CIDEr/0.42 YiSi, 1.26 BLEU/3.84 WER/1.70 METEOR/0.13 CIDEr/1.30 YiSi for the evaluation set and the newstest2014/2015/2016/2017 test set, respectively. On the dataset WMT EN-CS, our model outperformed the original model by 3.87 BLEU/3.05 WER/9.22 METEOR/3.81 ROUGE_L/0.36 CIDEr/4.14 YiSi and 4.62 BLEU/2.41 WER/9.82 METEOR/4.82 ROUGE_L/0.44 CIDEr/5.25 YiSi, 3.78 BLEU/5.09 WER/9.09 METEOR/4.24 ROUGE_L/0.35 CIDEr/3.97 YiSi, 4.42 BLEU/2.87 WER/3.21 METEOR/4.42 ROUGE_L/0.38 CIDEr/4.83 YiSi, 3.42 BLEU/3.97 WER/2.79 METEOR/3.66 ROUGE_L/0.33 CIDEr/4.00 YiSi for the evaluation set and the newstest2014/2015/2016/2017 test set, respectively.This research makes the following contributions:(1)
Various types of attention mechanisms, which were used in RNNs, CNN, and transformers, were reviewed with mathematical expressions.(2)
The authors proposed a method to calculate the maximum number of heads. The method keeps the head size at a large level so that the head attention among the subspaces can be computed well. Moreover, the calculation method solves the low-rank bottleneck and prevents excessively long training and decoding time.(3)
For NMT, the interacting-head attention model for the transformer was proposed, under which all attention heads can fully communicate with each other.


## 2. Preliminaries

This section recaps the transformer architecture, which outshines RNNs and CNN in seq2seq tasks, reviews the background of various forms of attention, especially multihead attention used in transformer [5], analyzes the low-rank bottleneck induced by multihead attention in the standard transformer, and introduces the two mainstream solutions to low-rank bottleneck, as well as their problems in NMT.


### 2.1. Transformer

The transformer architecture resolves NMT solely by relying on the attention algorithm [5]. It has been proved that the transformer-based models are superior to the models using RNNs and CNN [1–4, 8, 9]. Like RNNs and CNN, the standard transformer-based model employs the encoder-to-decoder structure for NMT [14]. This structure maps the source sequence to a hidden state matrix as a natural language understanding (NLU) task and views the matrix elements as the context vectors or conditions for producing the target sequence. Encoder and decoder blocks are stacked in the encoder-to-decoder structure.Each encoder block usually comprises a multihead self-attention layer and a feedforward layer with residual connection [15], followed by a normalization layer [16]. As the core component of the encoder, the multihead self-attention layer captures the hidden representations of all the tokens within the source sequence. This operation mainly depends on the SAN, which learns the mutual attention score of any two tokens in the source sequence. It should be noted that the learned attention scores constitute an asymmetric square matrix, because of the learned parameters. For example, aij, the attention score from the i-th token to the j-th token, is not equal to aji, the attention score from the j-th token to the i-th token. Specifically, the SAN computes the attention scores by the scaled dot product attention algorithm. Since each token is visible to the others, the encoder can capture the feature of each token in two directions. There are two primary functions of the encoder: (1) learning the hidden representations of the input sequence as a condition for natural language generation (NLG) tasks, for example, NMT; (2) completing downstream NLP tasks, such as sentiment classification or labeling by transfer learning, after being trained independently as a masked language model (MLM) [17] and connected to specific networks.The decoder blocks have a similar structure as encoder blocks. The only difference lies in an additional sublayer, which computes the attention scores between the representations of the source sequence given by the encoder and the current target token representation given by the multihead SAN of the decoder. This sublayer, known as the encoder-decoder attention layer, is followed by a multihead attention layer. In the decoder, two attention mechanisms, namely, multihead self-attention and encoder-decoder attention, are arranged to capture the hidden state of the target token in each block. Since the token is only visible to its leftward tokens, the self-attention scores form a lower-dimensional triangular matrix. In other words, the multihead self-attention layer aims to focus the current target token only on the leftward tokens and mask the future tokens in the target sequence. In addition, the decoder learns the leftward token representations to generate the token probability distribution in each time step. During training, the probability distribution of the target token is computed based on the ground-truth leftward target tokens or their representations. All the representations are given by the encoder as a context vector for generating the target sequence. During inference, the current token probability distribution is computed based on the previous target token distribution. All the token representations are given by the encoder. The decoder works in a teacher-forcing way during training, while in an auto-regressive way during inference. The difference between the two stages is that the last token feature comes from the last ground-truth token and the last generated token given by the trained model, respectively.Because the attention mechanism is not order-aware, the transformer-based models add the positional information into the tokens, for example, absolute positional embedding.


### 2.2. Attention

For NMT, the translation performance hinges on the attention mechanism, in addition to the encoder-to-decoder structure. Bahdanau et al. [1] pioneered the use of the attention mechanism for NMT along with RNN. Sutskever et al. [8] and Luong et al. [9] further advanced the implementation of the attention mechanism in NMT. After the introduction of the attention mechanism, a target token no longer depends only on the same context vector. The different roles of the source token in target token generation are reflected. Along with the appearance of the transformer, complicated attention algorithms have been developed for specific NLP tasks, such as single head attention and multihead attention. Apart from linking up the encoder with the decoder, these algorithms learn the relationships in an end-to-end way.


#### 2.2.1. Dot Product Attention

Luong et al. explored the computing methods of an attention score, examined their effectiveness, divided attention mechanisms into global attention and local attention [9] (the former targets all the source tokens, while the latter considers the subset of all the source tokens), and designed three computing methods for the weight scores between two tensors or vectors along with RNNs for NMT. Here, some symbols used in Shazeer et al. [13] are adopted. Three computing methods can be expressed as(1)scorem,x=mTxdot,mTWxgeneral,tanhWm,xconcat,where m∈ℝd and x∈ℝd are the matching and matched column vectors, respectively; W∈ℝd×d is a learned parameter matrix; score⋅ is real. The larger the score, the more important x is to the generation of m. Dot-product attention is widely used for model implementation, by virtue of its fast speed and space efficiency [5]. In line with the notations given by Shazeer et al. [13], the attention between two sequences X∈ℝn×d and M∈ℝm×d is computed through a dot product operation.(2)X=XWX,M=MWM,AttentionX,M=softmaxX⋅MT,O=AttentionX,M⋅M,O^=O⋅Wo,where n and m are the length of X∈ℝn×d and M∈ℝm×d with the same dimension d, respectively. To keep the shape constant between the input and the output, O∈ℝn×d is regarded as the final output or mapped further to a lower or higher dimension with a linear projection matrix Wo∈ℝd×do to get the final output.


#### 2.2.2. Scaled Dot Product Attention

Scaled dot product attention is referred to as single head attention in this research. This attention mechanism projects the inputX into dk-dimensional queries Q and projects the other inputs M into dk-dimensional keys K and dv-dimensional values V. The increase of dk pushes up the dot products, which in turn make the softmax function converge into regions where it has extremely small gradients [5]. Therefore, the attention score is scaled with 1/dk.Firstly, it is necessary to explain the calculation of attention scores by a single head attention between two tensorsX∈ℝn×dX and M∈ℝm×dM, where the next projection operation is needed to deal with the dimensional difference. The matrices of queries Q∈ℝn×dk, keys K∈ℝm×dk, and values V∈ℝm×dv can be, respectively, obtained with the linear projection matrices Wq∈ℝdX×dk,Wk∈ℝdM×dk, and Wv∈ℝdM×dv on X, M, and M. The global computing can be defined as(3)Q=X•Wq,K=M•Wk,V=M•Wv,AttentionX,M=softmaxQKTdk,O=AttentionX,M•V,O^=O•Wo,where O∈ℝn×dv is the output. The O^ value is obtained following the last linear projection. If the self-attention scores are computed within a sequence, the linear projection matrices Wq,Wk,Wv,andWo must function on the same tensor; namely, X≡M. If X is different from M, the encoder-to-decoder attention scores should be calculated by the formula (3). Scaled dot production self-attention is applied in the SAN of the encoder and the decoder, as well as the encoder-decoder attention layer. In fact, Vaswani et al. [5] used a transformer to capture the token dependencies, relying on multihead scaled dot production attention.


#### 2.2.3. Multihead Attention

In the standard transformer, it is beneficial to split the representations into multiple heads and concatenate the subresults of heads in the end. This is because more heads elevate the expressive power and improve model performance. Both tensors are employed onX∈ℝn×dX and M∈ℝm×dM, where X represents the matching tensor and M represents the matched objective. The dimensions of queries, keys, and values are then split into h parts, which is equal to the number of heads. Therefore, the two tensors can be projected into three low-dimensional matrices (subqueries, subkeys, and subvalues) with the corresponding low-dimensional parameter matrices Wqi∈ℝdX×dki,Wki∈ℝdM×dki,andWvi∈ℝdM×dvi for the i−th head. Under most circumstances, dki is equal to dvi, and both are set to d/h, with d being the model dimension [5].In the end, all the suboutputsOi∈ℝn×dvi of subhead hi are concatenated as the final result O∈ℝn×dv. The final result can be further mapped into a lower or higher dimension with a linear projection matrix Wo∈ℝdv×do.(4)Qi=X•Wqi,Ki=M•Wki,Vi=M•Wvi,AttentioniX,M=softmaxQiKiTdki,Oi=AttentioniX,M•Vi,O=ConcatOi|i=1h,O^=O•Wo.In the standard transformer, the multihead attention mechanism is utilized in three sublayers: encoder SAN, decoder SAN, and encoder-decoder attention. During model implementation, all three sublayers adopt multihead dot product attention.


### 2.3. Low-Rank Bottleneck of Multihead Attention and Current Solutions

#### 2.3.1. Low-Rank Bottleneck

More heads theoretically enhance the expressive power, and fewer heads mean weaker expressive ability. Nevertheless, Bhojanapalli et al. [10] found when the number of the heads is greater than d/n (d and n are the model dimension and the sequence length, respectively), a low-rank bottleneck appears, making the model unable to represent an arbitrary context vector. To remove the bottleneck, the dimension d of the model can be increased while increasing the head number.   This approach is obviously expensive because more memory resources are required for the intense computations for model training.


#### 2.3.2. Increasing Key Size and Head Size

TheQ,K,andV are always set in the same dimensions (d). After determining the model dimension d and the number of heads h, a subhead projects Q,K,andV into some subspaces of Qi∈ℝn×dki,Ki∈ℝn×dki,andVi∈ℝn×dvi, using a series of projection matrices Wi∈ℝd×d/h, where n represents the length of the sequence, and dki=dvi=d/h is the subdimension. Then, the i−th head attention computes with Atteni=softmaxQiKiT/dki to produce a self-attention square matrix Atteni∈ℝn×n. Finally, the suboutputs Oi∈ℝn×dvi of the dot product between Atteni and Vi are concatenated.Nonetheless, projecting into a low-dimension subspace is equivalent to mapping an2-dimension attention score matrix with 2n⋅d/h variables. With the increase of h, 2n•d/h≪n2 results in a low-rank bottleneck. It is not ideal to reduce h or increase d. Either of them reduces the expressive power or adds to the computing load. Bhojanapalli et al. [10] presented a solution that breaks the constraint of dki=dvi=d/h: 2ndki⟶n2 is realized by increasing the key size dki. This approach, without changing the shape of the attention head or the computing process, satisfies the following relationships:(5)dki=n,dki≠dvi,dvi=dh.


#### 2.3.3. Talking-Head Attention

According to Vaswani et al., adequately increasing the size of heads could improve the expressive power. But this is not supported by any empirical evidence [5]. Specially, the translation is rather poor, when the token embedding is reduced to just one scalar. Under this circumstance, the dot product of the queries (one scalar) and keys (one scalar) cannot represent their subspace features. Shazeer et al. put forward a variant of multihead attention called talking-head attention, which adds two linear transformation matrices before and after the softmax function to compute the attention weights of the i−th head [13]. The addition enables each attention head to talk with each other.(6)Qi=X⋅Wqi,Ki=M⋅Wki,Vi=M⋅Wvi,Ji=Qi⋅KiTdki,i∈1,h.In talking-head attention, the attention score of thei−th head Ji∈ℝn×m is calculated the same as multi-head attention. Before normalization with the softmax function, the first talking between all heads is established with the projection matrix Wt1∈ℝh×h.(7)J=Ji|i=1h,J^=Wt1⋅J.Then, normalization is performed to get the attention weight, using the softmax function. After that, the second talking is established with another projection matrixWt2∈ℝh×h.(8)AiX,M=softmaxJ^i,A=Ai|i=1h,J^−1=Wt2⋅A.At last, the final output representations forX are computed by the same method as multihead attention.(9)Oi=J^−1i⋅Vi,O=Oi|i=1h,O^=O⋅Wo.


#### 2.3.4. Defects of the Two Solutions

The first solution aims to make2n⋅dk⟶n2 or dk⟶n. The designers of the solution set the head size of a head attention unit to the input sequence length and defined it as independent of the number of heads. For NMT, however, the sequence length varies greatly. The second solution employs linear transformation to change the distribution of different subattention matrices, which significantly increases the number of trainable parameters. In addition, the increase of h reduces the value of dk and weakens the features generated by the subspace. As a result, the second solution cannot improve the final translation performance. Overall, the low-rank bottleneck cannot be effectively solved, unless more complex high-dimensional spatial transformations are called for help.


## 2.1. Transformer

The transformer architecture resolves NMT solely by relying on the attention algorithm [5]. It has been proved that the transformer-based models are superior to the models using RNNs and CNN [1–4, 8, 9]. Like RNNs and CNN, the standard transformer-based model employs the encoder-to-decoder structure for NMT [14]. This structure maps the source sequence to a hidden state matrix as a natural language understanding (NLU) task and views the matrix elements as the context vectors or conditions for producing the target sequence. Encoder and decoder blocks are stacked in the encoder-to-decoder structure.Each encoder block usually comprises a multihead self-attention layer and a feedforward layer with residual connection [15], followed by a normalization layer [16]. As the core component of the encoder, the multihead self-attention layer captures the hidden representations of all the tokens within the source sequence. This operation mainly depends on the SAN, which learns the mutual attention score of any two tokens in the source sequence. It should be noted that the learned attention scores constitute an asymmetric square matrix, because of the learned parameters. For example, aij, the attention score from the i-th token to the j-th token, is not equal to aji, the attention score from the j-th token to the i-th token. Specifically, the SAN computes the attention scores by the scaled dot product attention algorithm. Since each token is visible to the others, the encoder can capture the feature of each token in two directions. There are two primary functions of the encoder: (1) learning the hidden representations of the input sequence as a condition for natural language generation (NLG) tasks, for example, NMT; (2) completing downstream NLP tasks, such as sentiment classification or labeling by transfer learning, after being trained independently as a masked language model (MLM) [17] and connected to specific networks.The decoder blocks have a similar structure as encoder blocks. The only difference lies in an additional sublayer, which computes the attention scores between the representations of the source sequence given by the encoder and the current target token representation given by the multihead SAN of the decoder. This sublayer, known as the encoder-decoder attention layer, is followed by a multihead attention layer. In the decoder, two attention mechanisms, namely, multihead self-attention and encoder-decoder attention, are arranged to capture the hidden state of the target token in each block. Since the token is only visible to its leftward tokens, the self-attention scores form a lower-dimensional triangular matrix. In other words, the multihead self-attention layer aims to focus the current target token only on the leftward tokens and mask the future tokens in the target sequence. In addition, the decoder learns the leftward token representations to generate the token probability distribution in each time step. During training, the probability distribution of the target token is computed based on the ground-truth leftward target tokens or their representations. All the representations are given by the encoder as a context vector for generating the target sequence. During inference, the current token probability distribution is computed based on the previous target token distribution. All the token representations are given by the encoder. The decoder works in a teacher-forcing way during training, while in an auto-regressive way during inference. The difference between the two stages is that the last token feature comes from the last ground-truth token and the last generated token given by the trained model, respectively.Because the attention mechanism is not order-aware, the transformer-based models add the positional information into the tokens, for example, absolute positional embedding.


## 2.2. Attention

For NMT, the translation performance hinges on the attention mechanism, in addition to the encoder-to-decoder structure. Bahdanau et al. [1] pioneered the use of the attention mechanism for NMT along with RNN. Sutskever et al. [8] and Luong et al. [9] further advanced the implementation of the attention mechanism in NMT. After the introduction of the attention mechanism, a target token no longer depends only on the same context vector. The different roles of the source token in target token generation are reflected. Along with the appearance of the transformer, complicated attention algorithms have been developed for specific NLP tasks, such as single head attention and multihead attention. Apart from linking up the encoder with the decoder, these algorithms learn the relationships in an end-to-end way.


### 2.2.1. Dot Product Attention

Luong et al. explored the computing methods of an attention score, examined their effectiveness, divided attention mechanisms into global attention and local attention [9] (the former targets all the source tokens, while the latter considers the subset of all the source tokens), and designed three computing methods for the weight scores between two tensors or vectors along with RNNs for NMT. Here, some symbols used in Shazeer et al. [13] are adopted. Three computing methods can be expressed as(1)scorem,x=mTxdot,mTWxgeneral,tanhWm,xconcat,where m∈ℝd and x∈ℝd are the matching and matched column vectors, respectively; W∈ℝd×d is a learned parameter matrix; score⋅ is real. The larger the score, the more important x is to the generation of m. Dot-product attention is widely used for model implementation, by virtue of its fast speed and space efficiency [5]. In line with the notations given by Shazeer et al. [13], the attention between two sequences X∈ℝn×d and M∈ℝm×d is computed through a dot product operation.(2)X=XWX,M=MWM,AttentionX,M=softmaxX⋅MT,O=AttentionX,M⋅M,O^=O⋅Wo,where n and m are the length of X∈ℝn×d and M∈ℝm×d with the same dimension d, respectively. To keep the shape constant between the input and the output, O∈ℝn×d is regarded as the final output or mapped further to a lower or higher dimension with a linear projection matrix Wo∈ℝd×do to get the final output.


### 2.2.2. Scaled Dot Product Attention

Scaled dot product attention is referred to as single head attention in this research. This attention mechanism projects the inputX into dk-dimensional queries Q and projects the other inputs M into dk-dimensional keys K and dv-dimensional values V. The increase of dk pushes up the dot products, which in turn make the softmax function converge into regions where it has extremely small gradients [5]. Therefore, the attention score is scaled with 1/dk.Firstly, it is necessary to explain the calculation of attention scores by a single head attention between two tensorsX∈ℝn×dX and M∈ℝm×dM, where the next projection operation is needed to deal with the dimensional difference. The matrices of queries Q∈ℝn×dk, keys K∈ℝm×dk, and values V∈ℝm×dv can be, respectively, obtained with the linear projection matrices Wq∈ℝdX×dk,Wk∈ℝdM×dk, and Wv∈ℝdM×dv on X, M, and M. The global computing can be defined as(3)Q=X•Wq,K=M•Wk,V=M•Wv,AttentionX,M=softmaxQKTdk,O=AttentionX,M•V,O^=O•Wo,where O∈ℝn×dv is the output. The O^ value is obtained following the last linear projection. If the self-attention scores are computed within a sequence, the linear projection matrices Wq,Wk,Wv,andWo must function on the same tensor; namely, X≡M. If X is different from M, the encoder-to-decoder attention scores should be calculated by the formula (3). Scaled dot production self-attention is applied in the SAN of the encoder and the decoder, as well as the encoder-decoder attention layer. In fact, Vaswani et al. [5] used a transformer to capture the token dependencies, relying on multihead scaled dot production attention.


### 2.2.3. Multihead Attention

In the standard transformer, it is beneficial to split the representations into multiple heads and concatenate the subresults of heads in the end. This is because more heads elevate the expressive power and improve model performance. Both tensors are employed onX∈ℝn×dX and M∈ℝm×dM, where X represents the matching tensor and M represents the matched objective. The dimensions of queries, keys, and values are then split into h parts, which is equal to the number of heads. Therefore, the two tensors can be projected into three low-dimensional matrices (subqueries, subkeys, and subvalues) with the corresponding low-dimensional parameter matrices Wqi∈ℝdX×dki,Wki∈ℝdM×dki,andWvi∈ℝdM×dvi for the i−th head. Under most circumstances, dki is equal to dvi, and both are set to d/h, with d being the model dimension [5].In the end, all the suboutputsOi∈ℝn×dvi of subhead hi are concatenated as the final result O∈ℝn×dv. The final result can be further mapped into a lower or higher dimension with a linear projection matrix Wo∈ℝdv×do.(4)Qi=X•Wqi,Ki=M•Wki,Vi=M•Wvi,AttentioniX,M=softmaxQiKiTdki,Oi=AttentioniX,M•Vi,O=ConcatOi|i=1h,O^=O•Wo.In the standard transformer, the multihead attention mechanism is utilized in three sublayers: encoder SAN, decoder SAN, and encoder-decoder attention. During model implementation, all three sublayers adopt multihead dot product attention.


## 2.2.1. Dot Product Attention

Luong et al. explored the computing methods of an attention score, examined their effectiveness, divided attention mechanisms into global attention and local attention [9] (the former targets all the source tokens, while the latter considers the subset of all the source tokens), and designed three computing methods for the weight scores between two tensors or vectors along with RNNs for NMT. Here, some symbols used in Shazeer et al. [13] are adopted. Three computing methods can be expressed as(1)scorem,x=mTxdot,mTWxgeneral,tanhWm,xconcat,where m∈ℝd and x∈ℝd are the matching and matched column vectors, respectively; W∈ℝd×d is a learned parameter matrix; score⋅ is real. The larger the score, the more important x is to the generation of m. Dot-product attention is widely used for model implementation, by virtue of its fast speed and space efficiency [5]. In line with the notations given by Shazeer et al. [13], the attention between two sequences X∈ℝn×d and M∈ℝm×d is computed through a dot product operation.(2)X=XWX,M=MWM,AttentionX,M=softmaxX⋅MT,O=AttentionX,M⋅M,O^=O⋅Wo,where n and m are the length of X∈ℝn×d and M∈ℝm×d with the same dimension d, respectively. To keep the shape constant between the input and the output, O∈ℝn×d is regarded as the final output or mapped further to a lower or higher dimension with a linear projection matrix Wo∈ℝd×do to get the final output.


## 2.2.2. Scaled Dot Product Attention

Scaled dot product attention is referred to as single head attention in this research. This attention mechanism projects the inputX into dk-dimensional queries Q and projects the other inputs M into dk-dimensional keys K and dv-dimensional values V. The increase of dk pushes up the dot products, which in turn make the softmax function converge into regions where it has extremely small gradients [5]. Therefore, the attention score is scaled with 1/dk.Firstly, it is necessary to explain the calculation of attention scores by a single head attention between two tensorsX∈ℝn×dX and M∈ℝm×dM, where the next projection operation is needed to deal with the dimensional difference. The matrices of queries Q∈ℝn×dk, keys K∈ℝm×dk, and values V∈ℝm×dv can be, respectively, obtained with the linear projection matrices Wq∈ℝdX×dk,Wk∈ℝdM×dk, and Wv∈ℝdM×dv on X, M, and M. The global computing can be defined as(3)Q=X•Wq,K=M•Wk,V=M•Wv,AttentionX,M=softmaxQKTdk,O=AttentionX,M•V,O^=O•Wo,where O∈ℝn×dv is the output. The O^ value is obtained following the last linear projection. If the self-attention scores are computed within a sequence, the linear projection matrices Wq,Wk,Wv,andWo must function on the same tensor; namely, X≡M. If X is different from M, the encoder-to-decoder attention scores should be calculated by the formula (3). Scaled dot production self-attention is applied in the SAN of the encoder and the decoder, as well as the encoder-decoder attention layer. In fact, Vaswani et al. [5] used a transformer to capture the token dependencies, relying on multihead scaled dot production attention.


## 2.2.3. Multihead Attention

In the standard transformer, it is beneficial to split the representations into multiple heads and concatenate the subresults of heads in the end. This is because more heads elevate the expressive power and improve model performance. Both tensors are employed onX∈ℝn×dX and M∈ℝm×dM, where X represents the matching tensor and M represents the matched objective. The dimensions of queries, keys, and values are then split into h parts, which is equal to the number of heads. Therefore, the two tensors can be projected into three low-dimensional matrices (subqueries, subkeys, and subvalues) with the corresponding low-dimensional parameter matrices Wqi∈ℝdX×dki,Wki∈ℝdM×dki,andWvi∈ℝdM×dvi for the i−th head. Under most circumstances, dki is equal to dvi, and both are set to d/h, with d being the model dimension [5].In the end, all the suboutputsOi∈ℝn×dvi of subhead hi are concatenated as the final result O∈ℝn×dv. The final result can be further mapped into a lower or higher dimension with a linear projection matrix Wo∈ℝdv×do.(4)Qi=X•Wqi,Ki=M•Wki,Vi=M•Wvi,AttentioniX,M=softmaxQiKiTdki,Oi=AttentioniX,M•Vi,O=ConcatOi|i=1h,O^=O•Wo.In the standard transformer, the multihead attention mechanism is utilized in three sublayers: encoder SAN, decoder SAN, and encoder-decoder attention. During model implementation, all three sublayers adopt multihead dot product attention.


## 2.3. Low-Rank Bottleneck of Multihead Attention and Current Solutions

### 2.3.1. Low-Rank Bottleneck

More heads theoretically enhance the expressive power, and fewer heads mean weaker expressive ability. Nevertheless, Bhojanapalli et al. [10] found when the number of the heads is greater than d/n (d and n are the model dimension and the sequence length, respectively), a low-rank bottleneck appears, making the model unable to represent an arbitrary context vector. To remove the bottleneck, the dimension d of the model can be increased while increasing the head number.   This approach is obviously expensive because more memory resources are required for the intense computations for model training.


### 2.3.2. Increasing Key Size and Head Size

TheQ,K,andV are always set in the same dimensions (d). After determining the model dimension d and the number of heads h, a subhead projects Q,K,andV into some subspaces of Qi∈ℝn×dki,Ki∈ℝn×dki,andVi∈ℝn×dvi, using a series of projection matrices Wi∈ℝd×d/h, where n represents the length of the sequence, and dki=dvi=d/h is the subdimension. Then, the i−th head attention computes with Atteni=softmaxQiKiT/dki to produce a self-attention square matrix Atteni∈ℝn×n. Finally, the suboutputs Oi∈ℝn×dvi of the dot product between Atteni and Vi are concatenated.Nonetheless, projecting into a low-dimension subspace is equivalent to mapping an2-dimension attention score matrix with 2n⋅d/h variables. With the increase of h, 2n•d/h≪n2 results in a low-rank bottleneck. It is not ideal to reduce h or increase d. Either of them reduces the expressive power or adds to the computing load. Bhojanapalli et al. [10] presented a solution that breaks the constraint of dki=dvi=d/h: 2ndki⟶n2 is realized by increasing the key size dki. This approach, without changing the shape of the attention head or the computing process, satisfies the following relationships:(5)dki=n,dki≠dvi,dvi=dh.


### 2.3.3. Talking-Head Attention

According to Vaswani et al., adequately increasing the size of heads could improve the expressive power. But this is not supported by any empirical evidence [5]. Specially, the translation is rather poor, when the token embedding is reduced to just one scalar. Under this circumstance, the dot product of the queries (one scalar) and keys (one scalar) cannot represent their subspace features. Shazeer et al. put forward a variant of multihead attention called talking-head attention, which adds two linear transformation matrices before and after the softmax function to compute the attention weights of the i−th head [13]. The addition enables each attention head to talk with each other.(6)Qi=X⋅Wqi,Ki=M⋅Wki,Vi=M⋅Wvi,Ji=Qi⋅KiTdki,i∈1,h.In talking-head attention, the attention score of thei−th head Ji∈ℝn×m is calculated the same as multi-head attention. Before normalization with the softmax function, the first talking between all heads is established with the projection matrix Wt1∈ℝh×h.(7)J=Ji|i=1h,J^=Wt1⋅J.Then, normalization is performed to get the attention weight, using the softmax function. After that, the second talking is established with another projection matrixWt2∈ℝh×h.(8)AiX,M=softmaxJ^i,A=Ai|i=1h,J^−1=Wt2⋅A.At last, the final output representations forX are computed by the same method as multihead attention.(9)Oi=J^−1i⋅Vi,O=Oi|i=1h,O^=O⋅Wo.


### 2.3.4. Defects of the Two Solutions

The first solution aims to make2n⋅dk⟶n2 or dk⟶n. The designers of the solution set the head size of a head attention unit to the input sequence length and defined it as independent of the number of heads. For NMT, however, the sequence length varies greatly. The second solution employs linear transformation to change the distribution of different subattention matrices, which significantly increases the number of trainable parameters. In addition, the increase of h reduces the value of dk and weakens the features generated by the subspace. As a result, the second solution cannot improve the final translation performance. Overall, the low-rank bottleneck cannot be effectively solved, unless more complex high-dimensional spatial transformations are called for help.


## 2.3.1. Low-Rank Bottleneck

More heads theoretically enhance the expressive power, and fewer heads mean weaker expressive ability. Nevertheless, Bhojanapalli et al. [10] found when the number of the heads is greater than d/n (d and n are the model dimension and the sequence length, respectively), a low-rank bottleneck appears, making the model unable to represent an arbitrary context vector. To remove the bottleneck, the dimension d of the model can be increased while increasing the head number.   This approach is obviously expensive because more memory resources are required for the intense computations for model training.


## 2.3.2. Increasing Key Size and Head Size

TheQ,K,andV are always set in the same dimensions (d). After determining the model dimension d and the number of heads h, a subhead projects Q,K,andV into some subspaces of Qi∈ℝn×dki,Ki∈ℝn×dki,andVi∈ℝn×dvi, using a series of projection matrices Wi∈ℝd×d/h, where n represents the length of the sequence, and dki=dvi=d/h is the subdimension. Then, the i−th head attention computes with Atteni=softmaxQiKiT/dki to produce a self-attention square matrix Atteni∈ℝn×n. Finally, the suboutputs Oi∈ℝn×dvi of the dot product between Atteni and Vi are concatenated.Nonetheless, projecting into a low-dimension subspace is equivalent to mapping an2-dimension attention score matrix with 2n⋅d/h variables. With the increase of h, 2n•d/h≪n2 results in a low-rank bottleneck. It is not ideal to reduce h or increase d. Either of them reduces the expressive power or adds to the computing load. Bhojanapalli et al. [10] presented a solution that breaks the constraint of dki=dvi=d/h: 2ndki⟶n2 is realized by increasing the key size dki. This approach, without changing the shape of the attention head or the computing process, satisfies the following relationships:(5)dki=n,dki≠dvi,dvi=dh.


## 2.3.3. Talking-Head Attention

According to Vaswani et al., adequately increasing the size of heads could improve the expressive power. But this is not supported by any empirical evidence [5]. Specially, the translation is rather poor, when the token embedding is reduced to just one scalar. Under this circumstance, the dot product of the queries (one scalar) and keys (one scalar) cannot represent their subspace features. Shazeer et al. put forward a variant of multihead attention called talking-head attention, which adds two linear transformation matrices before and after the softmax function to compute the attention weights of the i−th head [13]. The addition enables each attention head to talk with each other.(6)Qi=X⋅Wqi,Ki=M⋅Wki,Vi=M⋅Wvi,Ji=Qi⋅KiTdki,i∈1,h.In talking-head attention, the attention score of thei−th head Ji∈ℝn×m is calculated the same as multi-head attention. Before normalization with the softmax function, the first talking between all heads is established with the projection matrix Wt1∈ℝh×h.(7)J=Ji|i=1h,J^=Wt1⋅J.Then, normalization is performed to get the attention weight, using the softmax function. After that, the second talking is established with another projection matrixWt2∈ℝh×h.(8)AiX,M=softmaxJ^i,A=Ai|i=1h,J^−1=Wt2⋅A.At last, the final output representations forX are computed by the same method as multihead attention.(9)Oi=J^−1i⋅Vi,O=Oi|i=1h,O^=O⋅Wo.


## 2.3.4. Defects of the Two Solutions

The first solution aims to make2n⋅dk⟶n2 or dk⟶n. The designers of the solution set the head size of a head attention unit to the input sequence length and defined it as independent of the number of heads. For NMT, however, the sequence length varies greatly. The second solution employs linear transformation to change the distribution of different subattention matrices, which significantly increases the number of trainable parameters. In addition, the increase of h reduces the value of dk and weakens the features generated by the subspace. As a result, the second solution cannot improve the final translation performance. Overall, the low-rank bottleneck cannot be effectively solved, unless more complex high-dimensional spatial transformations are called for help.


## 3. Interacting-Head Attention

### 3.1. Theoretical Hypothesis

In the original multihead attention, a subhead computes the dot product among the subembeddings (head size) of the tokens in the same subspace. The head size in different subspaces is expected to have a strong correlation. The correlation should be strong when the head size is large or the number of heads is small and weak when the head size is small or the number of heads is large. The subembedding of different subspaces can be ignored because the subembedding of the same subspace is very small. Obviously, when the head size limitation reaches 1 and the number of heads equals model dimension, the dot product of subembedding in the same subspace is equal to the product of two scalars. This certainly cannot express the feature information of the same subspace. To calculate the correlation of the head size in different subspaces, this paper proposes a novel attention mechanism called interacting-head attention. It is assumed that the head size is no greater than the sequence length, aiming to prevent the low-rank bottleneck. The effectiveness of our model was verified experimentally based on this hypothesis.To clarify the composition, the associations between two adjacent tokens with different head sizes in different subspaces are displayed in Figure1, where the red line indicates the association of the head size in the same subspace, and the blue, black, green, and brown lines specify the association of the head size in subspaces 1, 2, …, (h − 1) and h, respectively. In fact, there is an association between any two head sizes of the tokens in different subspaces.Figure 1
Associations between the head size of different subspaces.


### 3.2. Graphical Representation

As shown in Figure2, the traditional multihead attention adopts the method of dividing before combining. Each subhead represents the matching between subembeddings in the same subspace. However, not all subheads are associated with each other. If the number of heads grows, the omission of the dependency among some heads will result in low performance. What is worse, only the partial attention among the corresponding queries and keys is considered, although the traditional mechanism covers the main matching information. In contrast, our mechanism considers the dependencies of all the attention among the queries and keys. In addition, it is assumed that the different dimensions of the head size of a token indicate morphology, syntax, and semantic information, respectively. The morphology must also have a close association with the morphology (more important attention score) of other tokens. Needless to say, it is also related to the syntax and semantic information of other tokens.Figure 2
Comparison between multihead attention (a) and our mechanism (b) with 4 heads.
(a)(b)Our mechanism has the following advantages:(1)
Compared with talking-head attention, our mechanism does not need to learn extra parameters, and only adds some inner product computations.(2)
Our mechanism learns subordinate information by interacting-head attention, in addition to the attention computation of all the tokens with talking-head attention in the same subspace. In this way, all parts can fully communicate with each other.


### 3.3. Sufficient Interactions between Heads

To ensure that any attention head attends to all subqueries and subkeys, this paper further examines the relationship between any subquery from the matching tensorX∈ℝn×dX and all the subkeys from the matched tensor M∈ℝm×dM, where X and M are the feature matrices of the source and target sequences, n and m are their lengths, and dX and dM are their dimensions, respectively. It is assumed that the number of heads is set to h. Like the original multihead attention, for the ith subspace, both tensors are mapped to other tensors Qi, Ki, Vi with the lower dimension using three linear transformation matrices Wqi, Wki, and Wvi. Actually, the dimensions of Wqi∈ℝdX×dki and Wki∈ℝdM×dki must be equal for resolving the dot product. Otherwise, Wvi∈ℝdM×dvi is the linear transformation matrix for M with dki=dvi=d/h. This process can be expressed as(10)Qi=X⋅Wqi,Ki=M⋅Wki,Vi=M⋅Wvi,i∈1,hQi∈ℝn×dki,Ki∈ℝm×dki,Vi∈ℝm×dvi.ForQi, the attention scores between it and all the subkeys are computed and then normalized by the softmax function.(11)AttentionijX,M=softmaxQiKjTdki|j=1h,Oij=AttentionijX,M⋅Vj|j=1h,where Oij∈ℝn×dvi is the attention output between the special subquery Qi and the dynamic subkey Kj. Assuredly, the calculation of the interacting-head attention on one sequence only needs to replace M with X. Next, the final output O can be obtained through similar concatenations of sub-sub-output and sub-output, respectively:(12)Oi=concatOij|j=1h,axis=−1,O=concatOi⋅Wo.A minimal python implementation is shown in Algorithm 1. In practice, the deep learning framework keras is used for all our experiments.Algorithm 1: Interacting-head Attention.
Input: heads, d_model, mask, q, k, vOutput: outputs, attns(1)
import tensorﬂow as tf, keras.layers.Dense, keras.callbacks.K(2)
dk = dv = d_model//heads(3)
qs_layer = Dense(heads∗dk), ks_layer = Dense(heads∗dk), vs_layer = Dense(heads∗dv)(4)
qs = qs_layer(q), ks = ks_layer(k), vs = vs_layer(v)(5)
qs ⟵ reshape(qs), ks  ←  reshape(ks), vs ← reshape(vs)(6)
temper = tf.sqrt(tf.shape(ks)[−1])(7)
for i = 1 to heads do(8)
for j = 1 to heads do(9)
a_ij = K.batch_dot(qs[:,i,:,:], ks[:,j,:,:],axes = [2])(10)
if mask is not None then:(11)
mmask = (−1e + 9)∗(1 − mask)(12)
a_ij  ⟵  K.Add([a_ij,mmask])(13)
end if(14)
a_ij  ←  K.expand dims(a_ij, axis = 1)(15)
attn.append(a_ij)(16)
end for(17)
end for(18)
j = 0(19)
while True do(20)
if j! = 0 and j%heads = = 0 then(21)
break(22)
end if(23)
for i = 1 to heads do(24)
a = attn[i∗heads + j](25)
temp_a.append(a)(26)
end for(27)
sm = K.Activation(“softmax,”K.Add(temp_a))(28)
output = K.batch_dot(sm[:,0,:,:],vs[:,j:,:],axes = [1,2])(29)
output  ←  K.expand_dims(output, axis = 0)(30)
sm  ←  K.permute_dimensions(sm, (1, 0, 2, 3))(31)
outpus.append(output)(32)
attns.append(sm)(33)
j  ←  j + 1(34)
end while(35)
outputs = K.concatente(outputs, axis = 0)(36)
attns = K.concatente(attns, axis = 0)(37)
outputs  ←  reshape(outputs), attns  ←  reshape(attns)


### 3.4. Choosing the Suitable Number of Heads

In Section2.2.3, the dimensions of the Qi, Ki, andVi matrices of the ith head are subject to dqi=dki=dvi=d/h, which can be written as h=d/dki. According to the definition of head size in [10], it can be expressed as h=d/dki=d/head_size. As mentioned before, increasing model dimensionality and the number of heads can enhance the expressive ability. But a heavy computing load and a large memory demand will ensue, which lead to a low-rank bottleneck. Our model initially adopts a fixed dimensionality d. Inspired by [10], to prevent the low-rank bottleneck, the sequence length is regarded as the minimum head size. Therefore, the mean sequence length of the training set should be computed to obtain the maximum number of heads. In our model, the maximum possible number of heads is computed by(13)hmax=dhead_size=dn,where d is the model dimensionality and n is the mean sequence length of the training set.


## 3.1. Theoretical Hypothesis

In the original multihead attention, a subhead computes the dot product among the subembeddings (head size) of the tokens in the same subspace. The head size in different subspaces is expected to have a strong correlation. The correlation should be strong when the head size is large or the number of heads is small and weak when the head size is small or the number of heads is large. The subembedding of different subspaces can be ignored because the subembedding of the same subspace is very small. Obviously, when the head size limitation reaches 1 and the number of heads equals model dimension, the dot product of subembedding in the same subspace is equal to the product of two scalars. This certainly cannot express the feature information of the same subspace. To calculate the correlation of the head size in different subspaces, this paper proposes a novel attention mechanism called interacting-head attention. It is assumed that the head size is no greater than the sequence length, aiming to prevent the low-rank bottleneck. The effectiveness of our model was verified experimentally based on this hypothesis.To clarify the composition, the associations between two adjacent tokens with different head sizes in different subspaces are displayed in Figure1, where the red line indicates the association of the head size in the same subspace, and the blue, black, green, and brown lines specify the association of the head size in subspaces 1, 2, …, (h − 1) and h, respectively. In fact, there is an association between any two head sizes of the tokens in different subspaces.Figure 1
Associations between the head size of different subspaces.


## 3.2. Graphical Representation

As shown in Figure2, the traditional multihead attention adopts the method of dividing before combining. Each subhead represents the matching between subembeddings in the same subspace. However, not all subheads are associated with each other. If the number of heads grows, the omission of the dependency among some heads will result in low performance. What is worse, only the partial attention among the corresponding queries and keys is considered, although the traditional mechanism covers the main matching information. In contrast, our mechanism considers the dependencies of all the attention among the queries and keys. In addition, it is assumed that the different dimensions of the head size of a token indicate morphology, syntax, and semantic information, respectively. The morphology must also have a close association with the morphology (more important attention score) of other tokens. Needless to say, it is also related to the syntax and semantic information of other tokens.Figure 2
Comparison between multihead attention (a) and our mechanism (b) with 4 heads.
(a)(b)Our mechanism has the following advantages:(1)
Compared with talking-head attention, our mechanism does not need to learn extra parameters, and only adds some inner product computations.(2)
Our mechanism learns subordinate information by interacting-head attention, in addition to the attention computation of all the tokens with talking-head attention in the same subspace. In this way, all parts can fully communicate with each other.


## 3.3. Sufficient Interactions between Heads

To ensure that any attention head attends to all subqueries and subkeys, this paper further examines the relationship between any subquery from the matching tensorX∈ℝn×dX and all the subkeys from the matched tensor M∈ℝm×dM, where X and M are the feature matrices of the source and target sequences, n and m are their lengths, and dX and dM are their dimensions, respectively. It is assumed that the number of heads is set to h. Like the original multihead attention, for the ith subspace, both tensors are mapped to other tensors Qi, Ki, Vi with the lower dimension using three linear transformation matrices Wqi, Wki, and Wvi. Actually, the dimensions of Wqi∈ℝdX×dki and Wki∈ℝdM×dki must be equal for resolving the dot product. Otherwise, Wvi∈ℝdM×dvi is the linear transformation matrix for M with dki=dvi=d/h. This process can be expressed as(10)Qi=X⋅Wqi,Ki=M⋅Wki,Vi=M⋅Wvi,i∈1,hQi∈ℝn×dki,Ki∈ℝm×dki,Vi∈ℝm×dvi.ForQi, the attention scores between it and all the subkeys are computed and then normalized by the softmax function.(11)AttentionijX,M=softmaxQiKjTdki|j=1h,Oij=AttentionijX,M⋅Vj|j=1h,where Oij∈ℝn×dvi is the attention output between the special subquery Qi and the dynamic subkey Kj. Assuredly, the calculation of the interacting-head attention on one sequence only needs to replace M with X. Next, the final output O can be obtained through similar concatenations of sub-sub-output and sub-output, respectively:(12)Oi=concatOij|j=1h,axis=−1,O=concatOi⋅Wo.A minimal python implementation is shown in Algorithm 1. In practice, the deep learning framework keras is used for all our experiments.Algorithm 1: Interacting-head Attention.
Input: heads, d_model, mask, q, k, vOutput: outputs, attns(1)
import tensorﬂow as tf, keras.layers.Dense, keras.callbacks.K(2)
dk = dv = d_model//heads(3)
qs_layer = Dense(heads∗dk), ks_layer = Dense(heads∗dk), vs_layer = Dense(heads∗dv)(4)
qs = qs_layer(q), ks = ks_layer(k), vs = vs_layer(v)(5)
qs ⟵ reshape(qs), ks  ←  reshape(ks), vs ← reshape(vs)(6)
temper = tf.sqrt(tf.shape(ks)[−1])(7)
for i = 1 to heads do(8)
for j = 1 to heads do(9)
a_ij = K.batch_dot(qs[:,i,:,:], ks[:,j,:,:],axes = [2])(10)
if mask is not None then:(11)
mmask = (−1e + 9)∗(1 − mask)(12)
a_ij  ⟵  K.Add([a_ij,mmask])(13)
end if(14)
a_ij  ←  K.expand dims(a_ij, axis = 1)(15)
attn.append(a_ij)(16)
end for(17)
end for(18)
j = 0(19)
while True do(20)
if j! = 0 and j%heads = = 0 then(21)
break(22)
end if(23)
for i = 1 to heads do(24)
a = attn[i∗heads + j](25)
temp_a.append(a)(26)
end for(27)
sm = K.Activation(“softmax,”K.Add(temp_a))(28)
output = K.batch_dot(sm[:,0,:,:],vs[:,j:,:],axes = [1,2])(29)
output  ←  K.expand_dims(output, axis = 0)(30)
sm  ←  K.permute_dimensions(sm, (1, 0, 2, 3))(31)
outpus.append(output)(32)
attns.append(sm)(33)
j  ←  j + 1(34)
end while(35)
outputs = K.concatente(outputs, axis = 0)(36)
attns = K.concatente(attns, axis = 0)(37)
outputs  ←  reshape(outputs), attns  ←  reshape(attns)


## 3.4. Choosing the Suitable Number of Heads

In Section2.2.3, the dimensions of the Qi, Ki, andVi matrices of the ith head are subject to dqi=dki=dvi=d/h, which can be written as h=d/dki. According to the definition of head size in [10], it can be expressed as h=d/dki=d/head_size. As mentioned before, increasing model dimensionality and the number of heads can enhance the expressive ability. But a heavy computing load and a large memory demand will ensue, which lead to a low-rank bottleneck. Our model initially adopts a fixed dimensionality d. Inspired by [10], to prevent the low-rank bottleneck, the sequence length is regarded as the minimum head size. Therefore, the mean sequence length of the training set should be computed to obtain the maximum number of heads. In our model, the maximum possible number of heads is computed by(13)hmax=dhead_size=dn,where d is the model dimensionality and n is the mean sequence length of the training set.


## 4. Experiments

This section tests our model on three datasets, namely, IWSLT16 DE-EN, WMT17 EN-DE, and WMT17 EN-CS. All of them are widely used as NMT benchmarks. Before the experiments, the three datasets were preprocessed, and the hyperparameters were configured. Three classic and efficient models were selected as baselines to demonstrate the superiority of our model in translation quality. The experimental results were analyzed to verify our hypothesis and reveal the merits and defects of our model.


### 4.1. Datasets

For the IWSLT16 DE-EN corpus, the experimental data were extracted from the evaluation campaign of the International Conference on Spoken Language Translation (IWSLT 2021) [18]. The extracted data consist of 181 k/12 k sentence pairs as training/evaluation sets. The concatenation of tst2010/2011/2012/2013/2014 was taken as the test set, including around 12 k sentence pairs.For the WMT17 machine translation task, EN-DE and EN-CS MT tasks were chosen as our problems because of the limited memory resources [19, 20]. For WMT17 EN-DE and EN-CS corpora, the training set consists of 5.85 million and 1 million sentence pairs, respectively. For the two corpora, newstest2013 of 3 k sequence pairs was treated as our evaluation set and newstest2014/2015/2016/2017 as the test set.Both datasets were preprocessed through data normalization and subword segmentation, using Moses, a de-facto standard toolkit for statistical machine translation (SMT) [21]. Firstly, the sentence pairs of all datasets were tokenized, and those longer than 80/80/100 on the training sets of IWSLT16 DE-EN, WMT17 EN-CS, and WMT17 EN-DE, respectively, were discarded. After that, a truecase model was trained on the cleaned train set and applied to each subset. Secondly, all sequence pairs were encrypted by bytes pair encoding (BPE) [22], using a sentence piece tool (https://github.com/google/sentencepiece) [23]. This step mitigates the influence of unknown (UNK), padding (PAD), and rare tokens. In IWSLT16 DE-EN and WMT17 EN-DE translation tasks, the source and target languages (EN and DE) have similar alphabets. Therefore, a shared vocabulary with 40,000/80000 tokens was learned on IWSLT16 and WMT17, respectively. In the WMT17 EN-CS translation task, a vocabulary was learned for English (EN) and Czech (CS) separately, because the two languages are distant from each other.As can be seen from Table1 and Figure 3, the sequence lengths of the languages in different datasets obeyed similar distributions and remained consistent with the mean sequence lengths. The length of sequence ranged from 3 to 120, from 1 to 332, and from 1 to 316 for IWSLT16 DE-EN, WMT17 EN-DE, and WMT17 EN-CS, respectively. The mean sequence lengths of the three datasets were set as 20, 25, and 26, respectively.Table 1
Number of sequence pairs and sequence length in IWSLT16 DE-EN, WMT17 EN-DE, and WMT17 EN-CS.
DatasetTrainEvalTestVocabTotal length of the train setMean length of the train setIWSLT16 DE-EN181,14912,09811,82539,6453,766,897/3,826,03820/21WMT17 EN-DE5,852,45730003003/2169/2999/300484,441148,665,327/149,176,20525/25WMT17 EN-CS1,010,91830003003/2656/2999/300525,560/39,67027,825,644/26,152,29927/25Note. Train, Eval, and Test represent the number of sequence pairs of different data subsets, respectively; length refers to the number of tokens in a sentence; total length of the train set is the total number of tokens in the training set; mean length of the train set is the ratio of the total length to the total sequence pairs in the training set.Figure 3
Histogram and line graph of sequence length distribution of IWSLT16 DE-EN, WMT17 EN-DE, and WMT17 EN-CS.


### 4.2. Parameter Settings

The settings of our experimental parameters refer to those in [5] which first proposed the transformer architecture for NMT. Our experiments were arranged based on an appropriate setup on the optimizer, learning rate, and hyperparameters. The optimizer was designed by Adam with β1=0.9, β2=0.997, and ε=10−9 as our optimizer [24]. The learning rate was configured by the warm-up strategy [5] with warm up−steps=8000. During training, the label smoothing rate [25] was set to 0.1, and the dropout was fixed at 0.1. Moreover, because of the limitation of GPU memory, the dimension of the hidden state for linear transformation was set to 1024, and the model dimension was set to 512. To avoid the low-rank bottleneck, the maximum number of heads was obtained by formula (13). Table 2 lists the possible values of the number of heads.Table 2
Possible number of heads in our model.
DatasetModelNumber of headsIWSLT16 DE-EN51225 [2, 4, 8, 16]WMT17 EN-DE51220 [2, 4, 8, 16]WMT17 EN-CS51220 [2, 4, 8, 16]All the experiments were conducted with TensorFlow 1.4.1 and keras 2.1.3 with reference project (https://github.com/Lsdefine/attention-is-all-you-need-keras). The attained model cpt files can also be converted into PyTorch bin files with transformers [26]. All the experiments were completed by two NVIDIA Tesla V100 GPUs of 32 GB memory.During the inference, a beam search algorithm was used with beam size 4 and batch size 8 to decode all test sets. The length penalty was set to 1 and 0.6 for IWSLT16 and WMT17 test sets, respectively.


### 4.3. Evaluation Metrics

The machine translation quality was evaluated objectively by multiple metrics, including bilingual evaluation understudy (BLEU) [27], word error rate (WER) [28], metric for evaluation of translation with explicit ordering (METEOR) [29], recall-oriented understudy of gisting evaluation (ROUGE) [30], consensus-based image description evaluation (CIDEr) [31]and YiSi [32].(1)
BLEU [27]. BLEU, one of the most manifold evaluation methods for machine translation, uses N-gram token matching to evaluate the similarity between the reference and the candidate. The quality is positively correlated with the proximity between the translations and the references.(2)
WER [28]. Similar to translation edit rate (TER) [33], WER computes the word error rate between the reference and hypothetical translation. The word errors include the number of substitutions, insertions, and deletions from the translation to the reference. The rate is the ratio of word errors to the length of the reference.(3)
METEOR [29]. Based on explicit word-to-word matches, METEOR includes identical words in the surface forms, morphological variants in stemmed forms, and synonyms in meanings between the reference and the candidate.(4)
ROUGE [30]. ROUGE was introduced by Chin-Yew Lin for text summarization. It contains four different measures: ROUGE-N, ROUGE-L, ROUGE-W, and ROUGE-S. Here, ROUGE-L is selected as the metric to evaluate machine translation. Note that L is the abbreviation of the longest common subsequence (LCS) between the reference and the candidate.(5)
CIDEr [31]. CIDEr is originally used to evaluate the generated image descriptions. It measures the similarity of a generated sequence against a set of ground truth sentences written by humans. This similarity reflects how well the generated descriptions capture the information of grammaticality, saliency, importance, and accuracy.(6)
YiSi [32]. YiSi is a family of quality evaluation and estimation metrics for semantic machine translation. In this paper, YiSi-1 is selected for its high average correlation with human assessment, thanks to the use of multilingual bidirectional encoder representations from transformers (BERT).BLEU, WER, METEOR, ROUGE_L, CIDEr, YiSi were computed using multi-bleu.perl (https://github.com/moses-smt/mosesdecoder), pyter (https://pypi.org/project/pyter/), nlg-eval (METEOR, ROUGE_L, CIDEr using https://github.com/Maluuba/nlg-eval) [34], and YiSi (https://github.com/chikiulo/yisi).


### 4.4. Baselines

(1)
Original multihead attention by Vaswani et al. [5]: the original transformer-based model is implemented based on multihead attention, which brings more expressive power than single head attention. The model linearly projects the queries, keys, and values with different, learned projection matrices to dk, dk, and dv dimensions, respectively. Each head yields dv-dimensional output values. All the attention heads are concatenated into the final values.(2)
Multihead attention (head size equaling sequence length) by Bhojanapalli et al. [10]: in the original multihead attention, the scaling between the number of heads and head size leads to a low-rank bottleneck. To overcome the problem, Bhojanapalli et al. set the head size to input sequence length and keep it independent of the number of heads. In this way, each head acquires more expressive power. The effectiveness of their approach was verified through experiments on the two tasks of Stanford Question Answering Dataset (SQuAD) and Multigenre Natural Language Inference (MNLI).(3)
Talking-head attention by Shazeer et al. [13]: with the increase in the number of heads, the dimensionality of query vectors and key vectors becomes so low that the dot product between the two types of vectors no longer includes useful information. This is what is commonly called a low-rank bottleneck. To address the problem, talking-head attention inserts two linear learned projection matrices across the attention-head dimension of the attention-logits tensor, allowing each head attention to target any subquery vector and subkey vector. The feasibility of this attention mechanism was tested on several seq2seq NLP tasks. But Shazeer et al. did not test the mechanism on any NMT task. Therefore, this paper implements the mechanism on both the evaluation benchmarks and compares it with our model.


### 4.5. Results

For the IWSLT2016 DE-EN translation task, all models almost reached the peak performance at 16 heads. As shown in Table3, the performance of the original multihead attention clearly declined by 3.31 BLEU/0.41 WER/2.72 METEOR/2.83 ROUGE_L/0.30 CIDEr/3.23 YiSi on the evaluation set and 1.15 WER/2.63 METEOR/2.69 ROUGE_L/0.27 CIDEr/2.88 YiSi on the test set, as the number of heads increased from 16 to 32. The trend signifies the occurrence of the low-rank bottleneck. As for the multihead attention with fixed head size, the performance reached a relatively stable state, when the number of heads was equal to 16. As the number of heads increased, the performance improved slightly. In talking-head attention, the performance varied similarly to multihead attention with fixed head size. When it comes to our interacting-head attention, the performance improved significantly by 2.78 BLEU/0.85 WER/2.90 METEOR/2.65 ROUGE_L/0.29 CIDEr/2.97 YiSi on the evaluation set and 2.43 BLEU/1.38 WER/3.05 METEOR/2.70 ROUGE_L/0.30 CIDEr/3.59 YiSi on the test set, compared with the original multihead attention.Table 3
Overall evaluation scores using four attention-based models on the IWSLT16 DE-EN evaluation set and test set.
ModelIWSLT16 subsetNumber of heads/Head size2/2564/1288/6416/3232/1664/8(a)Multihead attentiondev22.7325.7126.4427.2323.9219.50Test21.1524.0024.3825.1822.0517.94Multihead attention (head size =n, dmodel = 512)dev20.9323.0824.7926.0026.9426.38Test19.3521.3822.8124.6124.8624.59Talking-head attentiondev20.6621.3922.4724.0224.2123.65Test19.1319.8220.8722.2422.3021.72Interacting-head attentiondev29.5929.9429.5430.01(+2.78)——Test27.6128.0127.4427.61(+2.43)——(b)Multihead attentiondev5.824.853.563.103.518.73Test9.037.927.715.877.0214.39Multihead attention (head size =n, dmodel = 512)dev6.906.005.294.594.553.97Test11.4710.108.988.168.117.96Talking-head attentiondev9.037.316.584.814.514.33Test14.4611.909.839.108.698.90Interacting-head attentiondev2.412.442.312.25(0.85)——Test6.605.344.774.49(1.38)——(c)Multihead attentiondev25.4226.7228.5829.1426.4222.63Test24.7725.9627.6728.2025.5722.02Multihead attention (head size =n, dmodel = 512)dev23.4725.6925.7626.5127.0527.12Test22.8424.9524.9525.8825.2826.36Talking-head attentiondev22.4824.2823.8024.4625.2826.76Test21.8523.4623.1823.9424.4925.85Interacting-head attentiondev31.7331.9431.6532.04(+2.90)——Test30.8731.1130.7631.25(+3.05)——(d)Multihead attentiondev53.7255.6857.8558.7655.9350.52Test52.2354.3056.2357.2654.5749.24Multihead attention (head size =n, dmodel = 512)dev51.4654.0255.8355.5256.5356.99Test50.2152.7055.4454.3154.1855.60Talking-head attentiondev49.2651.6452.3853.7254.9255.78Test48.2350.1951.1952.3053.3354.45Interacting-head attentiondev61.0261.2961.1461.41(+2.65)——Test59.2559.8059.6259.96(+2.70)——(e)Multihead attentiondev1.942.392.612.692.391.89Test1.802.202.392.492.221.75Multihead attention (head size =n, dmodel = 512)dev1.992.202.242.272.332.38Test1.842.052.082.102.162.21Talking-head attentiondev1.701.871.842.092.202.32Test1.571.721.671.912.012.05Interacting-head attentiondev2.932.962.932.98(+0.29)——Test2.692.752.712.79(+0.30)——(f)Multihead attentiondev54.3355.9857.9158.4855.2549.83Test53.3454.1855.8856.4553.5748.44Multihead attention (head size =n, dmodel = 512)dev52.0554.2555.8557.1557.3356.45Test50.6352.5754.1855.5156.7955.94Talking-head attentiondev50.1951.3252.3253.0255.5557.06Test48.9749.6350.6351.6953.7255.83Interacting-head attentiondev60.9261.2260.9161.45(+2.97)——Test58.6859.1558.6760.04(+3.59)——Note. The units of the performances are (a) BLEU, (b) WER, (c) METEOR, (d) ROUGE_L, (e) CIDEr, and (f) YiSi.For the WMT17 EN-DE translation task, the original multihead attention had the best performance at 16 heads and performed poorer and poorer with the growing number of heads. As shown in Table4, multihead attention with fixed head size and talking-head attention achieved a slight improvement by solving the low-rank bottleneck. With our interacting-head attention, the performance improved by 2.31 BLEU/5.94 WER/1.46 METEOR/1.35 ROUGE_L/0.07 CIDEr/0.33 YiSi and 1.62 BLEU/6.04 WER/1.39 METEOR/0.11 CIDEr/0.87 YiSi on the evaluation set and newstest2014, respectively.Table 4
Overall evaluation scores using four attention-based models on the WMT17 EN-DE evaluation set and test set.
ModelWMT17 subsetNumber of heads/head size2/2564/1288/6416/3232/1664/8(a)Multihead attentiondev8.3812.5612.4815.1512.2012.45newstest20148.7711.3112.8515.7311.6112.28newstest20159.3612.2713.8016.7911.9013.23newstest201611.4014.8316.7920.9715.1716.16newstest20178.8311.8313.1516.4111.6312.99Multihead attention (head size =n)dev11.6612.5113.7014.5714.6115.29newstest201411.6413.2614.9315.4214.9615.00newstest201512.0414.0715.7816.5716.6717.57newstest201614.7117.5719.7720.3319.8020.08newstest201711.7213.5414.9216.0316.0716.33Talking-head attentiondev10.9013.8915.3115.3715.2315.50newstest201411.2112.6414.3315.2815.2915.39newstest201511.8512.8114.6115.9515.5515.36newstest201614.2216.3318.8419.5319.3420.43newstest201711.1512.8114.3515.4914.4415.36Interacting-head attentiondev13.0714.8414.5317.46(+2.31)——newstest201410.9114.7215.7517.35(+1.62)——newstest201511.9915.4316.9018.40(+1.21)——newstest201614.9519.6121.1222.36(+1.39)——newstest201711.0915.0017.3017.67(+1.26)——(b)Multihead attentiondev39.2337.8028.2315.5732.9322.47newstest201446.8545.5932.1019.6839.5928.80newstest201545.2244.3132.6422.6842.7828.58newstest201640.1439.3528.5115.9132.0423.97newstest201743.8542.7432.1219.0738.3826.50Multihead attention (head size =n)dev39.3736.7325.1122.9716.6713.42newstest201446.4035.7732.5628.6518.5618.77newstest201546.9338.4533.1628.0323.1320.07newstest201642.9130.2427.6622.7717.1513.26newstest201744.6534.6029.8226.7819.7516.52Talking-head attentiondev48.1740.6339.0324.8020.1316.25newstest201448.1948.0252.9833.9327.5820.32newstest201561.0953.8052.3333.4331.1322.05newstest201647.0247.2847.4231.0823.1614.82newstest201751.1347.3450.6334.5228.0120.05Interacting-head attentiondev58.1035.2020.839.63(5.94)——newstest201468.3947.2929.0113.64(6.04)——newstest201579.3948.5529.6416.05(6.63)——newstest201667.1244.2125.9411.27(4.64)——newstest201781.5649.0730.8915.23(3.84)——(c)Multihead attentiondev19.3619.5420.3123.1620.5620.53newstest201419.3919.6420.8323.7120.7220.97newstest201519.3619.6021.1524.1120.7021.06newstest201620.4321.2722.7326.2922.4422.63newstest201719.5219.8521.0523.9620.7021.11Multihead attention (head size =n)dev19.4720.9621.0423.0123.3223.79newstest201419.6921.4522.3023.5223.9124.45newstest201519.6421.5422.6123.7024.0224.46newstest201620.9523.2624.4725.6525.4426.99newstest201719.6821.5022.2523.7324.0524.49Talking-head attentiondev17.7119.5521.3823.2223.5123.19newstest201417.7319.5921.8723.6523.7323.75newstest201517.3319.3922.0323.8124.1424.26newstest201618.6020.5223.1925.4026.0225.88newstest201717.7419.3921.9023.7123.6624.15Interacting-head attentiondev22.4322.6123.8724.62(+1.46)——newstest201422.6722.6624.3525.10(+1.39)——newstest201522.8123.0924.5225.53(+1.42)——newstest201624.6925.0026.6827.27(+0.98)——newstest201722.1222.7624.3825.66(+1.70)——(d)Multihead attentiondev40.0840.1941.5445.7742.4742.79newstest201436.6537.8140.2249.0440.4041.18newstest201537.3339.6342.5646.8441.6942.71newstest201639.8841.9844.4044.8844.5245.13newstest201736.9439.0141.4346.0941.0242.02Multihead attention (head size =n)dev41.3943.6744.8945.7945.8346.01newstest201439.7542.3543.6244.6945.0747.25newstest201540.7643.3745.5446.1846.2248.15newstest201643.1846.5148.2149.3448.9650.08newstest201740.4143.0544.8745.5245.5546.08Talking-head attentiondev38.4240.2042.0343.9944.6745.33newstest201435.6739.2342.5142.7445.8646.09newstest201537.9438.6141.0044.1544.9246.67newstest201638.8341.2142.8846.2848.0546.99newstest201737.2939.5840.9343.3045.2345.65Interacting-head attentiondev45.4746.0346.5447.12(+1.35)——newstest201443.9543.7745.1548.20——newstest201545.3345.8346.6147.31(+0.51)——newstest201647.9748.8349.9450.47(+5.59)——newstest201743.6144.5845.9145.99——(e)Multihead attentiondev1.261.301.371.641.371.38newstest20141.131.191.261.521.261.26newstest20151.241.311.421.631.361.36newstest20161.391.501.621.931.611.60newstest20171.171.221.351.611.291.33Multihead attention (head size =n)dev1.231.381.451.631.631.66newstest20141.131.301.391.541.551.58newstest20151.191.371.451.671.681.65newstest20161.381.651.801.941.851.99newstest20171.161.351.431.601.711.70Talking-head attentiondev1.061.201.381.421.491.58newstest20140.961.111.351.381.461.51newstest20151.041.201.371.391.361.39newstest20161.161.401.671.451.481.62newstest20170.981.151.431.391.481.47Interacting-head attentiondev1.631.641.671.71(+0.07)——newstest20141.431.511.571.63(+0.11)——newstest20151.631.611.741.81(+0.18)——newstest20161.891.882.022.17(+0.24)——newstest20171.521.501.651.74(+0.13)——(f)Multihead attentiondev43.7444.7145.3548.8246.2845.37newstest201441.5042.5743.8047.8444.4443.77newstest201543.1744.1045.9949.6845.8145.49newstest201644.9045.9647.3752.2947.8647.13newstest201742.1143.3644.9948.7144.9144.71Multihead attention (head size =n)dev44.5846.0047.0648.5148.9949.01newstest201443.0944.6345.6347.4048.0447.99newstest201544.3746.0947.5448.8349.1549.56newstest201646.2448.3049.6551.2051.3051.06newstest201743.8245.4746.3848.3448.5548.77Talking-head attentiondev38.1139.6340.7344.0746.1448.60newstest201435.4638.3239.0242.7744.5247.33newstest201537.4838.7140.3544.3546.8449.52newstest201637.0940.2541.4345.9648.3650.70newstest201736.2038.9339.4743.7545.8548.05Interacting-head attentiondev48.0348.2348.7849.15(+0.33)——newstest201446.7246.6347.7848.71(+0.87)——newstest201548.1148.5249.0950.20(+0.52)——newstest201650.8850.7751.4752.71(+0.42)——newstest201747.7348.4849.8350.01(+1.30)——Note. The units of the performances are (a) BLEU, (b) WER, (c) METEOR, (d) ROUGE_L, (e) CIDEr, and (f) YiSi.For the WMT17 EN-CS translation task, the original multihead attention model once again encountered the low-rank bottleneck at 16 heads. As shown in Table5, our interacting-head attention achieved better result (3.87 BLEU/3.05 WER/9.22 METEOR/3.81 ROUGE_L/0.36 CIDEr/4.14 YiSi on evaluation set, and 4.62 BLEU/2.41 WER/9.82 METEOR/4.82 ROUGE_L/0.44 CIDEr/5.25 YiSi, 3.78 BLEU/5.09 WER/9.09 METEOR/4.24 ROUGE_L/0.35 CIDEr/3.97 YiSi, 4.42 BLEU/2.87 WER/3.21 METEOR/4.42 ROUGE_L/0.38 CIDEr/4.83 YiSi, and 3.42 BLEU/3.97 WER/2.79 METEOR/3.66 ROUGE_L/0.33 CIDEr/4.00 YiSi on newstest2014/2015/2016/2017, respectively).Table 5
Overall evaluation scores using four attention-based models on the WMT17 EN-CS evaluation set and test set.
ModelWMT17 subsetNumber of heads/head size2/2564/1288/6416/3232/1664/8(a)Multihead attentiondev11.6913.9613.7614.1412.1011.98newstest201412.9015.6514.8215.5213.2612.71newstest201511.0312.4811.6612.6210.0410.08newstest201611.9813.8513.0914.3211.1610.85newstest201710.2612.4511.7112.369.9610.14Multihead attention (head size =n)dev10.1411.8712.9913.3514.3614.46newstest201411.3213.3414.0414.4915.7915.28newstest20159.1110.1811.5511.6712.6912.05newstest201610.2311.8612.5112.6213.7313.83newstest20178.9910.4311.3011.2312.5412.47Talking-head attentiondev9.8010.0611.7712.0612.7512.68newstest201410.9010.6511.6512.3312.9412.34newstest20158.178.5410.0611.7011.5111.57newstest20169.119.3810.5011.2511.7811.73newstest20178.508.639.9211.0511.5211.83Interacting-head attention (our model)dev17.0117.7617.9318.01(+3.87)——newstest201418.7119.4820.0120.14(+4.62)——newstest201515.5216.4916.3816.40(+3.78)——newstest201617.2018.3718.2918.74(+4.42)——newstest201714.6615.6615.7915.78(+3.42)——(b)Multihead attentiondev15.1312.3315.2710.3029.6016.80newstest201413.9511.6614.158.6225.5713.85newstest201523.6817.9623.3417.1739.5324.06newstest201618.4414.9019.3112.7035.2821.31newstest201720.7018.2021.0616.2136.4421.63Multihead attention (head size =n)dev17.3015.5313.4312.4310.8314.37newstest201415.0214.3512.1211.129.198.56newstest201527.6023.8720.2218.7116.7916.91newstest201621.3420.3418.2416.2413.8014.37newstest201724.2321.5620.0719.2715.9118.66Talking-head attentiondev28.3743.4327.0034.3327.5727.51newstest201421.2533.7028.9434.9725.8626.22newstest201538.0355.3537.9547.2142.4846.87newstest201630.3145.8234.4444.1136.3536.83newstest201731.0147.7535.7741.6335.8635.76Interacting-head attention (our model)dev7.637.278.037.25(3.05)——newstest20146.996.437.166.21(2.41)——newstest201512.9912.0911.9412.08(5.09)——newstest201610.149.809.709.83(2.87)——newstest201712.4812.9511.7112.24(3.97)——(c)Multihead attentiondev17.3719.5219.1619.5518.8517.92newstest201418.4120.8220.4220.8019.9218.93newstest201516.6918.9018.5118.7718.1717.32newstest201617.2119.5919.0519.3618.4417.45newstest201715.6517.9617.5117.8517.1316.26Multihead attention (head size =n)dev16.0917.6218.6518.9119.6818.10newstest201417.0418.8219.6920.0920.8820.06newstest201515.3016.6818.0818.1619.0518.37newstest201615.8517.4218.4118.6219.3918.92newstest201714.7016.0016.9917.1019.9917.42Talking-head attentiondev15.9315.8216.0516.8516.4816.72newstest201416.8616.5816.3317.0416.7616.76newstest201515.0014.7414.9715.7515.6816.12newstest201615.5915.0815.0715.8715.5015.85newstest201714.3314.0114.1115.1514.8715.62Interacting-head attentiondev21.7622.4325.9028.77(+9.22)——newstest201423.3024.2828.0730.62(+9.82)——newstest201521.0821.9825.2827.86(+9.09)——newstest201621.9922.9521.9822.57(+3.21)——newstest201719.8120.5519.7220.64(+2.79)——(d)Multihead attentiondev39.8043.1542.5143.2741.9340.36newstest201441.2544.9844.2245.1443.3142.08newstest201539.0442.1341.4242.2040.7439.46newstest201639.0042.8241.8142.8240.4139.08newstest201737.2940.3439.8040.4638.8337.75Multihead attention (head size =n)dev37.5040.0341.7142.1743.3643.13newstest201438.9041.9143.1143.6644.9544.47newstest201536.2338.5140.9940.8342.4041.67newstest201636.8739.1640.8140.9942.4142.19newstest201735.1137.5238.9538.9440.5940.38Talking-head attentiondev36.0137.5638.5840.7441.3740.95newstest201437.7639.2138.3640.4241.4641.27newstest201534.2335.2236.8039.0240.0640.81newstest201635.1036.0636.4238.5339.4739.95newstest201733.5034.7135.4637.7638.6238.41Interacting-head attention (our model)dev46.0745.9046.8747.08(+3.81)——newstest201448.1748.0749.1349.96(+4.82)——newstest201545.2245.2846.0146.44(+4.24)——newstest201645.8745.9446.8747.24(+4.42)——newstest201742.8342.9343.6544.12(+3.66)——(e)Multihead attentiondev1.421.381.371.401.321.20newstest20141.241.531.471.561.441.30newstest20151.091.301.271.321.241.11newstest20161.101.371.301.371.231.11newstest20171.021.221.201.251.441.06Multihead attention (head size =n)dev1.011.181.291.321.421.44newstest20141.071.291.391.441.531.53newstest20150.911.061.221.221.321.31newstest20160.961.121.231.271.351.37newstest20170.881.031.141.351.251.28Talking-head attentiondev0.900.740.920.800.79newstest20140.990.810.920.790.780.34newstest20150.790.630.900.700.700.32newstest20160.830.670.780.690.690.30newstest20170.780.610.770.680.670.33Interacting-head attentiondev1.631.74(+0.36)1.64newstest20141.791.97(+0.44)1.80newstest20151.551.65(+0.35)1.56newstest20161.621.75(+0.38)1.64newstest20171.431.55(+0.33)1.47(f)Multihead attentiondev28.6432.3731.7432.1231.4129.07newstest201430.3734.5833.7234.4733.0231.14newstest201527.6731.1530.4730.9330.1328.24newstest201628.1032.1131.2231.8930.0728.22newstest201726.2029.8929.2429.5428.5326.80Multihead attention (head size =n)dev26.6528.9430.6231.0632.2632.57newstest201428.0031.2132.4232.9934.2334.23newstest201525.0227.2529.6929.6831.0731.05newstest201625.8028.2930.0230.2731.5031.75newstest201724.2726.5928.1128.1129.7630.05Talking-head attentiondev25.1927.4829.8230.2331.5031.47newstest201427.1229.2829.9430.2031.7531.82newstest201523.6025.3727.9828.5730.1730.55newstest201624.5126.3728.0928.3929.6829.64newstest201722.7924.8926.9027.3728.8128.88Interacting-head attentiondev35.3435.0936.5036.51(+4.14)——newstest201437.7737.5938.7539.83(+5.25)——newstest201534.3234.2535.1735.48(+3.97)——newstest201635.2835.3935.6836.94(4.83)——newstest201732.4032.5834.8133.89(+4.00)——Note. The units of the performances are (a) BLEU, (b) WER, (c) METEOR, (d) ROUGE_L, (e) CIDEr, and (f) YiSi.


### 4.6. Analysis

#### 4.6.1. Horizontal and Longitudinal Analyses

Horizontally, a low-rank bottleneck occurs inevitably, when the number of heads reached a certain level. To some extent, the previous models address this problem at the cost of performance degradation. Machine translation is a generation task between different languages. Compared with the results of previous studies, our model brings significant performance improvement and reveals strong correlations between the subembeddings in different subspaces. Longitudinally, the expressive ability of the model increases with the number of heads, until the latter reaches the bottleneck pointd/n. The superiority of interacting-head attention over the original multihead attention is the result of the function among subembeddings in different subspaces.


#### 4.6.2. Influencing Factor Analysis

As shown in Tables3 and 4, multihead attention with fixed head size and talking-head attention sacrifice performance for solving the low-rank bottleneck. The final performance is primarily affected by four factors: the dimensions of queries, keys, values, and the number of heads. The leading impactors of the attention matrix are the dimensions of queries and keys. In multihead attention with fixed head size, the attention matrix is realized by the dimensions of queries and keys, both of which are equal to the mean sequence length. The model performance hinges on such factors as the dimension of values, the number of heads, as well as the mean sequence before/after the low-rank bottleneck point. In talking-head attention, the linear transformation has a greater impact on the attention matrix before softmax normalization than after that operation. In our experiments, linear transformations were applied with both functions. The poor performance may be attributable to the use of masked multihead attention in the decoder.Interacting-head attention is more effective than the original multihead attention, revealing a strong relationship between the head size of different subspaces. Specifically, when the number of heads is small, there is a strong relationship between different subembeddings in different subspaces. With the growth of the number of heads, the said relationship is gradually weakened. In particular, interacting-head attention degenerates into multihead attention, after the number of heads surpasses d/n.


#### 4.6.3. Training Speed

As shown in Figures4–6 and Tables 6–8, the training time of our model extended significantly when there were 16 heads. The value of 8 was selected to strike a balance between the training time and the decoding performance. In particular, for the IWSLT16 DE-EN dataset, the training time per epoch of our model increased by only 13 minutes, while the performance was improved by 2 BLEU. The slowdown of training is caused by the calculation of the attention relationship among different subspaces.Figure 4
Training time of each epoch of four models on the IWSLT16 DE-EN dataset.Figure 5
Training time of each epoch of four models on the WMT17 EN-DE dataset.Figure 6
Training time of each epoch of four models on WMT17 EN-CS dataset.Table 6
Training time on IWSLT16 DE-EN training dataset.
ModelNumber of heads248163264Multihead attention10 m 10 s10 m 30 s10 m 55 s11 m 30 s12 m 45 s15 m 30 sMultihead attention (head size =n)9 m 20 s9 m 40 s10 m 10 s11 m 05 s12 m 55 s16 m 55 sTalking-head attention10 m 30 s10 m 40 s11 m11 m 40 s15 m 50 sInteracting-head attention10 m 30 s11 m 30 s14 m 30 s24 m 30 s——Note. The units of m and s stand for minute and second, respectively.Table 7
Training time on WMT17 EN-DE training dataset.
ModelNumber of heads/head size2/2564/1288/6416/3232/1664/8Multihead attention9 h 55 m11 h11 h 30 m12 h12 h 30 m14 hMultihead attention (head size =n)11 h 30 m11 h 40 m11 h 50 m12 h 50 m13 h 10 m17 h 30 mTalking-head attention11 h 5 m11 h 40 m11 h 50 m12 h 20 m13 h 50 m15 hInteracting-head attention10 h 40 m11 h 30 m12 h 50 m20 h——Note. The units of h, m, and s stand for hour, minute, and second, respectively.Table 8
Training time on the WMT17 EN-CS training dataset.
ModelNumber of heads/head size2/2564/1288/6416/3232/1664/8Multihead attention1 h1 h 5 m1 h 8 m1 h 10 m1 h 15 m1 h 30 mMultihead attention (head size =n)50 m53 m57 m1 h 3 m1 h 15 m1 h 50 mTalking-head attention58 m59 m1 h1 h 5 m1 h 10 m1 h 30 mInteracting-head attention53 m58 m1 h 13 m1 h 50 m——Note. The units of h, m, and s stand for hour, minute, and second, respectively.


#### 4.6.4. Trainable Variables

Compared with the original multihead attention, the interacting-head attention model does not bring more training parameters but only adds the inner product calculation of tensors in different subspaces. The tensor calculation of different tokens in different subspaces slows down the training process. The slowdown is no big deal, given the huge translation improvement of our model. Besides, this problem can be solved by setting the maximum number of heads as a fixed scalar.


#### 4.6.5. Maximum Number of Heads

To verify the suitability of the number of heads, our model was subjected to an ablation test, with the number of heads changing from 32 to 64. As shown in Table9, a low-rank bottleneck occurred, once the number of heads exceeded a threshold, although the performance of our model was better than that of the original multihead attention. According to the performance variation, the threshold should be d/n as the maximum number of heads. The test was only carried out on the IWSLT16 DE-EN dataset because the training time of our model grows exponentially after the number of heads surpasses the threshold.Table 9
Overall evaluation scores of interacting-head attention on IWSLT16 DE-EN evaluation set and test set.
ModelDatasetSubsetNumber of heads/head size32/1664/8Interacting-head attentionIWSLT16dev24.3819.98Test22.8518.54Note. The unit of the performance is BLEU.


### 4.7. Discussion

In original multihead attention, the translation performance is positively correlated with the number of heads when the heads are between 2 and 16 and negatively correlated with the number of heads when the heads surpass 16. Within a certain range, many heads enhance the expressive power. Once the number exceeds a threshold, a low-rank bottleneck will take place, due to the ultrasmall dimensionality in the subspace. In original multihead attention, when there are many heads, the dimensions of each subquery, subkey, and subvalue meet the condition:dq=dk=dv=dmodel/h. In this case, dq and dk are small, and the sequence length is usually greater than dmodel/h.In multihead attention with fixed head size, the low-rank bottleneck can be avoided by settingdq=dk=n in the subspace, when there are many heads.In talking-head attention, the independence of the subattention matrices is improved through linear transformation between the subhead attention matrices, which is actually performed between thei−thdq and the j−thdk. However, the attention matrices in the sequences are actually sparse. The sparsity can be inferred through the syntactic dependency tree and Bayesian network, and even be observed through visualization tools like Bertviz [35] (https://github.com/jessevig/bertviz). The irregular sparsity makes it difficult to learn the optimal weight coefficients from the overall perspective of the attention matrix.In our model, two thorny problems are resolved. Firstly, the original multihead attention only calculates the hidden features of different tokens in the same space and concatenates all subfeatures into the final output. However, our experimental results show certain connections of different tokens in different subspaces. Secondly, our model adopts the solution of multihead attention with fixed head size and proposes a method for optimizing the maximum number of heads, thereby preventing the low-rank bottleneck induced by the low dimensionality of the subspaces. The disadvantage of our model is the requirement of many tensor calculations, which prolongs the training time. Future research will try to reduce the tensor calculations by capturing the key attention and ignoring the minor attention between the tokens.


## 4.1. Datasets

For the IWSLT16 DE-EN corpus, the experimental data were extracted from the evaluation campaign of the International Conference on Spoken Language Translation (IWSLT 2021) [18]. The extracted data consist of 181 k/12 k sentence pairs as training/evaluation sets. The concatenation of tst2010/2011/2012/2013/2014 was taken as the test set, including around 12 k sentence pairs.For the WMT17 machine translation task, EN-DE and EN-CS MT tasks were chosen as our problems because of the limited memory resources [19, 20]. For WMT17 EN-DE and EN-CS corpora, the training set consists of 5.85 million and 1 million sentence pairs, respectively. For the two corpora, newstest2013 of 3 k sequence pairs was treated as our evaluation set and newstest2014/2015/2016/2017 as the test set.Both datasets were preprocessed through data normalization and subword segmentation, using Moses, a de-facto standard toolkit for statistical machine translation (SMT) [21]. Firstly, the sentence pairs of all datasets were tokenized, and those longer than 80/80/100 on the training sets of IWSLT16 DE-EN, WMT17 EN-CS, and WMT17 EN-DE, respectively, were discarded. After that, a truecase model was trained on the cleaned train set and applied to each subset. Secondly, all sequence pairs were encrypted by bytes pair encoding (BPE) [22], using a sentence piece tool (https://github.com/google/sentencepiece) [23]. This step mitigates the influence of unknown (UNK), padding (PAD), and rare tokens. In IWSLT16 DE-EN and WMT17 EN-DE translation tasks, the source and target languages (EN and DE) have similar alphabets. Therefore, a shared vocabulary with 40,000/80000 tokens was learned on IWSLT16 and WMT17, respectively. In the WMT17 EN-CS translation task, a vocabulary was learned for English (EN) and Czech (CS) separately, because the two languages are distant from each other.As can be seen from Table1 and Figure 3, the sequence lengths of the languages in different datasets obeyed similar distributions and remained consistent with the mean sequence lengths. The length of sequence ranged from 3 to 120, from 1 to 332, and from 1 to 316 for IWSLT16 DE-EN, WMT17 EN-DE, and WMT17 EN-CS, respectively. The mean sequence lengths of the three datasets were set as 20, 25, and 26, respectively.Table 1
Number of sequence pairs and sequence length in IWSLT16 DE-EN, WMT17 EN-DE, and WMT17 EN-CS.
DatasetTrainEvalTestVocabTotal length of the train setMean length of the train setIWSLT16 DE-EN181,14912,09811,82539,6453,766,897/3,826,03820/21WMT17 EN-DE5,852,45730003003/2169/2999/300484,441148,665,327/149,176,20525/25WMT17 EN-CS1,010,91830003003/2656/2999/300525,560/39,67027,825,644/26,152,29927/25Note. Train, Eval, and Test represent the number of sequence pairs of different data subsets, respectively; length refers to the number of tokens in a sentence; total length of the train set is the total number of tokens in the training set; mean length of the train set is the ratio of the total length to the total sequence pairs in the training set.Figure 3
Histogram and line graph of sequence length distribution of IWSLT16 DE-EN, WMT17 EN-DE, and WMT17 EN-CS.


## 4.2. Parameter Settings

The settings of our experimental parameters refer to those in [5] which first proposed the transformer architecture for NMT. Our experiments were arranged based on an appropriate setup on the optimizer, learning rate, and hyperparameters. The optimizer was designed by Adam with β1=0.9, β2=0.997, and ε=10−9 as our optimizer [24]. The learning rate was configured by the warm-up strategy [5] with warm up−steps=8000. During training, the label smoothing rate [25] was set to 0.1, and the dropout was fixed at 0.1. Moreover, because of the limitation of GPU memory, the dimension of the hidden state for linear transformation was set to 1024, and the model dimension was set to 512. To avoid the low-rank bottleneck, the maximum number of heads was obtained by formula (13). Table 2 lists the possible values of the number of heads.Table 2
Possible number of heads in our model.
DatasetModelNumber of headsIWSLT16 DE-EN51225 [2, 4, 8, 16]WMT17 EN-DE51220 [2, 4, 8, 16]WMT17 EN-CS51220 [2, 4, 8, 16]All the experiments were conducted with TensorFlow 1.4.1 and keras 2.1.3 with reference project (https://github.com/Lsdefine/attention-is-all-you-need-keras). The attained model cpt files can also be converted into PyTorch bin files with transformers [26]. All the experiments were completed by two NVIDIA Tesla V100 GPUs of 32 GB memory.During the inference, a beam search algorithm was used with beam size 4 and batch size 8 to decode all test sets. The length penalty was set to 1 and 0.6 for IWSLT16 and WMT17 test sets, respectively.


## 4.3. Evaluation Metrics

The machine translation quality was evaluated objectively by multiple metrics, including bilingual evaluation understudy (BLEU) [27], word error rate (WER) [28], metric for evaluation of translation with explicit ordering (METEOR) [29], recall-oriented understudy of gisting evaluation (ROUGE) [30], consensus-based image description evaluation (CIDEr) [31]and YiSi [32].(1)
BLEU [27]. BLEU, one of the most manifold evaluation methods for machine translation, uses N-gram token matching to evaluate the similarity between the reference and the candidate. The quality is positively correlated with the proximity between the translations and the references.(2)
WER [28]. Similar to translation edit rate (TER) [33], WER computes the word error rate between the reference and hypothetical translation. The word errors include the number of substitutions, insertions, and deletions from the translation to the reference. The rate is the ratio of word errors to the length of the reference.(3)
METEOR [29]. Based on explicit word-to-word matches, METEOR includes identical words in the surface forms, morphological variants in stemmed forms, and synonyms in meanings between the reference and the candidate.(4)
ROUGE [30]. ROUGE was introduced by Chin-Yew Lin for text summarization. It contains four different measures: ROUGE-N, ROUGE-L, ROUGE-W, and ROUGE-S. Here, ROUGE-L is selected as the metric to evaluate machine translation. Note that L is the abbreviation of the longest common subsequence (LCS) between the reference and the candidate.(5)
CIDEr [31]. CIDEr is originally used to evaluate the generated image descriptions. It measures the similarity of a generated sequence against a set of ground truth sentences written by humans. This similarity reflects how well the generated descriptions capture the information of grammaticality, saliency, importance, and accuracy.(6)
YiSi [32]. YiSi is a family of quality evaluation and estimation metrics for semantic machine translation. In this paper, YiSi-1 is selected for its high average correlation with human assessment, thanks to the use of multilingual bidirectional encoder representations from transformers (BERT).BLEU, WER, METEOR, ROUGE_L, CIDEr, YiSi were computed using multi-bleu.perl (https://github.com/moses-smt/mosesdecoder), pyter (https://pypi.org/project/pyter/), nlg-eval (METEOR, ROUGE_L, CIDEr using https://github.com/Maluuba/nlg-eval) [34], and YiSi (https://github.com/chikiulo/yisi).


## 4.4. Baselines

(1)
Original multihead attention by Vaswani et al. [5]: the original transformer-based model is implemented based on multihead attention, which brings more expressive power than single head attention. The model linearly projects the queries, keys, and values with different, learned projection matrices to dk, dk, and dv dimensions, respectively. Each head yields dv-dimensional output values. All the attention heads are concatenated into the final values.(2)
Multihead attention (head size equaling sequence length) by Bhojanapalli et al. [10]: in the original multihead attention, the scaling between the number of heads and head size leads to a low-rank bottleneck. To overcome the problem, Bhojanapalli et al. set the head size to input sequence length and keep it independent of the number of heads. In this way, each head acquires more expressive power. The effectiveness of their approach was verified through experiments on the two tasks of Stanford Question Answering Dataset (SQuAD) and Multigenre Natural Language Inference (MNLI).(3)
Talking-head attention by Shazeer et al. [13]: with the increase in the number of heads, the dimensionality of query vectors and key vectors becomes so low that the dot product between the two types of vectors no longer includes useful information. This is what is commonly called a low-rank bottleneck. To address the problem, talking-head attention inserts two linear learned projection matrices across the attention-head dimension of the attention-logits tensor, allowing each head attention to target any subquery vector and subkey vector. The feasibility of this attention mechanism was tested on several seq2seq NLP tasks. But Shazeer et al. did not test the mechanism on any NMT task. Therefore, this paper implements the mechanism on both the evaluation benchmarks and compares it with our model.


## 4.5. Results

For the IWSLT2016 DE-EN translation task, all models almost reached the peak performance at 16 heads. As shown in Table3, the performance of the original multihead attention clearly declined by 3.31 BLEU/0.41 WER/2.72 METEOR/2.83 ROUGE_L/0.30 CIDEr/3.23 YiSi on the evaluation set and 1.15 WER/2.63 METEOR/2.69 ROUGE_L/0.27 CIDEr/2.88 YiSi on the test set, as the number of heads increased from 16 to 32. The trend signifies the occurrence of the low-rank bottleneck. As for the multihead attention with fixed head size, the performance reached a relatively stable state, when the number of heads was equal to 16. As the number of heads increased, the performance improved slightly. In talking-head attention, the performance varied similarly to multihead attention with fixed head size. When it comes to our interacting-head attention, the performance improved significantly by 2.78 BLEU/0.85 WER/2.90 METEOR/2.65 ROUGE_L/0.29 CIDEr/2.97 YiSi on the evaluation set and 2.43 BLEU/1.38 WER/3.05 METEOR/2.70 ROUGE_L/0.30 CIDEr/3.59 YiSi on the test set, compared with the original multihead attention.Table 3
Overall evaluation scores using four attention-based models on the IWSLT16 DE-EN evaluation set and test set.
ModelIWSLT16 subsetNumber of heads/Head size2/2564/1288/6416/3232/1664/8(a)Multihead attentiondev22.7325.7126.4427.2323.9219.50Test21.1524.0024.3825.1822.0517.94Multihead attention (head size =n, dmodel = 512)dev20.9323.0824.7926.0026.9426.38Test19.3521.3822.8124.6124.8624.59Talking-head attentiondev20.6621.3922.4724.0224.2123.65Test19.1319.8220.8722.2422.3021.72Interacting-head attentiondev29.5929.9429.5430.01(+2.78)——Test27.6128.0127.4427.61(+2.43)——(b)Multihead attentiondev5.824.853.563.103.518.73Test9.037.927.715.877.0214.39Multihead attention (head size =n, dmodel = 512)dev6.906.005.294.594.553.97Test11.4710.108.988.168.117.96Talking-head attentiondev9.037.316.584.814.514.33Test14.4611.909.839.108.698.90Interacting-head attentiondev2.412.442.312.25(0.85)——Test6.605.344.774.49(1.38)——(c)Multihead attentiondev25.4226.7228.5829.1426.4222.63Test24.7725.9627.6728.2025.5722.02Multihead attention (head size =n, dmodel = 512)dev23.4725.6925.7626.5127.0527.12Test22.8424.9524.9525.8825.2826.36Talking-head attentiondev22.4824.2823.8024.4625.2826.76Test21.8523.4623.1823.9424.4925.85Interacting-head attentiondev31.7331.9431.6532.04(+2.90)——Test30.8731.1130.7631.25(+3.05)——(d)Multihead attentiondev53.7255.6857.8558.7655.9350.52Test52.2354.3056.2357.2654.5749.24Multihead attention (head size =n, dmodel = 512)dev51.4654.0255.8355.5256.5356.99Test50.2152.7055.4454.3154.1855.60Talking-head attentiondev49.2651.6452.3853.7254.9255.78Test48.2350.1951.1952.3053.3354.45Interacting-head attentiondev61.0261.2961.1461.41(+2.65)——Test59.2559.8059.6259.96(+2.70)——(e)Multihead attentiondev1.942.392.612.692.391.89Test1.802.202.392.492.221.75Multihead attention (head size =n, dmodel = 512)dev1.992.202.242.272.332.38Test1.842.052.082.102.162.21Talking-head attentiondev1.701.871.842.092.202.32Test1.571.721.671.912.012.05Interacting-head attentiondev2.932.962.932.98(+0.29)——Test2.692.752.712.79(+0.30)——(f)Multihead attentiondev54.3355.9857.9158.4855.2549.83Test53.3454.1855.8856.4553.5748.44Multihead attention (head size =n, dmodel = 512)dev52.0554.2555.8557.1557.3356.45Test50.6352.5754.1855.5156.7955.94Talking-head attentiondev50.1951.3252.3253.0255.5557.06Test48.9749.6350.6351.6953.7255.83Interacting-head attentiondev60.9261.2260.9161.45(+2.97)——Test58.6859.1558.6760.04(+3.59)——Note. The units of the performances are (a) BLEU, (b) WER, (c) METEOR, (d) ROUGE_L, (e) CIDEr, and (f) YiSi.For the WMT17 EN-DE translation task, the original multihead attention had the best performance at 16 heads and performed poorer and poorer with the growing number of heads. As shown in Table4, multihead attention with fixed head size and talking-head attention achieved a slight improvement by solving the low-rank bottleneck. With our interacting-head attention, the performance improved by 2.31 BLEU/5.94 WER/1.46 METEOR/1.35 ROUGE_L/0.07 CIDEr/0.33 YiSi and 1.62 BLEU/6.04 WER/1.39 METEOR/0.11 CIDEr/0.87 YiSi on the evaluation set and newstest2014, respectively.Table 4
Overall evaluation scores using four attention-based models on the WMT17 EN-DE evaluation set and test set.
ModelWMT17 subsetNumber of heads/head size2/2564/1288/6416/3232/1664/8(a)Multihead attentiondev8.3812.5612.4815.1512.2012.45newstest20148.7711.3112.8515.7311.6112.28newstest20159.3612.2713.8016.7911.9013.23newstest201611.4014.8316.7920.9715.1716.16newstest20178.8311.8313.1516.4111.6312.99Multihead attention (head size =n)dev11.6612.5113.7014.5714.6115.29newstest201411.6413.2614.9315.4214.9615.00newstest201512.0414.0715.7816.5716.6717.57newstest201614.7117.5719.7720.3319.8020.08newstest201711.7213.5414.9216.0316.0716.33Talking-head attentiondev10.9013.8915.3115.3715.2315.50newstest201411.2112.6414.3315.2815.2915.39newstest201511.8512.8114.6115.9515.5515.36newstest201614.2216.3318.8419.5319.3420.43newstest201711.1512.8114.3515.4914.4415.36Interacting-head attentiondev13.0714.8414.5317.46(+2.31)——newstest201410.9114.7215.7517.35(+1.62)——newstest201511.9915.4316.9018.40(+1.21)——newstest201614.9519.6121.1222.36(+1.39)——newstest201711.0915.0017.3017.67(+1.26)——(b)Multihead attentiondev39.2337.8028.2315.5732.9322.47newstest201446.8545.5932.1019.6839.5928.80newstest201545.2244.3132.6422.6842.7828.58newstest201640.1439.3528.5115.9132.0423.97newstest201743.8542.7432.1219.0738.3826.50Multihead attention (head size =n)dev39.3736.7325.1122.9716.6713.42newstest201446.4035.7732.5628.6518.5618.77newstest201546.9338.4533.1628.0323.1320.07newstest201642.9130.2427.6622.7717.1513.26newstest201744.6534.6029.8226.7819.7516.52Talking-head attentiondev48.1740.6339.0324.8020.1316.25newstest201448.1948.0252.9833.9327.5820.32newstest201561.0953.8052.3333.4331.1322.05newstest201647.0247.2847.4231.0823.1614.82newstest201751.1347.3450.6334.5228.0120.05Interacting-head attentiondev58.1035.2020.839.63(5.94)——newstest201468.3947.2929.0113.64(6.04)——newstest201579.3948.5529.6416.05(6.63)——newstest201667.1244.2125.9411.27(4.64)——newstest201781.5649.0730.8915.23(3.84)——(c)Multihead attentiondev19.3619.5420.3123.1620.5620.53newstest201419.3919.6420.8323.7120.7220.97newstest201519.3619.6021.1524.1120.7021.06newstest201620.4321.2722.7326.2922.4422.63newstest201719.5219.8521.0523.9620.7021.11Multihead attention (head size =n)dev19.4720.9621.0423.0123.3223.79newstest201419.6921.4522.3023.5223.9124.45newstest201519.6421.5422.6123.7024.0224.46newstest201620.9523.2624.4725.6525.4426.99newstest201719.6821.5022.2523.7324.0524.49Talking-head attentiondev17.7119.5521.3823.2223.5123.19newstest201417.7319.5921.8723.6523.7323.75newstest201517.3319.3922.0323.8124.1424.26newstest201618.6020.5223.1925.4026.0225.88newstest201717.7419.3921.9023.7123.6624.15Interacting-head attentiondev22.4322.6123.8724.62(+1.46)——newstest201422.6722.6624.3525.10(+1.39)——newstest201522.8123.0924.5225.53(+1.42)——newstest201624.6925.0026.6827.27(+0.98)——newstest201722.1222.7624.3825.66(+1.70)——(d)Multihead attentiondev40.0840.1941.5445.7742.4742.79newstest201436.6537.8140.2249.0440.4041.18newstest201537.3339.6342.5646.8441.6942.71newstest201639.8841.9844.4044.8844.5245.13newstest201736.9439.0141.4346.0941.0242.02Multihead attention (head size =n)dev41.3943.6744.8945.7945.8346.01newstest201439.7542.3543.6244.6945.0747.25newstest201540.7643.3745.5446.1846.2248.15newstest201643.1846.5148.2149.3448.9650.08newstest201740.4143.0544.8745.5245.5546.08Talking-head attentiondev38.4240.2042.0343.9944.6745.33newstest201435.6739.2342.5142.7445.8646.09newstest201537.9438.6141.0044.1544.9246.67newstest201638.8341.2142.8846.2848.0546.99newstest201737.2939.5840.9343.3045.2345.65Interacting-head attentiondev45.4746.0346.5447.12(+1.35)——newstest201443.9543.7745.1548.20——newstest201545.3345.8346.6147.31(+0.51)——newstest201647.9748.8349.9450.47(+5.59)——newstest201743.6144.5845.9145.99——(e)Multihead attentiondev1.261.301.371.641.371.38newstest20141.131.191.261.521.261.26newstest20151.241.311.421.631.361.36newstest20161.391.501.621.931.611.60newstest20171.171.221.351.611.291.33Multihead attention (head size =n)dev1.231.381.451.631.631.66newstest20141.131.301.391.541.551.58newstest20151.191.371.451.671.681.65newstest20161.381.651.801.941.851.99newstest20171.161.351.431.601.711.70Talking-head attentiondev1.061.201.381.421.491.58newstest20140.961.111.351.381.461.51newstest20151.041.201.371.391.361.39newstest20161.161.401.671.451.481.62newstest20170.981.151.431.391.481.47Interacting-head attentiondev1.631.641.671.71(+0.07)——newstest20141.431.511.571.63(+0.11)——newstest20151.631.611.741.81(+0.18)——newstest20161.891.882.022.17(+0.24)——newstest20171.521.501.651.74(+0.13)——(f)Multihead attentiondev43.7444.7145.3548.8246.2845.37newstest201441.5042.5743.8047.8444.4443.77newstest201543.1744.1045.9949.6845.8145.49newstest201644.9045.9647.3752.2947.8647.13newstest201742.1143.3644.9948.7144.9144.71Multihead attention (head size =n)dev44.5846.0047.0648.5148.9949.01newstest201443.0944.6345.6347.4048.0447.99newstest201544.3746.0947.5448.8349.1549.56newstest201646.2448.3049.6551.2051.3051.06newstest201743.8245.4746.3848.3448.5548.77Talking-head attentiondev38.1139.6340.7344.0746.1448.60newstest201435.4638.3239.0242.7744.5247.33newstest201537.4838.7140.3544.3546.8449.52newstest201637.0940.2541.4345.9648.3650.70newstest201736.2038.9339.4743.7545.8548.05Interacting-head attentiondev48.0348.2348.7849.15(+0.33)——newstest201446.7246.6347.7848.71(+0.87)——newstest201548.1148.5249.0950.20(+0.52)——newstest201650.8850.7751.4752.71(+0.42)——newstest201747.7348.4849.8350.01(+1.30)——Note. The units of the performances are (a) BLEU, (b) WER, (c) METEOR, (d) ROUGE_L, (e) CIDEr, and (f) YiSi.For the WMT17 EN-CS translation task, the original multihead attention model once again encountered the low-rank bottleneck at 16 heads. As shown in Table5, our interacting-head attention achieved better result (3.87 BLEU/3.05 WER/9.22 METEOR/3.81 ROUGE_L/0.36 CIDEr/4.14 YiSi on evaluation set, and 4.62 BLEU/2.41 WER/9.82 METEOR/4.82 ROUGE_L/0.44 CIDEr/5.25 YiSi, 3.78 BLEU/5.09 WER/9.09 METEOR/4.24 ROUGE_L/0.35 CIDEr/3.97 YiSi, 4.42 BLEU/2.87 WER/3.21 METEOR/4.42 ROUGE_L/0.38 CIDEr/4.83 YiSi, and 3.42 BLEU/3.97 WER/2.79 METEOR/3.66 ROUGE_L/0.33 CIDEr/4.00 YiSi on newstest2014/2015/2016/2017, respectively).Table 5
Overall evaluation scores using four attention-based models on the WMT17 EN-CS evaluation set and test set.
ModelWMT17 subsetNumber of heads/head size2/2564/1288/6416/3232/1664/8(a)Multihead attentiondev11.6913.9613.7614.1412.1011.98newstest201412.9015.6514.8215.5213.2612.71newstest201511.0312.4811.6612.6210.0410.08newstest201611.9813.8513.0914.3211.1610.85newstest201710.2612.4511.7112.369.9610.14Multihead attention (head size =n)dev10.1411.8712.9913.3514.3614.46newstest201411.3213.3414.0414.4915.7915.28newstest20159.1110.1811.5511.6712.6912.05newstest201610.2311.8612.5112.6213.7313.83newstest20178.9910.4311.3011.2312.5412.47Talking-head attentiondev9.8010.0611.7712.0612.7512.68newstest201410.9010.6511.6512.3312.9412.34newstest20158.178.5410.0611.7011.5111.57newstest20169.119.3810.5011.2511.7811.73newstest20178.508.639.9211.0511.5211.83Interacting-head attention (our model)dev17.0117.7617.9318.01(+3.87)——newstest201418.7119.4820.0120.14(+4.62)——newstest201515.5216.4916.3816.40(+3.78)——newstest201617.2018.3718.2918.74(+4.42)——newstest201714.6615.6615.7915.78(+3.42)——(b)Multihead attentiondev15.1312.3315.2710.3029.6016.80newstest201413.9511.6614.158.6225.5713.85newstest201523.6817.9623.3417.1739.5324.06newstest201618.4414.9019.3112.7035.2821.31newstest201720.7018.2021.0616.2136.4421.63Multihead attention (head size =n)dev17.3015.5313.4312.4310.8314.37newstest201415.0214.3512.1211.129.198.56newstest201527.6023.8720.2218.7116.7916.91newstest201621.3420.3418.2416.2413.8014.37newstest201724.2321.5620.0719.2715.9118.66Talking-head attentiondev28.3743.4327.0034.3327.5727.51newstest201421.2533.7028.9434.9725.8626.22newstest201538.0355.3537.9547.2142.4846.87newstest201630.3145.8234.4444.1136.3536.83newstest201731.0147.7535.7741.6335.8635.76Interacting-head attention (our model)dev7.637.278.037.25(3.05)——newstest20146.996.437.166.21(2.41)——newstest201512.9912.0911.9412.08(5.09)——newstest201610.149.809.709.83(2.87)——newstest201712.4812.9511.7112.24(3.97)——(c)Multihead attentiondev17.3719.5219.1619.5518.8517.92newstest201418.4120.8220.4220.8019.9218.93newstest201516.6918.9018.5118.7718.1717.32newstest201617.2119.5919.0519.3618.4417.45newstest201715.6517.9617.5117.8517.1316.26Multihead attention (head size =n)dev16.0917.6218.6518.9119.6818.10newstest201417.0418.8219.6920.0920.8820.06newstest201515.3016.6818.0818.1619.0518.37newstest201615.8517.4218.4118.6219.3918.92newstest201714.7016.0016.9917.1019.9917.42Talking-head attentiondev15.9315.8216.0516.8516.4816.72newstest201416.8616.5816.3317.0416.7616.76newstest201515.0014.7414.9715.7515.6816.12newstest201615.5915.0815.0715.8715.5015.85newstest201714.3314.0114.1115.1514.8715.62Interacting-head attentiondev21.7622.4325.9028.77(+9.22)——newstest201423.3024.2828.0730.62(+9.82)——newstest201521.0821.9825.2827.86(+9.09)——newstest201621.9922.9521.9822.57(+3.21)——newstest201719.8120.5519.7220.64(+2.79)——(d)Multihead attentiondev39.8043.1542.5143.2741.9340.36newstest201441.2544.9844.2245.1443.3142.08newstest201539.0442.1341.4242.2040.7439.46newstest201639.0042.8241.8142.8240.4139.08newstest201737.2940.3439.8040.4638.8337.75Multihead attention (head size =n)dev37.5040.0341.7142.1743.3643.13newstest201438.9041.9143.1143.6644.9544.47newstest201536.2338.5140.9940.8342.4041.67newstest201636.8739.1640.8140.9942.4142.19newstest201735.1137.5238.9538.9440.5940.38Talking-head attentiondev36.0137.5638.5840.7441.3740.95newstest201437.7639.2138.3640.4241.4641.27newstest201534.2335.2236.8039.0240.0640.81newstest201635.1036.0636.4238.5339.4739.95newstest201733.5034.7135.4637.7638.6238.41Interacting-head attention (our model)dev46.0745.9046.8747.08(+3.81)——newstest201448.1748.0749.1349.96(+4.82)——newstest201545.2245.2846.0146.44(+4.24)——newstest201645.8745.9446.8747.24(+4.42)——newstest201742.8342.9343.6544.12(+3.66)——(e)Multihead attentiondev1.421.381.371.401.321.20newstest20141.241.531.471.561.441.30newstest20151.091.301.271.321.241.11newstest20161.101.371.301.371.231.11newstest20171.021.221.201.251.441.06Multihead attention (head size =n)dev1.011.181.291.321.421.44newstest20141.071.291.391.441.531.53newstest20150.911.061.221.221.321.31newstest20160.961.121.231.271.351.37newstest20170.881.031.141.351.251.28Talking-head attentiondev0.900.740.920.800.79newstest20140.990.810.920.790.780.34newstest20150.790.630.900.700.700.32newstest20160.830.670.780.690.690.30newstest20170.780.610.770.680.670.33Interacting-head attentiondev1.631.74(+0.36)1.64newstest20141.791.97(+0.44)1.80newstest20151.551.65(+0.35)1.56newstest20161.621.75(+0.38)1.64newstest20171.431.55(+0.33)1.47(f)Multihead attentiondev28.6432.3731.7432.1231.4129.07newstest201430.3734.5833.7234.4733.0231.14newstest201527.6731.1530.4730.9330.1328.24newstest201628.1032.1131.2231.8930.0728.22newstest201726.2029.8929.2429.5428.5326.80Multihead attention (head size =n)dev26.6528.9430.6231.0632.2632.57newstest201428.0031.2132.4232.9934.2334.23newstest201525.0227.2529.6929.6831.0731.05newstest201625.8028.2930.0230.2731.5031.75newstest201724.2726.5928.1128.1129.7630.05Talking-head attentiondev25.1927.4829.8230.2331.5031.47newstest201427.1229.2829.9430.2031.7531.82newstest201523.6025.3727.9828.5730.1730.55newstest201624.5126.3728.0928.3929.6829.64newstest201722.7924.8926.9027.3728.8128.88Interacting-head attentiondev35.3435.0936.5036.51(+4.14)——newstest201437.7737.5938.7539.83(+5.25)——newstest201534.3234.2535.1735.48(+3.97)——newstest201635.2835.3935.6836.94(4.83)——newstest201732.4032.5834.8133.89(+4.00)——Note. The units of the performances are (a) BLEU, (b) WER, (c) METEOR, (d) ROUGE_L, (e) CIDEr, and (f) YiSi.


## 4.6. Analysis

### 4.6.1. Horizontal and Longitudinal Analyses

Horizontally, a low-rank bottleneck occurs inevitably, when the number of heads reached a certain level. To some extent, the previous models address this problem at the cost of performance degradation. Machine translation is a generation task between different languages. Compared with the results of previous studies, our model brings significant performance improvement and reveals strong correlations between the subembeddings in different subspaces. Longitudinally, the expressive ability of the model increases with the number of heads, until the latter reaches the bottleneck pointd/n. The superiority of interacting-head attention over the original multihead attention is the result of the function among subembeddings in different subspaces.


### 4.6.2. Influencing Factor Analysis

As shown in Tables3 and 4, multihead attention with fixed head size and talking-head attention sacrifice performance for solving the low-rank bottleneck. The final performance is primarily affected by four factors: the dimensions of queries, keys, values, and the number of heads. The leading impactors of the attention matrix are the dimensions of queries and keys. In multihead attention with fixed head size, the attention matrix is realized by the dimensions of queries and keys, both of which are equal to the mean sequence length. The model performance hinges on such factors as the dimension of values, the number of heads, as well as the mean sequence before/after the low-rank bottleneck point. In talking-head attention, the linear transformation has a greater impact on the attention matrix before softmax normalization than after that operation. In our experiments, linear transformations were applied with both functions. The poor performance may be attributable to the use of masked multihead attention in the decoder.Interacting-head attention is more effective than the original multihead attention, revealing a strong relationship between the head size of different subspaces. Specifically, when the number of heads is small, there is a strong relationship between different subembeddings in different subspaces. With the growth of the number of heads, the said relationship is gradually weakened. In particular, interacting-head attention degenerates into multihead attention, after the number of heads surpasses d/n.


### 4.6.3. Training Speed

As shown in Figures4–6 and Tables 6–8, the training time of our model extended significantly when there were 16 heads. The value of 8 was selected to strike a balance between the training time and the decoding performance. In particular, for the IWSLT16 DE-EN dataset, the training time per epoch of our model increased by only 13 minutes, while the performance was improved by 2 BLEU. The slowdown of training is caused by the calculation of the attention relationship among different subspaces.Figure 4
Training time of each epoch of four models on the IWSLT16 DE-EN dataset.Figure 5
Training time of each epoch of four models on the WMT17 EN-DE dataset.Figure 6
Training time of each epoch of four models on WMT17 EN-CS dataset.Table 6
Training time on IWSLT16 DE-EN training dataset.
ModelNumber of heads248163264Multihead attention10 m 10 s10 m 30 s10 m 55 s11 m 30 s12 m 45 s15 m 30 sMultihead attention (head size =n)9 m 20 s9 m 40 s10 m 10 s11 m 05 s12 m 55 s16 m 55 sTalking-head attention10 m 30 s10 m 40 s11 m11 m 40 s15 m 50 sInteracting-head attention10 m 30 s11 m 30 s14 m 30 s24 m 30 s——Note. The units of m and s stand for minute and second, respectively.Table 7
Training time on WMT17 EN-DE training dataset.
ModelNumber of heads/head size2/2564/1288/6416/3232/1664/8Multihead attention9 h 55 m11 h11 h 30 m12 h12 h 30 m14 hMultihead attention (head size =n)11 h 30 m11 h 40 m11 h 50 m12 h 50 m13 h 10 m17 h 30 mTalking-head attention11 h 5 m11 h 40 m11 h 50 m12 h 20 m13 h 50 m15 hInteracting-head attention10 h 40 m11 h 30 m12 h 50 m20 h——Note. The units of h, m, and s stand for hour, minute, and second, respectively.Table 8
Training time on the WMT17 EN-CS training dataset.
ModelNumber of heads/head size2/2564/1288/6416/3232/1664/8Multihead attention1 h1 h 5 m1 h 8 m1 h 10 m1 h 15 m1 h 30 mMultihead attention (head size =n)50 m53 m57 m1 h 3 m1 h 15 m1 h 50 mTalking-head attention58 m59 m1 h1 h 5 m1 h 10 m1 h 30 mInteracting-head attention53 m58 m1 h 13 m1 h 50 m——Note. The units of h, m, and s stand for hour, minute, and second, respectively.


### 4.6.4. Trainable Variables

Compared with the original multihead attention, the interacting-head attention model does not bring more training parameters but only adds the inner product calculation of tensors in different subspaces. The tensor calculation of different tokens in different subspaces slows down the training process. The slowdown is no big deal, given the huge translation improvement of our model. Besides, this problem can be solved by setting the maximum number of heads as a fixed scalar.


### 4.6.5. Maximum Number of Heads

To verify the suitability of the number of heads, our model was subjected to an ablation test, with the number of heads changing from 32 to 64. As shown in Table9, a low-rank bottleneck occurred, once the number of heads exceeded a threshold, although the performance of our model was better than that of the original multihead attention. According to the performance variation, the threshold should be d/n as the maximum number of heads. The test was only carried out on the IWSLT16 DE-EN dataset because the training time of our model grows exponentially after the number of heads surpasses the threshold.Table 9
Overall evaluation scores of interacting-head attention on IWSLT16 DE-EN evaluation set and test set.
ModelDatasetSubsetNumber of heads/head size32/1664/8Interacting-head attentionIWSLT16dev24.3819.98Test22.8518.54Note. The unit of the performance is BLEU.


## 4.6.1. Horizontal and Longitudinal Analyses

Horizontally, a low-rank bottleneck occurs inevitably, when the number of heads reached a certain level. To some extent, the previous models address this problem at the cost of performance degradation. Machine translation is a generation task between different languages. Compared with the results of previous studies, our model brings significant performance improvement and reveals strong correlations between the subembeddings in different subspaces. Longitudinally, the expressive ability of the model increases with the number of heads, until the latter reaches the bottleneck pointd/n. The superiority of interacting-head attention over the original multihead attention is the result of the function among subembeddings in different subspaces.


## 4.6.2. Influencing Factor Analysis

As shown in Tables3 and 4, multihead attention with fixed head size and talking-head attention sacrifice performance for solving the low-rank bottleneck. The final performance is primarily affected by four factors: the dimensions of queries, keys, values, and the number of heads. The leading impactors of the attention matrix are the dimensions of queries and keys. In multihead attention with fixed head size, the attention matrix is realized by the dimensions of queries and keys, both of which are equal to the mean sequence length. The model performance hinges on such factors as the dimension of values, the number of heads, as well as the mean sequence before/after the low-rank bottleneck point. In talking-head attention, the linear transformation has a greater impact on the attention matrix before softmax normalization than after that operation. In our experiments, linear transformations were applied with both functions. The poor performance may be attributable to the use of masked multihead attention in the decoder.Interacting-head attention is more effective than the original multihead attention, revealing a strong relationship between the head size of different subspaces. Specifically, when the number of heads is small, there is a strong relationship between different subembeddings in different subspaces. With the growth of the number of heads, the said relationship is gradually weakened. In particular, interacting-head attention degenerates into multihead attention, after the number of heads surpasses d/n.


## 4.6.3. Training Speed

As shown in Figures4–6 and Tables 6–8, the training time of our model extended significantly when there were 16 heads. The value of 8 was selected to strike a balance between the training time and the decoding performance. In particular, for the IWSLT16 DE-EN dataset, the training time per epoch of our model increased by only 13 minutes, while the performance was improved by 2 BLEU. The slowdown of training is caused by the calculation of the attention relationship among different subspaces.Figure 4
Training time of each epoch of four models on the IWSLT16 DE-EN dataset.Figure 5
Training time of each epoch of four models on the WMT17 EN-DE dataset.Figure 6
Training time of each epoch of four models on WMT17 EN-CS dataset.Table 6
Training time on IWSLT16 DE-EN training dataset.
ModelNumber of heads248163264Multihead attention10 m 10 s10 m 30 s10 m 55 s11 m 30 s12 m 45 s15 m 30 sMultihead attention (head size =n)9 m 20 s9 m 40 s10 m 10 s11 m 05 s12 m 55 s16 m 55 sTalking-head attention10 m 30 s10 m 40 s11 m11 m 40 s15 m 50 sInteracting-head attention10 m 30 s11 m 30 s14 m 30 s24 m 30 s——Note. The units of m and s stand for minute and second, respectively.Table 7
Training time on WMT17 EN-DE training dataset.
ModelNumber of heads/head size2/2564/1288/6416/3232/1664/8Multihead attention9 h 55 m11 h11 h 30 m12 h12 h 30 m14 hMultihead attention (head size =n)11 h 30 m11 h 40 m11 h 50 m12 h 50 m13 h 10 m17 h 30 mTalking-head attention11 h 5 m11 h 40 m11 h 50 m12 h 20 m13 h 50 m15 hInteracting-head attention10 h 40 m11 h 30 m12 h 50 m20 h——Note. The units of h, m, and s stand for hour, minute, and second, respectively.Table 8
Training time on the WMT17 EN-CS training dataset.
ModelNumber of heads/head size2/2564/1288/6416/3232/1664/8Multihead attention1 h1 h 5 m1 h 8 m1 h 10 m1 h 15 m1 h 30 mMultihead attention (head size =n)50 m53 m57 m1 h 3 m1 h 15 m1 h 50 mTalking-head attention58 m59 m1 h1 h 5 m1 h 10 m1 h 30 mInteracting-head attention53 m58 m1 h 13 m1 h 50 m——Note. The units of h, m, and s stand for hour, minute, and second, respectively.


## 4.6.4. Trainable Variables

Compared with the original multihead attention, the interacting-head attention model does not bring more training parameters but only adds the inner product calculation of tensors in different subspaces. The tensor calculation of different tokens in different subspaces slows down the training process. The slowdown is no big deal, given the huge translation improvement of our model. Besides, this problem can be solved by setting the maximum number of heads as a fixed scalar.


## 4.6.5. Maximum Number of Heads

To verify the suitability of the number of heads, our model was subjected to an ablation test, with the number of heads changing from 32 to 64. As shown in Table9, a low-rank bottleneck occurred, once the number of heads exceeded a threshold, although the performance of our model was better than that of the original multihead attention. According to the performance variation, the threshold should be d/n as the maximum number of heads. The test was only carried out on the IWSLT16 DE-EN dataset because the training time of our model grows exponentially after the number of heads surpasses the threshold.Table 9
Overall evaluation scores of interacting-head attention on IWSLT16 DE-EN evaluation set and test set.
ModelDatasetSubsetNumber of heads/head size32/1664/8Interacting-head attentionIWSLT16dev24.3819.98Test22.8518.54Note. The unit of the performance is BLEU.


## 4.7. Discussion

In original multihead attention, the translation performance is positively correlated with the number of heads when the heads are between 2 and 16 and negatively correlated with the number of heads when the heads surpass 16. Within a certain range, many heads enhance the expressive power. Once the number exceeds a threshold, a low-rank bottleneck will take place, due to the ultrasmall dimensionality in the subspace. In original multihead attention, when there are many heads, the dimensions of each subquery, subkey, and subvalue meet the condition:dq=dk=dv=dmodel/h. In this case, dq and dk are small, and the sequence length is usually greater than dmodel/h.In multihead attention with fixed head size, the low-rank bottleneck can be avoided by settingdq=dk=n in the subspace, when there are many heads.In talking-head attention, the independence of the subattention matrices is improved through linear transformation between the subhead attention matrices, which is actually performed between thei−thdq and the j−thdk. However, the attention matrices in the sequences are actually sparse. The sparsity can be inferred through the syntactic dependency tree and Bayesian network, and even be observed through visualization tools like Bertviz [35] (https://github.com/jessevig/bertviz). The irregular sparsity makes it difficult to learn the optimal weight coefficients from the overall perspective of the attention matrix.In our model, two thorny problems are resolved. Firstly, the original multihead attention only calculates the hidden features of different tokens in the same space and concatenates all subfeatures into the final output. However, our experimental results show certain connections of different tokens in different subspaces. Secondly, our model adopts the solution of multihead attention with fixed head size and proposes a method for optimizing the maximum number of heads, thereby preventing the low-rank bottleneck induced by the low dimensionality of the subspaces. The disadvantage of our model is the requirement of many tensor calculations, which prolongs the training time. Future research will try to reduce the tensor calculations by capturing the key attention and ignoring the minor attention between the tokens.


## 5. Conclusion

Currently, the transformer-based models employ the multihead attention mechanism for NMT, which computes the attention scores between the tokens themselves and among the tokens in the same subspaces. However, language is complex, which contains multidimensional information such as lexical, syntactic, and semantic information, and there are relationships between different dimensions of information. Therefore, this paper proposes the interacting-head attention model, which boasts two advantages. On the one hand, our model confirms the attention relationship between different tokens in different subspaces and uses this relationship to improve translation performance. On the other hand, the model provides a new method for optimizing the maximum number of heads, which helps to prevent the low-rank bottleneck. Besides, a threshold was defined for the number of heads, aiming to avoid the exponential growth of training time. Under this premise, using our model can greatly improve the translation performance. In conclusion, experimental research in this paper argues that the interacting-head attention mechanism is significantly effective for NMT. Simultaneously, the experimental results show that there is a strong interaction in the different dimensions of information of all the tokens within a sequence.However, this model has 2 disadvantages. On the one hand, the attention scores between sequence tokens are different, and some even tend to be 0. Therefore, the attention relationship between tokens should not be a fully connected network, but a sparse network which also can reduce the time complexity of computing the attention matrix. On the other hand, considering the attention relationship between different tokens in different subspaces, it is necessary to perform lots of tensor inner product calculations, especially with more heads. As a result, the training and decoding times are extended to a certain extent. These defects of the proposed model will be addressed in future work.



---

*Source: 2998242-2022-06-21.xml*