Venue: NAACL 2021

# DA-Transformer: Distance-aware Transformer

Chuhan Wu\({}^{\dagger}\) Fangzhao Wu\({}^{\ddagger}\) Yongfeng Huang\({}^{\dagger}\)

\({}^{\dagger}\)Department of Electronic Engineering & BNRist, Tsinghua University, Beijing 100084, China

\({}^{\ddagger}\)Microsoft Research Asia, Beijing 100080, China

{wuchuhan15,wufangzhao}@gmail.com, yfhuang@tsinghua.edu.cn

###### Abstract

Transformer has achieved great success in the NLP field by composing various advanced models like BERT and GPT. However, Transformer and its existing variants may not be optimal in capturing token distances because the position or distance embeddings used by these methods usually cannot keep the precise information of real distances, which may not be beneficial for modeling the orders and relations of contexts. In this paper, we propose DA-Transformer, which is a distance-aware Transformer that can exploit the real distance. We propose to incorporate the real distances between tokens to re-scale the raw self-attention weights, which are computed by the relevance between attention query and key. Concretely, in different self-attention heads the relative distance between each pair of tokens is weighted by different learnable parameters, which control the different preferences on long- or short-term information of these heads. Since the raw weighted real distances may not be optimal for adjusting self-attention weights, we propose a learnable sigmoid function to map them into re-scaled coefficients that have proper ranges. We first clip the raw self-attention weights via the ReLU function to keep non-negativity and introduce sparsity, and then multiply them with the re-scaled coefficients to encode real distance information into self-attention. Extensive experiments on five benchmark datasets show that DA-Transformer can effectively improve the performance of many tasks and outperform the vanilla Transformer and its several variants.

+
Footnote †: _Correspondedings of the 2021 Conference of the North American Chapter of the Association for Computational Linguistics: Human Language Technologies_, pages 2059–2068 June 6-11, 2021. ©2021 Association for Computational Linguistics

## 1 Introduction

Transformer Vaswani et al. (2017) has achieved huge success in the NLP field in recent years Kobayashi et al. (2020). It serves as the basic architecture of various state-of-the-art models like BERT Devlin et al. (2019) and GPT Radford et al. (2019), and boosts the performance of many tasks like text generation Koncel-Kedziorski et al. (2019), machine translation Vaswani et al. (2017), and reading comprehension Xu et al. (2019). Thus, the improvement on the Transformer architecture would be beneficial for many NLP-related fields Wu et al. (2020).

A core component of Transformer is multi-head self-attention, which is responsible for modeling the relations between contexts Yang et al. (2019); Guo et al. (2019). However, self-attention is position-agnostic since it does not distinguish the orders of inputs. Thus, in the vanilla Transformer, position encoding is applied to the input to help Transformer capture position information. However, in contrast to recurrent and convolutional neural networks, it is difficult for vanilla Transformers to be aware of the token distances Shaw et al. (2018), which are usually important cues for context modeling. Thus, several works explored to incorporate token distance information into Transformer. For example, Shaw et al. (2018) proposed to combine the embeddings of relative positions with attention key and value in the self-attention network. They restricted the maximum relative distance to only keep the precise relative position information within a certain distance. Yan et al. (2019) proposed a variant of self-attention network for named entity recognition, which incorporates sinusoidal embeddings of relative position to compute attention weights in a direction- and distance-aware way. However, the distance or relative position embeddings used by these methods usually cannot keep the precise information of the real distance, which may not be beneficial for the Transformer to capture word orders and the context relations.

In this paper, we propose a distance-aware Transformer (DA-Transformer), which can explicitly exploit real token distance information to enhance context modeling by leveraging the relative distances between different tokens to re-scale the raw attention weights before softmax normalization.



More specifically, since global and local context modeling usually have different distance preferences, we propose to learn a different parameter in different attention heads to weight the token distances, which control the preferences of attention heads on long or short distances. In addition, since the weighted distances may not have been restricted to a proper range, we propose a learnable sigmoid function to map the weighted distances into re-scaled coefficients. They are further multiplied with the raw attention weights that are clipped by the ReLU function for keeping the non-negativity and introducing sparsity. We conduct extensive experiments on five benchmark datasets for different tasks, and the results demonstrate that our approach can effectively enhance the performance of Transformer and outperform its several variants with distance modeling.

The main contributions of this paper include:

