A Compare-Aggregate Model for Matching Text Sequences
Many NLP tasks including machine comprehension, answer selection and text entailment require the comparison between sequences. Matching the important units between sequences is a key to solve these problems. In this paper, we present a general “compare-aggregate” framework that performs word-level matching followed by aggregation using Convolutional Neural Networks. We particularly focus on the different comparison functions we can use to match two vectors. We use four different datasets to evaluate the model. We find that some simple comparison functions based on element-wise operations can work better than standard neural network and neural tensor network.
|School of Information Systems|
|Singapore Management University|
|School of Information Systems|
|Singapore Management University|
Many natural language processing problems involve matching two or more sequences to make a decision. For example, in textual entailment, one needs to determine whether a hypothesis sentence can be inferred from a premise sentence (Bowman et al., 2015). In machine comprehension, given a passage, a question needs to be matched against it in order to find the correct answer (Richardson et al., 2013; Tapaswi et al., 2016). Table 1 gives two example sequence matching problems. In the first example, a passage, a question and four candidate answers are given. We can see that to get the correct answer, we need to match the question against the passage and identify the last sentence to be the answer-bearing sentence. In the second example, given a question and a set of candidate answers, we need to find the answer that best matches the question. Because of the fundamental importance of comparing two sequences of text to judge their semantic similarity or relatedness, sequence matching has been well studied in natural language processing.
With recent advances of neural network models in natural language processing, a standard practice for sequence modeling now is to encode a sequence of text as an embedding vector using models such as RNN and CNN. To match two sequences, a straightforward approach is to encode each sequence as a vector and then to combine the two vectors to make a decision (Bowman et al., 2015; Feng et al., 2015). However, it has been found that using a single vector to encode an entire sequence is not sufficient to capture all the important information from the sequence, and therefore advanced techniques such as attention mechanisms and memory networks have been applied to sequence matching problems (Hermann et al., 2015; Hill et al., 2016; Rocktäschel et al., 2015).
A common trait of a number of these recent studies on sequence matching problems is the use of a “compare-aggregate” framework (Wang & Jiang, 2016b; He & Lin, 2016; Parikh et al., 2016). In such a framework, comparison of two sequences is not done by comparing two vectors each representing an entire sequence. Instead, these models first compare vector representations of smaller units such as words from these sequences and then aggregate these comparison results to make the final decision. For example, the match-LSTM model proposed by Wang & Jiang (2016b) for textual entailment first compares each word in the hypothesis with an attention-weighted version of the premise. The comparison results are then aggregated through an LSTM. He & Lin (2016) proposed a pairwise word interaction model that first takes each pair of words from two sequences and applies a comparison unit on the two words. It then combines the results of these word interactions using a similarity focus layer followed by a multi-layer CNN. Parikh et al. (2016) proposed a decomposable attention model for textual entailment, in which words from each sequence are compared with an attention-weighted version of the other sequence to produce a series of comparison vectors. The comparison vectors are then aggregated and fed into a feed forward network for final classification.
Although these studies have shown the effectiveness of such a “compare-aggregate” framework for sequence matching, there are at least two limitations with these previous studies: (1) Each of the models proposed in these studies is tested on one or two tasks only, but we hypothesize that this general framework is effective on many sequence matching problems. There has not been any study that empirically verifies this. (2) More importantly, these studies did not pay much attention to the comparison function that is used to compare two small textual units. Usually a standard feedforward network is used (Hu et al., 2014; Wang & Jiang, 2016b) to combine two vectors representing two units that need to be compared, e.g., two words. However, based on the nature of these sequence matching problems, we essentially need to measure how semantically similar the two sequences are. Presumably, this property of these sequence matching problems should guide us in choosing more appropriate comparison functions. Indeed He & Lin (2016) used cosine similarity, Euclidean distance and dot product to define the comparison function, which seem to be better justifiable. But they did not systematically evaluate these similarity or distance functions or compare them with a standard feedforward network.
In this paper, we argue that the general “compare-aggregate” framework is effective for a wide range of sequence matching problems. We present a model that follows this general framework and test it on four different datasets, namely, MovieQA, InsuranceQA, WikiQA and SNLI. The first three datasets are for Question Answering, but the setups of the tasks are quite different. The last dataset is for textual entailment. More importantly, we systematically present and test six different comparison functions. We find that overall a comparison function based on element-wise subtraction and multiplication works the best on the four datasets.
The contributions of this work are twofold: (1) Using four different datasets, we show that our model following the “compare-aggregate” framework is very effective when compared with the state-of-the-art performance on these datasets. (2) We conduct systematic evaluation of different comparison functions and show that a comparison function based on element-wise operations, which is not widely used for word-level matching, works the best across the different datasets. We believe that these findings will be useful for future research on sequence matching problems. We have also made our code available online.111https://github.com/shuohangwang/SeqMatchSeq
In this section, we propose a general model following the “compare-aggregate” framework for matching two sequences. This general model can be applied to different tasks. We focus our discussion on six different comparison functions that can be plugged into this general “compare-aggregate” model. In particular, we hypothesize that two comparison functions based on element-wise operations, Sub and Mult, are good middle ground between highly flexible functions using standard neural network models and highly restrictive functions based on cosine similarity and/or Euclidean distance. As we will show in the experiment section, these comparison functions based on element-wise operations can indeed perform very well on a number of sequence matching problems.
2.1 Problem Definition and Model Overview
The general setup of the sequence matching problem we consider is the following. We assume there are two sequences to be matched. We use two matrices and to represent the word embeddings of the two sequences, where and are the lengths of the two sequences, respectively, and is the dimensionality of the word embeddings. In other words, each column vector of or is an embedding vector representing a single word. Given a pair of and , the goal is to predict a label . For example, in textual entailment, may represent a premise and a hypothesis, and indicates whether entails or contradicts . In question answering, may be a question and a candidate answer, and indicates whether is the correct answer to .
We treat the problem as a supervised learning task. We assume that a set of training examples in the form of is given and we aim to learn a model that maps any pair of to a .
An overview of our model is shown in Figure 1. The model can be divided into the following four layers:
Preprocessing: We use a preprocessing layer (not shown in the figure) to process and to obtain two new matrices and . The purpose is to obtain a new embedding vector for each word in each sequence that captures some contextual information in addition to the word itself. For example, , which is the column vector of , encodes the word in together with its context in .
Attention: We apply a standard attention mechanism on and to obtain attention weights over the column vectors in for each column vector in . With these attention weights, for each column vector in , we obtain a corresponding vector , which is an attention-weighted sum of the column vectors of .
Comparison: We use a comparison function to combine each pair of and into a vector .
Aggregation: We use a CNN layer to aggregate the sequence of vectors for the final classification.
Although this model follows more or less the same framework as the model proposed by Parikh et al. (2016), our work has some notable differences. First, we will pay much attention to the comparison function and compare a number of options, including a some uncommon ones based on element-wise operations. Second, we apply our model to four different datasets representing four different tasks to evaluate its general effectiveness for sequence matching problems. There are also some other differences from the work by Parikh et al. (2016). For example, we use a CNN layer instead of summation and concatenation for aggregation. Our attention mechanism is one-directional instead of two-directional.
In the rest of this section we will present the model in detail. We will focus mostly on the comparison functions we consider.
2.2 Preprocessing and Attention
Our preprocessing layer uses a recurrent neural network to process the two sequences. We use a modified version of LSTM/GRU in which we keep only the input gates for remembering meaningful words:
where is element-wise multiplication, and and are parameters to be learned. The outer product produces a matrix or row vector by repeating the vector or scalar on the left for times.
The attention layer is built on top of the resulting and as follows:
where and are parameters to be learned, is the attention weight matrix, and are the attention-weighted vectors. Specifically, , which is the column vector of , is a weighted sum of the column vectors of and represents the part of that best matches the word in . Next we will combine and using a comparison function.
The goal of the comparison layer is to match each , which represents the word and its context in , with , which represents a weighted version of that best matches . Let denote a comparison function that transforms and into a vector to represent the comparison result.
A natural choice of is a standard neural network layer that consists of a linear transformation followed by a non-linear activation function. For example, we can consider the following choice:
where matrix and vector are parameters to be learned.
Alternatively, another natural choice is a neural tensor network (Socher et al., 2013) as follows:
where tensor and vector are parameters to be learned.
However, we note that for many sequence matching problems, we intend to measure the semantic similarity or relatedness of the two sequences. So at the word level, we also intend to check how similar or related is to . For this reason, a more natural choice used in some previous work is Euclidean distance or cosine similarity between and . We therefore consider the following definition of :
Note that with EucCos, the resulting vector is only a 2-dimensional vector. Although EucCos is a well-justified comparison function, we suspect that it may lose some useful information from the original vectors and . On the other hand, NN and NTN are too general and thus do not capture the intuition that we care mostly about the similarity between and .
To use something that is a good compromise between the two extreme cases, we consider the following two new comparison functions, which operate on the two vectors in an element-wise manner. These functions have been used previously by Tai et al. (2015).
Note that the operator is element-wise multiplication. For both comparison functions, the resulting vector has the same dimensionality as and .
We can see that Sub is closely related to Euclidean distance in that Euclidean distance is the sum of all the entries of the vector produced by Sub. But by not summing up these entries, Sub preserves some information about the different dimensions of the original two vectors. Similarly, Mult is closely related to cosine similarity but preserves some information about the original two vectors.
Finally, we consider combining Sub and Mult followed by an NN layer as follows:
In summary, we consider six different comparison functions: NN, NTN, EucCos, Sub, Mult and SubMult+NN. Among these functions, the last three (Sub, Mult and SubMult+NN) have not been widely used in previous work for word-level matching.
After we apply the comparison function to each pair of and to obtain a series of vectors , finally we aggregate these vectors using a one-layer CNN (Kim, 2014):
is then used for the final classification, where is the number of windows in CNN.
In this section, we evaluate our model on four different datasets representing different tasks. The first three datasets are question answering tasks while the last one is on textual entailment. The statistics of the four datasets are shown in Table 2. We will fist introduce the task settings and the way we customize the “compare-aggregate” structure to each task. Then we will show the baselines for the different datasets. Finally, we discuss the experiment results shown in Table 3.
|CNN with GESD||-||-||65.4||65.3||61.0||-||-||-||-|
3.1 Task-specific Model Structures
In all these tasks, we use matrix to represent the question or premise and matrix () to represent the answer or the hypothesis. For the machine comprehension task MovieQA (Tapaswi et al., 2016), there is also a matrix that represents the plot of a movie. Here is the length of the question or premise, the length of the answer, and the length of the plot.
For the SNLI (Bowman et al., 2015) dataset, the task is text entailment, which identifies the relationship (entailment, contradiction or neutral) between a premise sentence and a hypothesis sentence. Here , and there are exactly two sequences to match. The actual model structure is what we have described before.
For the InsuranceQA (Feng et al., 2015) dataset, the task is an answer selection task which needs to select the correct answer for a question from a candidate pool. For the WikiQA (Yang et al., 2015) datasets, we need to rank the candidate answers according to a question. For both tasks, there are candidate answers for each question. Let us use to represent the resulting vector produced by Eqn. 9 for the answer. In order to select one of the answers, we first define . We then compute the probability of the answer to be the correct one as follows:
where , , , are parameters to be learned.
For the machine comprehension task MovieQA, each question is related to Plot Synopses written by fans after watching the movie and each question has five candidate answers. So for each candidate answer there are three sequences to be matched: the plot , the question and the answer . For each , we first match and and refer to the matching result at position as , as generated by one of the comparison functions . Similarly, we also match with and refer to the matching result at position as . We then define
To select an answer from the candidate answers, again we use Eqn. 10 to compute the probabilities.
Here, we will introduce the baselines for each dataset. We did not re-implement these models but simply took the reported performance for the purpose of comparison.
SNLI: W-by-W Attention: The model by Rocktäschel et al. (2015), who first introduced attention mechanism into text entailment. match-LSTM: The model by Wang & Jiang (2016b), which concatenates the matched words as the inputs of an LSTM. LSTMN: Long short-term memory-networks proposed by Cheng et al. (2016). Decomp Attention: Another “compare-aggregate” model proposed by Parikh et al. (2016). EBIM+TreeLSTM: The state-of-the-art model proposed by Chen et al. (2016) on the SNLI dataset.
InsuranceQA: IR model: This model by Bendersky et al. (2010) learns the concept information to help rank the candidates. CNN with GESD: This model by Feng et al. (2015) uses Euclidean distance and dot product between sequence representations built through convolutional neural networks to select the answer. Attentive LSTM: Tan et al. (2016) used soft-attention mechanism to select the most important information from the candidates according to the representation of the questions. IARNN-Occam: This model by Wang et al. (2016) adds regularization on the attention weights. IARNN-Gate: This model by Wang et al. (2016) uses the representation of the question to build the GRU gates for each candidate answer.
WikiQA: IARNN-Occam and IARNN-Gate as introduced before. CNN-Cnt: This model by Yang et al. (2015) combines sentence representations built by a convolutional neural network with logistic regression. ABCNN: This model is Attention-Based Convolutional Neural Network proposed by Yin et al. (2015). CubeCNN proposed by He & Lin (2016) builds a CNN on all pairs of word similarity.
MovieQA: All the baselines we consider come from Tapaswi et al. (2016)’s work: Cosine Word2Vec: A sliding window is used to select the answer according to the similarities computed through Word2Vec between the sentences in plot and the question/answer. Cosine TFIDF: This model is similar to the previous method but uses bag-of-word with tf-idf scores to compute similarity. SSCB TFIDF: Instead of using the sliding window method, a convolutional neural network is built on the sentence level similarities.
3.3 Analysis of Results
We use accuracy as the evaluation metric for the datasets MovieQA, InsuranceQA and SNLI, as there is only one correct answer or one label for each instance. For WikiQA, there may be multiple correct answers, so evaluation metrics we use are Mean Average Precision (MAP) and Mean Reciprocal Rank (MRR).
We observe the following from the results. (1) Overall, we can find that our general “compare-aggregate” structure achieves the best performance on MovieQA, InsuranceQA, WikiQA datasets and very competitive performance on the SNLI dataset. Especially for the InsuranceQA dataset, with any comparison function we use, our model can outperform all the previous models. (2) The comparison method SubMult+NN is the best in general. (3) Some simple comparison functions can achieve better performance than the neural networks or neural tensor network comparison functions. For example, the simplest comparison function EucCos achieves nearly the best performance in the MovieQA dataset, and the element-wise comparison functions, which do not need parameters can achieve the best performance on the WikiQA data set.
3.4 Further Analyses
To further explain how our model works, we visualize the max values in each dimension of the convolutional layer. We use two examples shown in Table 1 from MovieQA and InsuranceQA data sets respectively. In the top of Figure 2, we can see that the plot words that also appear in either the question or the answer will draw more attention by the CNN. We hypothesize that if the nearby words in the plot can match both the words in question and the words in one answer, then this answer is more likely to be the correct one. Similarly, the bottom one of Figure 2 also shows that the CNN will focus more on the matched word representations. If the words in one answer continuously match the words in the question, this answer is more likely to be the correct one.
4 Related Work
We review related work in three types of general structures for matching sequences.
Siamense network: These kinds of models use the same structure, such as RNN or CNN, to build the representations for the sequences separately and then use them for classification. Then cosine similarity (Feng et al., 2015; Yang et al., 2015), element-wise operation (Tai et al., 2015; Mou et al., 2016) or neural network-based combination Bowman et al. (2015) are used for sequence matching.
Attentive network: Soft-attention mechanism (Bahdanau et al., 2014) has been widely used for sequence matching in machine comprehension (Hermann et al., 2015), text entailment (Rocktäschel et al., 2015) and question answering (Tan et al., 2016). Instead of using the final state of RNN to represent a sequence, these studies use weighted sum of all the states for the sequence representation.
Compare-Aggregate network: This kind of framework is to perform the word level matching (Wang & Jiang, 2016a; Parikh et al., 2016; He & Lin, 2016; Trischler et al., 2016). Our work is under this framework. But our structure is different from previous models and our model can be applied on different tasks. Besides, we analyzed different word-level comparison functions separately.
In this paper, we systematically analyzed the effectiveness of a “compare-aggregate” model on four different datasets representing different tasks. Moreover, we compared and tested different kinds of word-level comparison functions and found that some element-wise comparison functions can outperform the others. According to our experiment results, many different tasks can share the same “compare-aggregate” structure. In the future work, we would like to test its effectiveness on multi-task learning.
- Bahdanau et al. (2014) Dzmitry Bahdanau, Kyunghyun Cho, and Yoshua Bengio. Neural machine translation by jointly learning to align and translate. In Proceedings of the International Conference on Learning Representations, 2014.
- Bendersky et al. (2010) Michael Bendersky, Donald Metzler, and W Bruce Croft. Learning concept importance using a weighted dependence model. In Proceedings of the third ACM international conference on Web search and data mining, pp. 31–40. ACM, 2010.
- Bowman et al. (2015) Samuel R Bowman, Gabor Angeli, Christopher Potts, and Christopher D Manning. A large annotated corpus for learning natural language inference. In Proceedings of the Conference on Empirical Methods in Natural Language Processing, 2015.
- Chen et al. (2016) Qian Chen, Xiaodan Zhu, Zhenhua Ling, Si Wei, and Hui Jiang. Enhancing and combining sequential and tree lstm for natural language inference. arXiv preprint arXiv:1609.06038, 2016.
- Cheng et al. (2016) Jianpeng Cheng, Li Dong, and Mirella Lapata. Long short-term memory-networks for machine reading. In Proceedings of the Conference on Empirical Methods in Natural Language Processing, 2016.
- Feng et al. (2015) Minwei Feng, Bing Xiang, Michael R Glass, Lidan Wang, and Bowen Zhou. Applying deep learning to answer selection: A study and an open task. In 2015 IEEE Workshop on Automatic Speech Recognition and Understanding (ASRU), pp. 813–820. IEEE, 2015.
- He & Lin (2016) Hua He and Jimmy Lin. Pairwise word interaction modeling with deep neural networks for semantic similarity measurement. In Proceedings of the 2016 Conference of the North American Chapter of the Association for Computational Linguistics: Human Language Technologies, pp. 937–948, 2016.
- Hermann et al. (2015) Karl Moritz Hermann, Tomas Kocisky, Edward Grefenstette, Lasse Espeholt, Will Kay, Mustafa Suleyman, and Phil Blunsom. Teaching machines to read and comprehend. In Proceedings of the Conference on Advances in Neural Information Processing Systems, pp. 1693–1701, 2015.
- Hill et al. (2016) Felix Hill, Antoine Bordes, Sumit Chopra, and Jason Weston. The Goldilocks principle: Reading children’s books with explicit memory representations. In Proceedings of the International Conference on Learning Representations, 2016.
- Hu et al. (2014) Baotian Hu, Zhengdong Lu, Hang Li, and Qingcai Chen. Convolutional neural network architectures for matching natural language sentences. In Advances in Neural Information Processing Systems, pp. 2042–2050, 2014.
- Kim (2014) Yoon Kim. Convolutional neural networks for sentence classification. In Proceedings of the Conference on Empirical Methods in Natural Language Processing, 2014.
- Kingma & Ba (2015) Diederik Kingma and Jimmy Ba. Adam: A method for stochastic optimization. In Proceedings of the International Conference on Learning Representations, 2015.
- Mou et al. (2016) Lili Mou, Rui Men, Ge Li, Yan Xu, Lu Zhang, Rui Yan, and Zhi Jin. Natural language inference by tree-based convolution and heuristic matching. In Proceedings of the Conference on Empirical Methods in Natural Language Processing, 2016.
- Parikh et al. (2016) Ankur P Parikh, Oscar Täckström, Dipanjan Das, and Jakob Uszkoreit. A decomposable attention model for natural language inference. In Proceedings of the Conference on Empirical Methods in Natural Language Processing, 2016.
- Pennington et al. (2014) Jeffrey Pennington, Richard Socher, and Christopher D Manning. GloVe: Global vectors for word representation. In Proceedings of the Conference on Empirical Methods in Natural Language Processing, 2014.
- Richardson et al. (2013) Matthew Richardson, Christopher JC Burges, and Erin Renshaw. MCTest: A challenge dataset for the open-domain machine comprehension of text. In Proceedings of the Conference on Empirical Methods in Natural Language Processing, 2013.
- Rocktäschel et al. (2015) Tim Rocktäschel, Edward Grefenstette, Karl Moritz Hermann, Tomáš Kočiskỳ, and Phil Blunsom. Reasoning about entailment with neural attention. In Proceedings of the International Conference on Learning Representations, 2015.
- Socher et al. (2013) Richard Socher, Alex Perelygin, Jean Y Wu, Jason Chuang, Christopher D Manning, Andrew Y Ng, and Christopher Potts. Recursive deep models for semantic compositionality over a sentiment treebank. In Proceedings of the conference on empirical methods in natural language processing, 2013.
- Tai et al. (2015) Kai Sheng Tai, Richard Socher, and Christopher D Manning. Improved semantic representations from tree-structured long short-term memory networks. In Proceedings of the Conference on Association for Computational Linguistics, 2015.
- Tan et al. (2016) Ming Tan, Cicero dos Santos, Bing Xiang, and Bowen Zhou. Improved representation learning for question answer matching. In Proceedings of the Conference on Association for Computational Linguistics, 2016.
- Tapaswi et al. (2016) Makarand Tapaswi, Yukun Zhu, Rainer Stiefelhagen, Antonio Torralba, Raquel Urtasun, and Sanja Fidler. MovieQA: Understanding stories in movies through question-answering. In Proceedings of IEEE Conference on Computer Vision and Pattern Recognition, 2016.
- Trischler et al. (2016) Adam Trischler, Zheng Ye, Xingdi Yuan, Jing He, Phillip Bachman, and Kaheer Suleman. A parallel-hierarchical model for machine comprehension on sparse data. In Proceedings of the Conference on Association for Computational Linguistics, 2016.
- Wang et al. (2016) Bingning Wang, Kang Liu, and Jun Zhao. Inner attention based recurrent neural networks for answer selection. In Proceedings of the Conference on Association for Computational Linguistics, 2016.
- Wang & Jiang (2016a) Shuohang Wang and Jing Jiang. Machine comprehension using match-lstm and answer pointer. arXiv preprint arXiv:1608.07905, 2016a.
- Wang & Jiang (2016b) Shuohang Wang and Jing Jiang. Learning natural language inference with LSTM. In Proceedings of the Conference on the North American Chapter of the Association for Computational Linguistics, 2016b.
- Yang et al. (2015) Yi Yang, Wen-tau Yih, and Christopher Meek. Wikiqa: A challenge dataset for open-domain question answering. In Proceedings of the Conference on Empirical Methods in Natural Language Processing, 2015.
- Yin et al. (2015) Wenpeng Yin, Hinrich Schütze, Bing Xiang, and Bowen Zhou. Abcnn: Attention-based convolutional neural network for modeling sentence pairs. arXiv preprint arXiv:1512.05193, 2015.
Appendix A appendix
Following are the implementation details. The word embeddings are initialized from GloVe (Pennington et al., 2014). During training, they are not updated. The word embeddings not found in GloVe are initialized with zero.
The dimensionality of the hidden layers is set to be 150. We use ADAMAX (Kingma & Ba, 2015) with the coefficients and to optimize the model. The batch size is set to be 30 and the learning rate is 0.002. We do not use L2-regularization. The hyper-parameter we tuned is the dropout on the embedding layer. For WikiQA, which is relatively small dataset, we also tune the learning rate and batch size. For the convolutional window sizes for MovieQA, InsuranceQA, WikiQA and SNLI, we use [1,3,5], [1,2,3], [1,2,3,4,5] and [1,2,3,4,5], respectively.