SlowGuess/NAACL_2021_OCR huggingface dataset:
Hanjie Chen $^{1}$  Song Feng $^{2}$  Jatin Ganhotra $^{2}$  Hui Wan $^{2}$  Chulaka Gunasekara $^{2}$  Sachindra Joshi $^{2}$  Yangfeng Ji $^{1}$
$^{1}$ Department of Computer Science, University of Virginia, Charlottesville, VA, USA  $^{2}$ IBM Research AI
{hc9mx, yangfeng}@virginia.edu
{sfeng, jatinganhotra, hwan}@us.ibm.com
{Chulaka.Gunasekara@, jsachind@in.}ibm.com
Explaining neural network models is important for increasing their trustworthiness in real-world applications. Most existing methods generate post-hoc explanations for neural network models by identifying individual feature attributions or detecting interactions between adjacent features. However, for models with text pairs as inputs (e.g., paraphrase identification), existing methods are not sufficient to capture feature interactions between two texts and their simple extension of computing all word-pair interactions between two texts is computationally inefficient. In this work, we propose the Group Mask (GMASK) method to implicitly detect word correlations by grouping correlated words from the input text pair together and measure their contribution to the corresponding NLP tasks as a whole. The proposed method is evaluated with two different model architectures (decomposable attention model and BERT) across four datasets, including natural language inference and paraphrase identification tasks. Experiments show the effectiveness of GMASK in providing faithful explanations to these models<sup>1</sup>.
Explaining deep neural networks is critical for revealing their prediction behaviors and enhancing the trustworthiness of applying them in real-world applications. Many methods have been proposed to explain neural network models from the posthoc manner that generates faithful explanations based on model predictions (Ribeiro et al., 2016; Lundberg and Lee, 2017; Sundararajan et al., 2017; Guidotti et al., 2018). Most existing work focuses on identifying word attributions (Roektaschel et al., 2015; Li et al., 2016; Thorne et al., 2019) for NLP tasks. Knowing which individual features are important might not be enough for explaining model
Figure 1: An illustration of obtaining individual word attributions (Indiv. Attr.) and weighted word attributions (Weighted Attr.), where the color of each block represents word importance or group importance, and the color saturation of purple lines indicates the probability of a word belonging to a specific group.
behaviors. Then, other recent work exploits feature interactions as explanations (Singh et al., 2018; Chen et al., 2020; Tsang et al., 2020). However, they could suffer computation inefficiency while computing interactions between all word pairs, and they also fall short for identifying multiple important words correlated from different input sources for predictions. Such intuitions are particularly important for explaining sentence pair modeling tasks such as natural language inference (NLI) (Bowman et al., 2015) and paraphrase identification (PI) (Yin and Schütze, 2015).
Figure 1 shows an example of NLI, where the model makes correct prediction as CONTRADIC-TION. The first column visualizes individual word attributions to the prediction, where the top four important words are man, banjo, guitar, a. However, the correlations between them are unclear and intuitively man and a are irrelevant to the model prediction, which makes the explanation
$^{1}$ Code for this paper is available at https://github.com/UVa-NLP/GMASK
Proceedings of the 2021 Conference of the North American Chapter of the Association for Computational Linguistics: Human Language Technologies, pages 3917-3930 June 6-11, 2021. ©2021 Association for Computational Linguistics
untrustworthy. A good explanation should be able to capture correlated words between the sentence pair, and identify their importance to the model prediction.
In this work, we propose Group Masks (GMASK), a model-agnostic approach that considers the importance of correlated words from two input sentences. In particular, it distributes correlated words into a group and learns the group importance. In Figure 1, the input words are distributed in four groups with importance  $G2 > G1 > G3 > G4$ . The color saturation of purple lines represents the probability of a word belonging to a group. Different from individual word attributions, GMASK assigns electric, guitar, and banjo into important groups ( $G2 / G1$ ), while man and a into unimportant groups ( $G3 / G4$ ). The weighted word attributions computed as the weighted sum of group importance identify the important words electric, guitar from  $x_{1}$  and banjo from  $x_{2}$ , which explains the model prediction.
The contribution of this work is three-fold: (1) we introduce GMASK method to explain sentence pair modeling tasks by learning weighted word attributions based on word correlations; (2) we propose a sampling-based method to solve the optimization objective of GMASK; and (3) we evaluate the proposed method with two types neural network models (decomposable attention model (Parikh et al., 2016) and BERT (Devlin et al., 2018)), for two types of sentence pair modeling tasks on four datasets. Experiments show the superiority of GMASK in generating faithful explanations compared to other competitive methods.
Many approaches have been proposed to explain deep neural networks from the post-hoc manner, such as gradient-based explanation methods (Hechtlinger, 2016; Sundararajan et al., 2017), attention-based methods (Ghaeini et al., 2018; Serrano and Smith, 2019), and decomposition-based methods (Murdoch et al., 2018; Du et al., 2019). However these white-box explanation methods are either rendering doubt regarding faithfulness (Jain and Wallace, 2019; Wiegreffe and Pinter, 2019) or being limited to specific neural networks. In this work, we mainly focus on model-agnostic explanation methods, which are applicable to any black-box models.
Feature attributions Many approaches explain models by assigning feature attributions to model predictions. For example, perturbation-based methods quantify feature attributions by erasing features (Li et al., 2016) or using local linear approximation as LIME (Ribeiro et al., 2016). KernelSHAP (Lundberg and Lee, 2017) utilized Shapley values (Shapley, 1953) to compute feature attributions. Another line of work proposed learning feature attributions, such as L2X (Chen et al., 2018) which maximizes mutual information to recognize important features, and IBA (Schulz et al., 2020) which identifies feature attributions by optimizing the information bottleneck (Tishby et al., 2000). However, these approaches produce individual word attributions without considering feature correlations. Our proposed method implicitly detects correlated words and generates weighted word attributions.
Feature interactions Some work proposed to generate explanations beyond word-level features by detecting feature interactions. Murdoch et al. (2018) proposed contextual decomposition (CD) to compute word interactions and Singh et al. (2018) and Jin et al. (2019) further proposed hierarchical versions based on that. Other work adopted Shapley interaction index to compute feature interactions (Lundberg et al., 2018) and build hierarchical explanations (Chen et al., 2020). However, computing feature interactions between all word pairs is computationally inefficient (Tsang et al., 2018). Methods which only consider the interactions between adjacent features are not applicable to sentence pair modeling tasks as critical interactions usually form between words from different sentences. GMASK distributes correlated words from the input text pair into a group, and learns the group importance, without explicitly detecting feature interactions between all word pairs.
Word masks Some related work utilized word masks to select important features for building interpretable neural networks (Lei et al., 2016; Bastings et al., 2019) or improving the interpretability of existing models (Chen and Ji, 2020). De Cao et al. (2020) proposed to track the information flow of input features through the layers of BERT models. Different from the prior work, GMASK applies masks on a group of correlated words.
Figure 2: The left part shows that masks are applied on the word embedding layer, selecting important words for the neural network model. The outputs  $y$  and  $\tilde{y}$  are corresponding to the original input  $\boldsymbol{x} = [\boldsymbol{x}_1, \boldsymbol{x}_2]^T$  and masked input  $\tilde{\boldsymbol{x}} = [\tilde{\boldsymbol{x}}_1, \tilde{\boldsymbol{x}}_2]^T$  respectively. The right part shows the sampling process of GMASK. For  $Z$ , the color saturation of purple blocks represents the probability of a word belonging to a specific group (i.e.  $\phi_{i,j}(\iota)$ ).  $z$  is a sample of  $Z$  with binary values. For  $G$ , the color of each block represents group importance.  $g$  is a one-hot vector sampled from  $G$ , indicating which group being selected.  $w$  is a sample of word masks obtained by multiplying  $z$  and  $g$ .
This section introduces the proposed GMASK method. GMASK implicitly learns word correlations, and distributes correlated words from different input sentences into a group. GMASK learns the importance of each group by randomly masking out groups of words. Finally, the weighted word attributions are computed based on word group distributions and group importance.
3.1 Explaining Models with Word Masks
As the left part of Figure 2 shows, the word masks are applied on input word embeddings, learning to select important words to explain the model prediction. For each input data, we generate a post-hoc explanation by learning a set of mask values which represent the word attributions.
For sentence pair modeling tasks, the input contains two sentences  $\boldsymbol{x}_1 = [\boldsymbol{x}_{1,1}^T, \dots, \boldsymbol{x}_{1,n_1}^T]^T$  and  $\boldsymbol{x}_2 = [\boldsymbol{x}_{2,1}^T, \dots, \boldsymbol{x}_{2,n_2}^T]^T$ , where  $\boldsymbol{x}_{i,j} \in \mathbb{R}^d$  ( $i \in \{1,2\}$ ,  $j \in \{1,\dots,n_i\}$ ) represents the word embedding and  $n_1$  and  $n_2$  are the number of words in the two texts respectively. We denote the neural network model as  $f(\cdot)$  which takes  $\boldsymbol{x}_1$  and  $\boldsymbol{x}_2$  as input and outputs a prediction label  $y = f(\boldsymbol{x})$ , where  $\boldsymbol{x} = [\boldsymbol{x}_1, \boldsymbol{x}_2]^T$ . To explain the model prediction, we learn a set of word masks  $\boldsymbol{W} = [W_{1,1}, \dots, W_{1,n_1}, W_{2,1}, \dots, W_{2,n_2}]^T$  to identify important words by multiplying the masks with input word embeddings,
$$
\tilde {\boldsymbol {x}} = \boldsymbol {W} \odot \boldsymbol {x}, \tag {1}
$$
where  $\odot$  is an element-wise multiplication, the masked input  $\tilde{\pmb{x}} = [\tilde{\pmb{x}}_1,\tilde{\pmb{x}}_2]^T$ ,  $\tilde{\pmb{x}}_{i,j} = W_{i,j}\cdot \pmb{x}_{i,j}$ $(i\in \{1,2\}$ ,  $j\in \{1,\dots ,n_i\})$ , and  $W_{i,j}\in \{0,1\}$
is a binary random variable with 1 and 0 indicating to select or mask out the word  $x_{i,j}$  respectively. To generate an effective explanation, the word masks  $W$  should have the following properties: (1) correctly selecting important words for the model prediction; (2) removing as many irrelevant words as possible to keep the explanation concise; (3) selecting or masking out correlated words together from the sentence pair.
Previous work on learning individual word masks only focuses on the first two properties (Chen and Ji, 2020; De Cao et al., 2020). To satisfy the third property, We propose GMASK to implicitly detect word correlations and distribute the correlated words into a group (e.g. electric, guitar, and banjo are assigned to  $G1$  or  $G2$  in Figure 1), and learn a group mask for these words. Specifically, we decompose each  $W_{i,j}$  in  $\pmb{W}$  into two random variables,
$$
W _ {i, j} = \sum_ {\iota = 1} ^ {t} \delta \left(Z _ {i, j}, \iota\right) \delta (G, \iota), \tag {2}
$$
where  $t$  is the predefined number of groups, and we will introduce how to pick up a  $t$  in subsection 3.3.  $Z_{i,j} \in \{1,\dots ,t\}$  indicates the word  $x_{i,j}$  belonging to which group, and  $G \in \{1,\ldots ,t\}$  indicates which group takes the mask value 1, which means all words in this group are selected as important words, while other words in the rest groups are masked out.  $\delta (a,b)$  is the Delta function with  $\delta (a,b) = 1$  when  $a = b$ , and 0 otherwise. The conditional dependency of  $W$ ,  $Z$ , and  $G$  can be represented as a graphical model. The problem of learning  $W$  is equivalent to learning  $Z$  and  $G$ , that
${}^{2}\mathbf{Z} \rightarrow  \mathbf{W} \leftarrow  G : \mathbf{Z}$  and  $G$  are dependent given  $\mathbf{W}$  .
is learning word distributions among the groups and group importance. According to  $\delta(Z_{i,j},\iota)$  and  $\delta(G,\iota)$ , the word masks  $\mathbf{W}$  will keep or mask out all words in group  $\iota$ , which satisfies the third property.
3.2 Learning GMASK
We formulate the problem of learning GMASK by optimizing the following objective in terms of the three properties,
$$
\max  _ {\Phi , \Psi} \mathbb {E} [ p (y \mid \boldsymbol {x}, \boldsymbol {z}, \boldsymbol {g}) ] - \gamma_ {1} \mathcal {L} _ {\boldsymbol {Z}} - \gamma_ {2} \mathcal {L} _ {G}, \tag {3}
$$
where  $\Phi$  and  $\Psi$  are parameters of  $Z$  and  $G$  respectively, and  $z$  and  $g$  are samples of  $Z$  and  $G$  respectively. We denote  $\mathcal{L}_Z$  and  $\mathcal{L}_G$  as regularizations on  $Z$  and  $G$  respectively, which are applied to make the learned masks satisfy the required properties. We will introduce the two regularization terms subsequently.  $\gamma_{1},\gamma_{2}\in \mathbb{R}_{+}$  are coefficients.
Optimizing the first term in Equation 3 is to make the word masks  $\mathbf{W}$  satisfy the first property, that is the model outputs the same prediction on the selected words as on the whole text. Given  $\mathbf{Z}$  and  $G$ , we have word masks  $\mathbf{W}$ , and multiply them with input word embeddings, and obtain the masked input  $\tilde{\mathbf{x}}$  as in Equation 1. The model output on  $\tilde{\mathbf{x}}$  is  $\tilde{y} = f(\tilde{\mathbf{x}})$ . If the masks correctly select important words, the predicted label on the selected words should be equal to that on the whole input text. We can optimize the first term by minimizing the cross entropy loss  $(\mathcal{L}_{ce}(\cdot ,\cdot))$  between  $\tilde{y}$  and  $y$ . The objective Equation 3 can be rewritten as
$$
\min  _ {\Phi , \Psi} \mathcal {L} _ {c e} (y, \tilde {y}) + \gamma_ {1} \mathcal {L} _ {Z} + \gamma_ {2} \mathcal {L} _ {G}. \tag {4}
$$
The last two terms in the optimization objective are to make word masks satisfy the second and third properties. We regularize  $Z$  to encourage each group contains some words from different sentences. We regularize  $G$  to ensure only one or few groups are identified as important (with relatively large probabilities). Optimizing the cross entropy loss with the two regularization terms can make the word masks select the important group of words, where the words are selected from the input sentence pair and are correlated.
Regularizations on  $Z$  and  $G$  As each  $Z_{i,j}$  ( $i \in \{1,2\}, j \in \{1,\dots,n_i\}$ ) indicates a word belonging to a specific group, it follows categorical distribution with probabilities  $[\phi_{i,j}(1),\ldots ,\phi_{i,j}(t)]$
where  $t$  is the predefined number of groups, and  $\phi_{i,j}(\iota) (\iota \in \{1, \dots, t\})$  represents the probability of the word in group  $\iota$ . Then we denote the parameters of  $\mathbf{Z}$  as  $\Phi$ ,
$$
\Phi = \left[ \begin{array}{c c c} \phi_ {1, 1} (1) & \dots & \phi_ {1, 1} (t) \\ \vdots & \dots & \vdots \\ \phi_ {1, n _ {1}} (1) & \dots & \phi_ {1, n _ {1}} (t) \\ \phi_ {2, 1} (1) & \dots & \phi_ {2, 1} (t) \\ \vdots & \dots & \vdots \\ \phi_ {2, n _ {2}} (1) & \dots & \phi_ {2, n _ {2}} (t) \end{array} \right] \tag {5}
$$
To ensure that each group contains some words from both input sentences, and also avoid assigning a bunch of words into one group, we distribute the words in each sentence evenly among all groups. Then each group implicitly captures the words from different sentences. We can regularize  $Z$  to achieve this goal. We sum each column of  $\Phi$  along the upper half rows and lower half rows respectively, and obtain two vectors by taking averages,  $\phi^U = \frac{1}{n_1} [\sum_{j=1}^{n_1} \phi_{1,j}(1), \ldots, \sum_{j=1}^{n_1} \phi_{1,j}(t)]$ ,  $\phi^L = \frac{1}{n_2} [\sum_{j=1}^{n_2} \phi_{2,j}(1), \ldots, \sum_{j=1}^{n_2} \phi_{2,j}(t)]$ . Then  $\phi^U$  and  $\phi^L$  are the distributions of two discrete variables  $Z^U$  and  $Z^L$ , which also represent the word distributions of the two input sentences among groups. To make the distributions of words even, we maximize the entropy of  $Z^U$  and  $Z^L$ , and have
$$
\mathcal {L} _ {\boldsymbol {Z}} = - \left(H \left(Z ^ {U}\right) + H \left(Z ^ {L}\right)\right), \tag {6}
$$
where  $H(\cdot)$  is entropy.
$G \in \{1, \dots, t\}$  also follows categorical distribution with probabilities  $\Psi = [\psi(1), \dots, \psi(t)]$ , where  $\psi(\iota) (\iota \in \{1, \dots, t\})$  represents the probability of group  $\iota$  being selected. According to the relation of  $W, Z, G$  in Equation 2, the word masks only keep the words assigned to the selected group. To ensure one or few groups have relatively large probabilities to be selected, we regularize  $G$  by minimizing its entropy, that is  $\mathcal{L}_G = H(G)$ . The final optimization objective is
$$
\min  _ {\Phi , \Psi} \mathcal {L} _ {c e} (y, \tilde {y}) - \gamma_ {1} \left(H \left(Z ^ {U}\right) + H \left(Z ^ {L}\right)\right) + \gamma_ {2} H (G). \tag {7}
$$
Optimization via sampling We adopt a sampling based method to solve Equation 7 by learning the parameters of  $Z$  and  $G$  (i.e.  $\{\Phi, \Psi\}$ ). As the right part of Figure 2 shows, we sample a  $z$  from the categorical distributions of  $Z$ , where each row
$z_{i,j}$  is a one-hot vector, indicating the word  $x_{i,j}$  assigned to a specific group. And we sample a  $\pmb{g}$  from the categorical distribution of  $G$ , which is a vertical one-hot vector, indicating the selected group. Then we obtain a sample of word masks by multiplying  $\pmb{z}$  and  $\pmb{g}$ , i.e.  $\pmb{w} = \pmb{z} \cdot \pmb{g}$ , where the mask values corresponding to the words in the selected group are 1, while the rest are 0. We apply the masks on the input word embeddings and optimize Equation 7 via stochastic gradient descent.
There are two challenges of the learning process: discreteness and large variance. We apply the Gumbel-softmax trick (Jang et al., 2016; Maddison et al., 2016) to address the discreteness of sampling from categorical distributions in backpropagation. See Appendix A for the continuous differentiable approximation of Gumbel-softmax. We do the sampling multiple times in subsection 3.3 and generate a batch of masked inputs of the original input data to decrease the variance in probing the model, and train for multiple epochs until the learnable parameters  $\{\Phi, \Psi\}$  reach stable values.
Weighted word attributions After training, we learn the parameters of  $\mathbf{Z}$ , i.e.  $\Phi$ , where each element  $\phi_{i,j}(\iota) \in (0,1)$  ( $i \in \{1,2\}$ ,  $j \in \{1,\dots,n_i\}$ ,  $\iota \in \{1,\dots,t\}$ ) represents the probability of word  $x_{i,j}$  belonging to group  $\iota$ . We also learn the parameters of  $G$ , i.e.  $\Psi$ , where each element  $\psi(\iota) \in (0,1)$  represents the importance of group  $\iota$ . According to the definition of word masks  $W$  in subsection 3.1, we know that each mask variable  $W_{i,j}$  follows Bernoulli distribution, and the probability of  $W_{i,j}$  taking 1 is denoted as  $\theta_{i,j}$ . We can compute  $\theta_{i,j}$  based on the relation of  $W_{i,j}$ ,  $Z_{i,j}$  and  $G$  in Equation 2, that is
$$
\theta_ {i, j} = \sum_ {\iota = 1} ^ {t} \phi_ {i, j} (\iota) \psi (\iota). \tag {8}
$$
We can see that  $\theta_{i,j}$  is the expectation of  $W_{i,j}$ , representing the weighted attribution of the word  $\pmb{x}_{i,j}$  to the model prediction. Hence, we have a set of weighted word attributions  $\Theta = [\theta_{1,1},\dots,\theta_{1,n_1},\theta_{2,1},\dots,\theta_{2,n_2}]^T$  for extracting important words as an explanation.
Complexity For a set of  $n$  words, computing interactions between all word pairs costs  $O(n^{2})$  and aggregating words step by step to form a tree structure even costs more time (Singh et al., 2018; Chen et al., 2020). GMASK circumvents the feature interaction detection by learning word groups. The
complexity is  $O(nt + t)$ , where  $t$  is the number of groups and usually  $t \ll n$  in practice.
3.3 Implementation Specification
We initialize the parameters of all categorical distributions  $(\{\Phi, \Psi\})$  with  $\frac{1}{t}$ , which means all words have the same importance and do not have any preference to be in a specific group at the start of training. To stabilize the learning process, we sample 100 - 1000 examples (depending on the model and datasets) and train at most 100 epochs until converge. The coefficients  $\gamma_{1}$  and  $\gamma_{2}$  are hyperparameters. We empirically found  $\gamma_{1} = 10$  and  $\gamma_{2} = 1$  work well in our experiments.
In our pilot experiments, we found that preliminarily filtering out some noisy or irrelevant words can help decrease the learnable parameters, hence accelerating the training process. Specifically, we adopt a simple word mask method from (Chen and Ji, 2020) to select a set of individual words for an input sentence pair before running GMASK. This simple method, denoted as IMASK, will learn individual word attributions as masks  $\pmb{R} = \{R_{i,j}\}_{i\in \{1,2\}}, j\in \{1,\dots,n_i\} \in \{0,1\}^{n_1 + n_2}$  regardless any correlation. Then, based on the expected values of  $\pmb{R}$ , we preliminarily select top  $k$  words for GMASK to further learn weighted word attributions. Within these top  $k$  words, assume  $k_{1}$  words from the first input text and  $k_{2}$  words from the second text, then we will set the number of groups as  $t = \min(k_{1},k_{2})$ , so that at least one group contains words from both sentences.  $k$  is a hyper-parameter associated with the average length of input texts. In the experiments, we set  $k = 10$ . Note that, the IMASK method adopted here can also be used as a baseline method for comparison.
We evaluate GMASK with two kinds of neural network models, decomposable attention model (DAttn) (Parikh et al., 2016) and BERT (Devlin et al., 2018), for two types of sentence pair modeling tasks on four datasets. We compare our method with four baselines.
Datasets e-SNLI (Camburu et al., 2018) is natural language inference task, where the model predicts the semantic relationship between two input sentences as entailment, contradiction, or neutral. Quora (Wang et al., 2017), QQP (Wang et al., 2018) and MRPC (Dolan and Brockett, 2005) are paraphrase identification tasks, where the model
Models	e-SNLI	Quora	QQP	MRPC
DAtn	86.62	86.78	85.00	68.30
BERT	90.38	90.48	89.00	83.70
Table 1: The prediction accuracy  $(\%)$  of different models on the four datasets.
decides whether two input texts are semantically equivalent or not. The statistics of the four datasets are in Appendix B.
Models We adopt the decomposable attention model (DAttn) (Parikh et al., 2016) and BERT (Devlin et al., 2018) model, and fine-tune the models on each downstream task to achieve the best performance, as Table 1 shows. The test results on QQP and MRPC are scored by the GLUE benchmark. The corresponding validation accuracy for each reported test accuracy is in Appendix C