* We propose a distance-aware Transformer that uses the real token distances to keep precise distance information in adjusting attention weights for accurate context modeling.
* We propose to use different parameters to weight real distances in different attention heads to control their diverse preferences on short-term or long-term information.
* We propose a learnable sigmoid function to map the weighted distances into re-scaled coefficients with proper ranges for better adjusting the attention weights.
* We conduct extensive experiments on five benchmark datasets and the results validate the effectiveness of our proposed method.

## 2 Related Work

### Transformer

To make this paper self-contained, we first briefly introduce the architecture of Transformer, which was initially introduced to the machine translation task Vaswani et al. (2017). It has become an important basic neural architecture of various state-of-the-art NLP models like BERT Devlin et al. (2019) and GPT Radford et al. (2019). The core component of Transformer is multi-head self-attention. It has \(h\) attention heads, where the parameters in each head are independent. For the \(i\)-th attention head, it takes a matrix \({\bf H}\) as the input. It first uses three independent parameter matrices \({\bf W}_{Q}^{(i)}\), \({\bf W}_{K}^{(i)}\), and \({\bf W}_{V}^{(i)}\) to respectively transform the input matrix \(H\) into the input query \({\bf Q}^{(i)}\), key \({\bf K}^{(i)}\) and value \({\bf V}^{(i)}\), which is formulated as follows:

\[{\bf Q}^{(i)},{\bf K}^{(i)},{\bf V}^{(i)}={\bf HW}_{Q}^{(i)},{\bf HW}_{K}^{(i) },{\bf HW}_{V}^{(i)}. \tag{1}\]

Then, it uses a scaled dot-product attention head to process its query, key and value, which is formulated as follows:

\[{\rm Attention}({\bf Q}^{(i)},{\bf K}^{(i)},{\bf V}^{(i)})={\rm softmax}( \frac{{\bf Q}^{(i)}{\bf K}^{(i)\top}}{\sqrt{d}}){\bf V}^{(i)}, \tag{2}\]

where \(d\) is the dimension of the vectors in the query and key. The outputs of the \(h\) attention heads are concatenated together and the final output is a linear projection of the concatenated representations, which is formulated as follows:

\[{\rm Multihead}({\bf Q},{\bf K},{\bf V})={\rm Concat}({\rm head}_{1},...,{ \rm head}_{b}){\bf W}_{O}, \tag{3}\] \[{\rm where~{}head}_{i}={\rm Attention}({\bf Q}^{(i)},{\bf K}^{( i)},{\bf V}^{(i)}),\]

where \({\bf W}_{O}\) is an output projection matrix. In the standard Transformer, a position-wise feed-forward neural network is further applied to the output of multi-head self-attention network. Its function is formulated as follows:

\[FFN({\bf x})=max(0,{\bf xW}_{1}+{\bf b}_{1}){\bf W}_{2}+{\bf b}_{2}, \tag{4}\]

where \({\bf W}_{1}\), \({\bf W}_{2}\), \({\bf b}_{1}\), \({\bf b}_{2}\) are kernel and bias parameters. Transformer also employs layer normalization Ba et al. (2016) and residual connection He et al. (2016) techniques after the multi-head self-attention and feed-forward neural networks, which are also kept in our method.

Since self-attention network does not distinguish the order and position of input tokens, Transformer adds the sinusoidal embeddings of positions to the input embeddings to capture position information. However, position embeddings may not be optimal for distance modeling in Transformer because distances cannot be precisely recovered from the dot-product between two position embeddings.

### Distance-aware Transformer

Instead of directly using the sinusoidal position embedding Vaswani et al. (2017) or the absolute position embedding Devlin et al. (2019), several variants of the Transformer explore to use the relative positions to better model the distance between contexts Shaw et al. (2018); Wang et al. (2019); Dai et al. (2019); Yan et al. (2019). For example, Shaw 

et al. (2018) proposed to add the embeddings of relative positions to the attention key and value to capture the relative distance between two tokens. They only kept the precise distance within a certain range by using a threshold to clip the maximum distance to help generalize to long sequences. Dai et al. (2019) proposed Transformer-XL, which uses another form of relative positional encodings that integrate content-dependent positional scores and a global positional score into the attention weights. Yan et al. (2019) proposed direction-aware sinusoidal relative position embeddings and used them in a similar way with Transformer-XL. In addition, they proposed to use the un-scaled attention to better fit the NER task. However, relative position embeddings may not be optimal for modeling distance information because they usually cannot keep the precise information of real token distances. Different from these methods, we propose to directly re-scale the attention weights based on the mapped relative distances instead of using sinusoidal position embeddings, which can explicitly encode real distance information to achieve more accurate distance modeling.

## 3 DA-Transformer

In this section, we introduce our proposed **d**istanceware Transformer (DA-Transformer) approach, which can effectively exploit real token distance information to enhance context modeling. It uses a learnable parameter to weight the real distances between tokens in each attention head, and uses a learnable sigmoid function to map the weighted distances into re-scaled coefficients with proper ranges, which are further used to adjust the raw attention weights before softmax normalization. The details of DA-Transformer are introduced in the following sections.

### Head-wise Distance Weighting

Similar with the standard Transformer, the input of our model is also a matrix that contains the representation of each token, which is denoted as \({\bf H}=[{\bf h}_{1},{\bf h}_{2},...,{\bf h}_{N}]\), where \(N\) is the length of the sequence. We denote the real relative distance between the \(i\)-th and \(j\)-th positions as \(R_{i,j}\), which is computed by \(R_{i,j}=|i-j|\). We can then obtain the relative distance matrix \({\bf R}\in\mathbb{R}^{N\times N}\) that describes the relative distance between each pair of positions. In each attention head, we use a learnable parameter \(w_{i}\) to weight the relative distance by \({\bf R}^{(i)}=w_{i}{\bf R}\), which will be further used to adjust the self-attention weights. In our method, we stipulate that a more positive \({\bf R}^{(i)}\) will amplify the attention weights more strongly while a more negative \({\bf R}^{(i)}\) will diminish them more intensively. Thus, a positive \(w_{i}\) means that this attention head prefers to capture long-distance information, while a negative \(w_{i}\) means that it focuses more on local contexts. By learning different values of \(w_{i}\), different attention heads may have different preferences on capturing either short-term or long-term contextual information with different intensity.

### Weighted Distance Mapping

Since the raw weighted distances may not be in the proper range for adjusting the attention weights, we need to map them into the re-scaled coefficients via a function \(\hat{\bf R}^{(i)}=f({\bf R}^{(i)})\) that is suitable for adjusting the self-attention weights. However, it is not a trivial task to design the function \(f(\cdot)\) because it needs to satisfy the following requirements: (1) \(f(0)=1\). We stipulate that zero distances do not influence the self-attention weights. (2) The value of \(f({\bf R}^{(i)})\) should be zero when \({\bf R}^{(i)}\rightarrow-\infty\). This requirement is to guarantee that if an attention head prefers to capture local information (\(w_{i}<0\)), the long-distance information should be surpassed.1 (3) The value of \(f({\bf R}^{(i)})\) should be limited when \({\bf R}^{(i)}\rightarrow+\infty\). This requirement is to ensure that the model is able to process long sequences without over-emphasize distant contexts. (4) The scale of \(f(\cdot)\) needs to be tunable. This aims to help the model better adjust the intensity of distance information. (5) The function \(f(\cdot)\) needs to be mono

Figure 1: The curves of our learnable sigmoid function under different \(v_{i}\).

 

tone. To satisfy the five requirements above, we propose a learnable sigmoid function to map the weighted relative distances \({\bf R}^{(i)}\), which is formulated as follows:

\[f({\bf R}^{(i)};v_{i})=\frac{1+\exp(v_{i})}{1+\exp(v_{i}-{\bf R}^{( i)})}, \tag{5}\]

where \(v_{i}\) is a learnable parameter in this head that controls the upperbound and ascending steepness of this function. The curves of our learnable sigmoid function under several different values of \(v_{i}\) are plotted in Fig. 1. We can see that the proposed function satisfies all the requirements above. In addition, from this figure we find that if \(v_{i}\) is larger, the upperbound of the curve is higher, which means that distance information is more intensive. When \(v_{i}=0\), it is in fact identical to the standard sigmoid function except for the scaling factor of 2. By mapping the weighted distances \({\bf R}^{(i)}\) via the function \(f(\cdot)\), we can obtain the final re-scaled coefficients \(\hat{\bf R}^{(i)}\) in a learnable way. Several illustrative examples of the re-scaled coefficients under \(w_{i}=\pm 1\) and \(v_{i}=\pm 1\) are respectively shown in Figs. 2(a)-2(d). We can see that if \(w_{i}\) is positive, long-distance contexts are preferred while short-term contexts are surpassed. The situation is reversed if \(w_{i}\) turns to negative. In addition, the coefficients in Fig. 2(c) have larger dynamic ranges than the coefficients in Fig. 2(a), indicating that long-distance information is more dominant in Fig. 2(c). Moreover, the coefficients in Fig. 2(d) are "sharper" than those in Fig. 2(b), which indicates that the model tends to capture shorter distances.

### Attention Adjustment

Then, we use the re-scaled coefficients to adjust the raw attention weights that are computed by the dot-product between the query and key, i.e., \(\frac{{\bf Q}^{(i)}{\bf K}^{(i)\top}}{\sqrt{d}}\). Different from existing methods that add the query-key dot-product with position or distance representations, in our approach we propose to multiply the re-scaled coefficients with the query-key dot-product. This is because for the tokens whose relations are very weak, if their re-scaled coefficients are large, their final attention weights will be over-amplified if we simply add the re-scaled coefficients to their raw attention weights. This is not optimal for modeling contextual information because the attention weights of irrelevant contexts cannot be fully surpassed. However, there are also some problems if we directly multiply the re-scaled coefficients \(\mathbf{\hat{R}}^{(i)}\) and the raw attention weights \(\underline{{\bf Q}^{(i)}{\bf K}^{(i)\top}}\). This is because the sign of attention weights \(\underline{{\bf Q}^{(i)}{\bf K}^{(i)\top}}\over\sqrt{d}\) is indefinite and the multiplied results cannot accurately reflect the influence of distance information. Thus, we propose to add a ReLU (Glorot et al., 2011) activation function to the raw attention weights to keep non-negativity. In this way, the final output \({\bf O}^{(i)}\) of an attention head can be formulated as follows:

\[{\bf O}^{(i)}=\mathrm{softmax}(\frac{\mathrm{ReLU}({\bf Q}^{(i)}{\bf K}^{(i) \top})*\hat{\bf R}^{(i)}}{\sqrt{d}}){\bf V}^{(i)}, \tag{6}\]

where \(*\) represents element-wise product. The ReLU function can also introduce sparsity to the self-attention because only the positive attention weights can be amplified by the re-scaled coefficients, which makes the attention weights in our method sharper. We concatenate the output from the \(h\) independent attention heads, and project it into a unified output. In addition, we keep the same layer normalization and residual connection strategy as the standard Transformer.

### Computational Complexity Analysis

Compared with the standard Transformer, the major additional time cost is brought by computing the re-scaled coefficients \(\mathbf{\hat{R}}^{(i)}\) and using them to adjust the attention weights. The theoretical time complexity of the two operations in each head is \(O(N^{2})\), which is much smaller than the time complexity of computing the attention weights, i.e., \(O(N^{2}\times d)\). In addition, both Eq. (5) and Eq. (6) in our approach can be computed in a vectorized

Figure 2: The re-scaled coefficient matrices under different values of \(w_{i}\) and \(v_{i}\). Dark regions indicate that the corresponding attention weights are promoted.

 

manner. Thus, the additional time consumption of our method is very light. Besides, the increase of parameters is also minimal because we only introduce \(2h\) additional parameters, which are usually ignorable compared with the projection matrices like \({\bf W}_{Q}^{(i)}\). Thus, our approach inherits the efficiency of the Transformer architecture.

## 4 Experiments

### Datasets and Experimental Settings

Our experiments are conducted on five benchmark datasets for different tasks. Four of them are benchmark NLP datasets. The first one is AG's News2 (denoted as _AG_), which is a news topic classification dataset. The second one is Amazon Electronics He and McAuley (2016) (denoted as _Amazon_), which is a dataset for review rating prediction. The third one is Stanford Sentiment Treebank Socher et al. (2013) (denoted as _SST_). We use the binary classification version of this dataset. The fourth one is Stanford Natural Language Inference Bowman et al. (2015) (_SNLI_) dataset, which is a widely used natural language inference dataset. The detailed statistics of these datasets are summarized in Table 1. In addition, we also conduct experiments on a benchmark news recommendation dataset named MIND Wu et al. (2020c), aiming to validate the effectiveness of our approach in both text and user modeling. It contains the news impression logs of 1 million users from Microsoft News3 from October 12 to November 22, 2019. The training set contains the logs in the first five weeks except those on the last day which are used for validation. The rest logs are used for test. The key statistics of this dataset are summarized in Table 2.

Footnote 2: [https://www.di.unipi.it/en/](https://www.di.unipi.it/en/)

Footnote 3: [https://www.msn.com/en-us](https://www.msn.com/en-us)

In our experiments, we use the 300-dimensional Glove Pennington et al. (2014) embeddings for word embedding initialization.4 The number of attention head is 16, and the output dimension of each attention is 16. We use one Transformer layer in all experiments. On the _AG_, _SST_ and _SNLI_ datasets, we directly apply Transformer-based methods to the sentences. On the _Amazon_ dataset, since reviews are usually long documents, we use Transformers in a hierarchical way by learning sentence representations from words via a word-level Transformer first and then learning document representations from sentences via a sentence-level Transformer. On the _MIND_ dataset, following Wu et al. (2019, 2020b) we also use a hierarchical model architecture that first learns representations of historical clicked news and candidate news from their titles with a word-level Transformer, then learns user representations from the representations of clicked news with a news-level Transformer, and final matches user and candidate news representations to compute click scores.5 We use the same model training strategy with negative sampling techniques as NRMS Wu et al. (2019). On all datasets we use Adam Kingma and Ba (2015) as the optimization algorithm and the learning rate is 1e-3. On the _AG_, _Amazon_, _SST_ and _SNLI_ datasets, accuracy and macro-Fscore are used as the performance metric. On the _MIND_ dataset, following Wu et al. (2019) we use the average AUC, MRR, nDCG@5 and nDCG@10 scores of all sessions as the metrics. Each experiment is repeated 5 times independently and the average results with standard deviations are reported.

Footnote 4: We do not use contextualized embeddings generated by language models like BERT because we mainly focus on validating the effectiveness of our Transformer architecture.

Footnote 5: Both the word-level and news-level Transformers contain one self-attention layer.

### Performance Evaluation

We compare our proposed _DA-Transformer_ method with several baseline methods, including: (1) _Transformer_Vaswani et al. (2017), the vanilla Transformer architecture, where sinusoidal positional embeddings are used. (2) _Transformer-RPR_Shaw et al. (2018), a variant of Transformer with relative position representations. (3) _Transformer-XL_Dai et al. (2019), a variant of Transformer that consists of a segment-level recurrence mechanism and a sinusoidal relative position encoding scheme. (4) _Adapted Transformer_Yan et al. (2019), a variant

\begin{table}
\begin{tabular}{l r r r r r} \hline
**Dataset** & **\# Train** & **\# Dev.** & **\# Test** & **\# Classes** & **Avg. len.** \\ \hline AG & 108k & 12k & 7.6k & 4 & 44 \\ Amazon & 40k & 5k & 5k & 5 & 133 \\ SST & 8k & 1k & 2k & 2 & 19 \\ SNLI & 55k & 10k & 10k & 2 & 22 \\ \hline \end{tabular}
\end{table}
Table 1: Statistics of _AG_, _Amazon_, _SST_ and _SNLI_ datasets.

\begin{table}
\begin{tabular}{l c l r} \hline \# Users & 1,000,000 & **Avg. title len.** & 11.52 \\
**\# News** & 161,013 & **\# Click samples** & 5,597,979 \\
**\# Impressions** & 500,000 & **\# Non-click samples** & 136,162,621 \\ \hline \end{tabular}
\end{table}
Table 2: Statistics of the _MIND_ dataset.

 

of Transformer that uses direction- and distance-aware position encoding. The results of our approach and these methods on the five datasets are respectively shown in Tables 4 and 5. From the results, we have several observations.

First, compared with the vanilla Transformer, the compared methods that consider distance information consistently achieve better performance. It shows that distance information is very important in context modeling. Second, among the methods with distance information, the performance of _Transformer-RPR_ is lower than the others. This may be because _Transformer-RPR_ does not keep the precise long-distance information. Third, by comparing _Transformer-XL_ and _Adapted Transformer_, we find that the performance of _Adapted Transformer_ is better on the SST dataset, while _Transformer-XL_ is better on other datasets. This is probably because _Adapted Transformer_ is more suitable for modeling local contexts and the sentences in the _SST_ dataset are usually short, while _Transformer-XL_ may be more appropriate for modeling long sequences. Fourth, our method consistently achieves better performance on the five datasets, and its improvement over the second best method is statistically significant (t-test p<0.05). This is because our method can explicitly encode real distance information rather than using positional encoding, making the modeling of distance more accurate.

We further compare the performance of different methods in a rating regression task on the _Amazon_ dataset. The results are shown in Fig. 3. From Fig. 3 we observe similar patterns with the results in classification tasks, which validate the generality of our DA-Transformer in different genres of tasks.

### Influence of Different Mapping Functions

Next, we study the influence of using different mapping functions \(f(\cdot)\) for computing the re-scaled coefficients. We compare the performance of our method w.r.t. several different \(f(\cdot)\), including: (1)

\begin{table}
\begin{tabular}{l c c c c} \hline \multirow{2}{*}{**Methods**} & \multicolumn{2}{c}{**SST**} & \multicolumn{2}{c}{**SNLI**} \\ \cline{2-6}  & Accuracy & Macro-F & Accuracy & Macro-F \\ \hline Transformer & 89.67\(\pm\)0.22 & 89.59\(\pm\)0.24 & 81.45\(\pm\)0.30 & 81.42\(\pm\)0.31 \\ Transformer-RPR & 89.94\(\pm\)0.19 & 89.90\(\pm\)0.20 & 82.20\(\pm\)0.31 & 82.18\(\pm\)0.31 \\ Transformer-XL & 90.06\(\pm\)0.20 & 90.02\(\pm\)0.21 & 83.19\(\pm\)0.29 & 83.15\(\pm\)0.30 \\ Adapted Transformer & 90.15\(\pm\)0.19 & 90.10\(\pm\)0.1 & 82.35\(\pm\)0.28 & 82.31\(\pm\)0.30 \\ \hline \multicolumn{6}{l}{*DA-Transformer} & **90.49\(\pm\)**0.17 & **90.43\(\pm\)**0.19 & **84.18\(\pm\)**0.27 & **84.16\(\pm\)**0.29 \\ \hline \end{tabular}
\end{table}
Table 4: Results on _SST_ and _SNLI_. *Improvement over the underlined second best results is significant at \(p<0.05\).

\begin{table}
\begin{tabular}{l c c c c} \hline \multirow{2}{*}{**Methods**} & \multicolumn{2}{c}{**AG**} & \multicolumn{2}{c}{**Amazon**} \\ \cline{2-5}  & Accuracy & Macro-F & Accuracy & Macro-F \\ \hline Transformer & 93.01\(\pm\)0.13 & 93.00\(\pm\)0.13 & 65.15\(\pm\)0.40 & 42.14\(\pm\)0.41 \\ Transformer-RPR & 93.14\(\pm\)0.12 & 93.13\(\pm\)0.13 & 65.29\(\pm\)0.38 & 42.40\(\pm\)0.40 \\ Transformer-XL & 93.35\(\pm\)0.10 & 93.34\(\pm\)0.11 & 65.50\(\pm\)0.40 & 42.88\(\pm\)0.43 \\ Adapted Transformer & 93.28\(\pm\)0.13 & 93.27\(\pm\)0.14 & 65.47\(\pm\)0.39 & 42.69\(\pm\)0.42 \\ \hline \multicolumn{6}{l}{*DA-Transformer} & **93.72\(\pm\)**0.11 & **93.70\(\pm\)**0.12 & **66.38\(\pm\)**0.39 & **44.29\(\pm\)**0.40 \\ \hline \end{tabular}
\end{table}
Table 3: Results on _AG_ and _Amazon_. *Improvement over the underlined second best results is significant at \(p<0.05\).

\begin{table}
\begin{tabular}{l c c c c} \hline \multirow{2}{*}{**Methods**} & AUC & MRR & nDCG@5 & nDCG@10 \\ \hline Transformer & 67.76\(\pm\)0.18 & 33.05\(\pm\)0.16 & 35.94\(\pm\)0.19 & 41.63\(\pm\)0.20 \\ Transformer-RPR & 67.81\(\pm\)0.16 & 33.10\(\pm\)0.17 & 35.98\(\pm\)0.20 & 41.65\(\pm\)0.21 \\ Transformer-XL & 67.92\(\pm\)0.16 & 33.15\(\pm\)0.16 & 36.04\(\pm\)0.20 & 41.70\(\pm\)0.19 \\ Adapted Transformer & 67.70\(\pm\)0.22 & 33.01\(\pm\)0.20 & 35.89\(\pm\)0.17 & 41.58\(\pm\)0.23 \\ \hline \multicolumn{6}{l}{*DA-Transformer} & **68.32\(\pm\)**0.15 & **33.36\(\pm\)**0.16 & **36.34\(\pm\)**0.14 & **42.07\(\pm\)**0.17 \\ \hline \end{tabular}
\end{table}
Table 5: Results on the _MIND_ dataset. *Improvement over the underlined second best results is significant at \(p<0.05\).

 

\(f(x)=min(x,T)\) (clip), using a threshold \(T\) to clip the weighted distance; (2) \(f(x)=k_{i}x+b_{i}\) (linear), using a linear transformation to the weighted distance; (3) \(f(x)=\exp(x)\) (exponent), using an exponent function to map the weighted distance; (4) \(f(x)=\frac{1}{1+\exp(-x)}\) (sigmoid), using the sigmoid function to activate the weighted distance; and (5) \(f(x;v_{i})=\frac{1+\exp(v_{i})}{1+\exp(v_{i}-x)}\), our learnable sigmoid function. Due to space limitation, we only present the results on the _AG_, _Amazon_ and _MIND_ datasets in Fig. 4. From these results, we find that clip is not optimal for mapping the weighted distance. This is because it cannot keep the precise distance information beyond a certain range. In addition, simply using the linear transformation is also insufficient. This may be because our attention adjustment method requires \(f(\cdot)\) to be positive, but linear transformation cannot guarantee. Besides, we find that the sigmoid function and our proposed function are better than the exponential function. This may be because long sequences will lead to the problem of exponent explosion, which is harmful to context modeling. Moreover, our proposed learnable sigmoid function is better than the standard sigmoid function. It shows that adjusting the activation function in a learnable way can better map the raw distances into re-scaled coefficients.

### Influence of Different Attention Adjusting Methods

Then, we explore the influence of different methods for adjusting the raw attention weights. We consider four different kinds of methods, including: (1) adding the re-scaled coefficients to the attention weights normalized by softmax (late add); (2) multiplying the re-scaled coefficients with the attention weights normalized by softmax (late multiply); (3) adding the re-scaled coefficients to the raw attention weights before normalization (early add), which is widely used in existing methods like _Transformer-XL_; (4) multiplying the re-scaled coefficients with the raw attention weights activated by ReLU, which is the method used in our approach (early multiply). The results on the _AG_, _Amazon_ and _MIND_ datasets are shown in Fig. 5. According to these results, we find that early adjustment is better than late adjustment. This may be because the late adjustment methods will change the total amount of attention, which may not be optimal. In addition, we find that multiplying is better than adding for both early and late adjustment. This may be because adding large re-scaled coefficients may over-amplify some attention weights. For example, if a raw attention weight is relatively small, it is not suitable to add large re-scaled coefficients to it because the corresponding contexts may not have close relations. In contrast, multiplying the re-scaled coefficients will not over-amplify the low attention weights. Moreover, in our early multiply method we further propose to use the ReLU function to introduce sparsity to make the Transformer more "focused". Thus, our method is better than the existing early add method in adjusting the attention weights.

### Model Interpretation

Finally, we interpret our proposed method by visualizing its key parameters and the attention weights. we first visualize the parameters \(w_{i}\) and \(v_{i}\) in our method, which control the preferences of attention heads on long-term or short-term information and the shape of the learnable sigmoid function, respectively. The visualization results on the _AG_ and _MIND_ datasets are respectively shown in Figs. 6 and 7.6 From Fig. 6, we find it is very interesting that half of the parameters \(w_{i}\) are positive and the rest of them are negative. It indicates that half of the attention heads mainly aim to capture local contexts, while the rest ones are responsible for modeling long-distance contexts. It may be because both short-term and long-term contexts are useful for understanding news topics. In addition, we find that most attention heads have negative \(v_{i}\) while the rest are positive. It shows that on the _AG_ dataset the intensity of attention adjustment is mild in most attention heads. From Fig. 7(a), we find long-term information is somewhat more important than local information in modeling news texts for

Figure 3: Performance comparison of rating regression on _Amazon_. Lower scores indicate better performance.

 

news recommendation. However, from Fig. 7(b) we find an interesting phenomenon that only one head has a strong negative \(w_{i}\) while the values of \(w_{i}\) in all the rest heads are positive. It means that only one attention head tends to capture short-term user interests while all the other heads prefer to capture long-term user interests. This is intuitive because users usually tend not to intensively click very similar news and their long-term interests may have more decisive influence on their news clicks. In addition, we find it is interesting that on _MIND_ all values of \(v_{i}\) are positive. It may indicate that distance information has a strong impact on the attention weights. These visualization results show that DA-Transformer can flexibly adjust its preference on short-term or long-term information and the intensity of attention adjustment by learning different values of \(w_{i}\) and \(v_{i}\) according to the task characteristics.7

Footnote 7: We do not observe significant correlations between the sequence length and the signs of \(w_{i}\). This may indicate that the values of \(w_{i}\) depend more on the task characteristics rather than text lengths.

We then visualize the attention weights produced by the vanilla Transformer and the distance-aware attention weights in our DA-Transformer method. The attention weights of a sentence in the _AG_ dataset computed by four different attention heads are respectively shown in Figs. 8(a) and 8(b). From Fig. 8(a), we find it is difficult to interpret the self-attention weights because they are too "soft". In addition, it is difficult for us to understand the dif

Figure 4: Influence of using different mapping functions.

Figure 5: Influence of using different attention adjusting methods.

Figure 6: The weights learned by different attention heads on the _AG_ dataset.

Figure 7: The distance weights learned by different attention heads on the _MIND_ dataset.

 

ferences between the information captured by different attention heads. Different from the vanilla Transformer, from Fig. 8(b) we find that the attention weights obtained by our method are more sparse, indicating that the attention mechanism in our method is more focused. In addition, it is easier for us to interpret the results by observing the attention heatmap. For example, the first two heatmaps in Fig. 8(b) are produced by the two attention heads with preferences on short-term contexts. We can see that they mainly capture the relations among local contexts, such as the relations between "biotech" and "sector". Differently, in the latter two heatmaps obtained by the two attention heads that prefer long-term contexts, we can observe that the model tends to capture the relations between a word (e.g., "biotech") with the global contexts. These results show that different attention heads in our method are responsible for capturing different kinds of information, and their differences can be directly observed from the self-attention weights. Thus, our method can be better interpreted than vanilla Transformers.

## 5 Conclusion

In this paper, we propose a distance-aware Transformer, which can leverage the real distance between contexts to adjust the self-attention weights for better context modeling. We propose to first use different learnable parameters in different attention heads to weight the real relative distance between tokens. Then, we propose a learnable sigmoid function to map the weighted distances into re-scaled coefficients with proper ranges. They are further multiplied with the raw attention weights that are activated by the ReLU function to keep non-negativity and produce sharper attention. Extensive experiments on five benchmark datasets show that our approach can effectively improve the performance of Transformer by introducing real distance information to facilitate context modeling.

#### Acknowledgments

This work was supported by the National Natural Science Foundation of China under Grant numbers U1936208 and U1936216.

Figure 8: The self-attention weights learned by the vanilla Transformer and our proposed DA-Transformer method.

 



## References

* [1]J. Lei Ba, J. R. Kiros, and G. E. Hinton (2016) Layer normalization. arXiv preprint arXiv:1607.06450. Cited by: SS3.1.
* S. Bowman, G. Angeli, C. Potts, and C. D Manning (2015) A large annotated corpus for learning natural language inference. In EMNLP, pp. 632-642. Cited by: SS1.
* Z. Dai, Z. Yang, Y. Yang, J. G. Carbonell, Q. Le, and R. Salakhutdinov (2019) Transformer-xl: attentive language models beyond a fixed-length context. In ACL, pp. 2978-2988. Cited by: SS1.
* J. Devlin, M. Chang, K. Lee, and K. Toutanova (2019) Bert: pre-training of deep bidirectional transformers for language understanding. In NAACL-HLT, pp. 4171-4186. Cited by: SS1.
* X. Glorot, A. Bordes, and Y. Bengio (2011) Deep sparse rectifier neural networks. In AISTATS, pp. 315-323. Cited by: SS1.
* Q. Guo, X. Qiu, P. Liu, Y. Shao, X. Xue, and Z. Zhang (2019) Star transformer. In NAACL-HLT, pp. 1315-1325. Cited by: SS1.
* K. He, X. Zhang, S. Ren, and J. Sun (2016) Deep residual learning for image recognition. In CVPR, pp. 770-778. Cited by: SS1.
* R. He and J. McAuley (2016) Ups and downs: modeling the visual evolution of fashion trends with one-class collaborative filtering. In WWW, pp. 507-517. Cited by: SS1.
* D. P. Kingma and J. Ba (2015) Adam: a method for stochastic optimization. In ICLR, Cited by: SS1, SS2.1, SS3.1, SS3.