Implementation of Dynamic memory networks by Kumar et al. http://arxiv.org/abs/1506.07285
Most tasks in natural language processing can be cast into question answering (QA) problems over language input. We introduce the dynamic memory network (DMN), a neural network architecture which processes input sequences and questions, forms episodic memories, and generates relevant answers. Questions trigger an iterative attention process which allows the model to condition its attention on the inputs and the result of previous iterations. These results are then reasoned over in a hierarchical recurrent sequence model to generate answers. The DMN can be trained end-to-end and obtains state-of-the-art results on several types of tasks and datasets: question answering (Facebook's bAbI dataset), text classification for sentiment analysis (Stanford Sentiment Treebank) and sequence modeling for part-of-speech tagging (WSJ-PTB). The training for these different tasks relies exclusively on trained word vector representations and input-question-answer triplets.READ FULL TEXT VIEW PDF
Implementation of Dynamic memory networks by Kumar et al. http://arxiv.org/abs/1506.07285
Implementation of Dynamic Memory Networks (Socher et. al. - https://arxiv.org/pdf/1506.07285.pdf)
Graphical Implementation of Dynamic Memory Network for Natural Language Processing based on the Metamind Article (https://arxiv.org/pdf/1506.07285.pdf).
Final Project for Metis Data Science Bootcamp, automated customer service agent
Keras implementation of the dynamic memory networks from https://arxiv.org/pdf/1603.01417.pdf
Question answering (QA) is a complex natural language processing task which requires an understanding of the meaning of a text and the ability to reason over relevant facts. Most, if not all, tasks in natural language processing can be cast as a question answering problem: high level tasks like machine translation (What is the translation into French?
); sequence modeling tasks like named entity recognition(Passos et al., 2014) (NER) (What are the named entity tags in this sentence?) or part-of-speech tagging (POS) (What are the part-of-speech tags?); classification problems like sentiment analysis (Socher et al., 2013) (What is the sentiment?); even multi-sentence joint classification problems like coreference resolution (Who does ”their” refer to?).
We propose the Dynamic Memory Network (DMN), a neural network based framework for general question answering tasks that is trained using raw input-question-answer triplets. Generally, it can solve sequence tagging tasks, classification problems, sequence-to-sequence tasks and question answering tasks that require transitive reasoning.
The DMN first computes a representation for all inputs and the question. The question representation then triggers an iterative attention process that searches the inputs and retrieves relevant facts. The DMN memory module then reasons over retrieved facts and provides a vector representation of all relevant information to an answer module which generates the answer.
Fig. 1 provides examples of inputs, questions and answers for tasks that are evaluated in this paper and for which a DMN achieves a new level of state-of-the-art performance.
|I:||Jane went to the hallway.|
|I:||Mary walked to the bathroom.|
|I:||Sandra went to the garden.|
|I:||Daniel went back to the garden.|
|I:||Sandra took the milk there.|
|Q:||Where is the milk?|
|I:||It started boring, but then it got interesting.|
|Q:||What’s the sentiment?|
|A:||PRP VBD JJ , CC RB PRP VBD JJ .|
We now give an overview of the modules that make up the DMN. We then examine each module in detail and give intuitions about its formulation. A high-level illustration of the DMN is shown in Fig. 2.
Input Module: The input module encodes raw text inputs from the task into distributed vector representations. In this paper, we focus on natural language related problems. In these cases, the input may be a sentence, a long story, a movie review, a news article, or several Wikipedia articles.
Question Module: Like the input module, the question module encodes the question of the task into a distributed vector representation. For example, in the case of question answering, the question may be a sentence such as Where did the author first fly?. The representation is fed into the episodic memory module, and forms the basis, or initial state, upon which the episodic memory module iterates.
Episodic Memory Module: Given a collection of input representations, the episodic memory module chooses which parts of the inputs to focus on through the attention mechanism. It then produces a ”memory” vector representation taking into account the question as well as the previous memory. Each iteration provides the module with newly relevant information about the input. In other words, the module has the ability to retrieve new information, in the form of input representations, which were thought to be irrelevant in previous iterations.
Answer Module: The answer module generates an answer from the final memory vector of the memory module.
A detailed visualization of these modules is shown in Fig.3.
In natural language processing problems, the input is a sequence of words
. One way to encode the input sequence is via a recurrent neural network(Elman, 1991). Word embeddings are given as inputs to the recurrent network. At each time step , the network updates its hidden state , where is the embedding matrix and is the word index of the th word of the input sequence.
In cases where the input sequence is a single sentence, the input module outputs the hidden states of the recurrent network. In cases where the input sequence is a list of sentences, we concatenate the sentences into a long list of word tokens, inserting after each sentence an end-of-sentence token. The hidden states at each of the end-of-sentence tokens are then the final representations of the input module. In subsequent sections, we denote the output of the input module as the sequence of fact representations , whereby denotes the th element in the output sequence of the input module. Note that in the case where the input is a single sentence, . That is, the number of output representations is equal to the number of words in the sentence. In the case where the input is a list of sentences, is equal the number of sentences.
Choice of recurrent network: In our experiments, we use a gated recurrent network (GRU) (Cho et al., 2014a; Chung et al., 2014). We also explored the more complex LSTM (Hochreiter & Schmidhuber, 1997) but it performed similarly and is more computationally expensive. Both work much better than the standard
RNN and we postulate that the main strength comes from having gates that allow the model to suffer less from the vanishing gradient problem(Hochreiter & Schmidhuber, 1997). Assume each time step has an input and a hidden state . The internal mechanics of the GRU is defined as:
where is an element-wise product, and . The dimensions
are hyperparameters. We abbreviate the above computation with.
Similar to the input sequence, the question is also most commonly given as a sequence of words in natural language processing problems. As before, we encode the question via a recurrent neural network. Given a question of words, hidden states for the question encoder at time is given by , represents the word embedding matrix as in the previous section and represents the word index of the th word in the question. We share the word embedding matrix across the input module and the question module. Unlike the input module, the question module produces as output the final hidden state of the recurrent network encoder: .
The episodic memory module iterates over representations outputted by the input module, while updating its internal episodic memory. In its general form, the episodic memory module is comprised of an attention mechanism as well as a recurrent network with which it updates its memory. During each iteration, the attention mechanism attends over the fact representations while taking into consideration the question representation and the previous memory to produce an episode .
The episode is then used, alongside the previous memories , to update the episodic memory . The initial state of this GRU is initialized to the question vector itself: . For some tasks, it is beneficial for episodic memory module to take multiple passes over the input. After passes, the final memory is given to the answer module.
Need for Multiple Episodes: The iterative nature of this module allows it to attend to different inputs during each pass. It also allows for a type of transitive inference, since the first pass may uncover the need to retrieve additional facts. For instance, in the example in Fig. 3, we are asked Where is the football? In the first iteration, the model ought attend to sentence 7 (John put down the football.), as the question asks about the football. Only once the model sees that John is relevant can it reason that the second iteration should retrieve where John was. Similarly, a second pass may help for sentiment analysis as we show in the experiments section below.
Attention Mechanism: In our work, we use a gating function as our attention mechanism. For each pass , the mechanism takes as input a candidate fact , a previous memory , and the question to compute a gate: .
The scoring function takes as input the feature set and produces a scalar score. We first define a large feature vector that captures a variety of similarities between input, memory and question vectors:
where is the element-wise product. The function
is a simple two-layer feed forward neural network
Some datasets, such as Facebook’s bAbI dataset, specify which facts are important for a given question. In those cases, the attention mechanism of the function can be trained in a supervised fashion with a standard cross-entropy cost function.
Memory Update Mechanism: To compute the episode for pass , we employ a modified GRU over the sequence of the inputs , weighted by the gates . The episode vector that is given to the answer module is the final state of the GRU. The equation to update the hidden states of the GRU at time and the equation to compute the episode are, respectively:
Criteria for Stopping: The episodic memory module also has a signal to stop iterating over inputs. To achieve this, we append a special end-of-passes representation to the input, and stop the iterative attention process if this representation is chosen by the gate function. For datasets without explicit supervision, we set a maximum number of iterations. The whole module is end-to-end differentiable.
The answer module generates an answer given a vector. Depending on the type of task, the answer module is either triggered once at the end of the episodic memory or at each time step.
We employ another GRU whose initial state is initialized to the last memory . At each timestep, it takes as input the question , last hidden state , as well as the previously predicted output .
where we concatenate the last generated word and the question vector as the input at each time step. The output is trained with the cross-entropy error classification of the correct sequence appended with a special end-of-sequence token.
In the sequence modeling task, we wish to label each word in the original sequence. To this end, the DMN is run in the same way as above over the input words. For word , we replace Eq. 8 with . Note that the gates for the first pass will be the same for each word, as the question is the same. This allows for speed-up in implementation by computing these gates only once. However, gates for subsequent passes will be different, as the episodes are different.
Training is cast as a supervised classification problem to minimize cross-entropy error of the answer sequence. For datasets with gate supervision, such as bAbI, we add the cross-entropy error of the gates into the overall cost. Because all modules communicate over vector representations and various types of differentiable and deep neural networks with gates, the entire DMN model can be trained via backpropagation and gradient descent.
Given the many shoulders on which this paper is standing and the many applications to which our model is applied, it is impossible to do related fields justice.
There are several deep learning models that have been applied to many different tasks in NLP. For instance, recursive neural networks have been used for parsing(Socher et al., 2011), sentiment analysis (Socher et al., 2013), paraphrase detection (Socher et al., 2011) and question answering (Iyyer et al., 2014) and logical inference (Bowman et al., 2014), among other tasks. However, because they lack the memory and question modules, a single model cannot solve as many varied tasks, nor tasks that require transitive reasoning over multiple sentences. Another commonly used model is the chain-structured recurrent neural network of the kind we employ above. Recurrent neural networks have been successfully used in language modeling (Mikolov & Zweig, 2012), speech recognition, and sentence generation from images (Karpathy & Fei-Fei, 2015). Also relevant is the sequence-to-sequence model used for machine translation by Sutskever et al. (Sutskever et al., 2014). This model uses two extremely large and deep LSTMs to encode a sentence in one language and then decode the sentence in another language. This sequence-to-sequence model is a special case of the DMN without a question and without episodic memory. Instead it maps an input sequence directly to an answer sequence.
Attention and Memory: The second line of work that is very relevant to DMNs is that of attention and memory in deep learning. Attention mechanisms are generally useful and can improve image classification (Stollenga & J. Masci, 2014), automatic image captioning (Xu et al., 2015) and machine translation (Cho et al., 2014b; Bahdanau et al., 2014)
. Neural Turing machines use memory to solve algorithmic problems such as list sorting(Graves et al., 2014). The work of recent months by Weston et al. on memory networks (Weston et al., 2015b) focuses on adding a memory component for natural language question answering. They have an input (I) and response (R) component and their generalization (G) and output feature map (O) components have some functional overlap with our episodic memory. However, the Memory Network cannot be applied to the same variety of NLP tasks since it processes sentences independently and not via a sequence model. It requires bag of -gram vector features as well as a separate feature that captures whether a sentence came before another one.
Various other neural memory or attention architectures have recently been proposed for algorithmic problems (Joulin & Mikolov, 2015; Kaiser & Sutskever, 2015), caption generation for images (Malinowski & Fritz, 2014; Chen & Zitnick, 2014), visual question answering (Yang et al., 2015) or other NLP problems and datasets (Hermann et al., 2015).
In contrast, the DMN employs neural sequence models for input representation, attention, and response mechanisms, thereby naturally capturing position and temporality. As a result, the DMN is directly applicable to a broader range of applications without feature engineering. We compare directly to Memory Networks on the bAbI dataset (Weston et al., 2015a).
NLP Applications: The DMN is a general model which we apply to several NLP problems. We compare to what, to the best of our knowledge, is the current state-of-the-art method for each task.
There are many different approaches to question answering: some build large knowledge bases (KBs) with open information extraction systems (Yates et al., 2007), some use neural networks, dependency trees and KBs (Bordes et al., 2012), others only sentences (Iyyer et al., 2014). A lot of other approaches exist. When QA systems do not produce the right answer, it is often unclear if it is because they do not have access to the facts, cannot reason over them or have never seen this type of question or phenomenon. Most QA dataset only have a few hundred questions and answers but require complex reasoning. They can hence not be solved by models that have to learn purely from examples. While synthetic datasets (Weston et al., 2015a) have problems and can often be solved easily with manual feature engineering, they let us disentangle failure modes of models and understand necessary QA capabilities. They are useful for analyzing models that attempt to learn everything and do not rely on external features like coreference, POS, parsing, logical rules, etc. The DMN is such a model. Another related model by Andreas et al. (2016) combines neural and logical reasoning for question answering over knowledge bases and visual question answering.
reports the previous state-of-the-art result based on a convolutional neural network that uses multiple word vector representations. The previous best model forpart-of-speech tagging on the Wall Street Journal section of the Penn Tree Bank (Marcus et al., 1993) was Sogaard (Søgaard, 2011) who used a semisupervised nearest neighbor approach. We also directly compare to paragraph vectors by (Le & Mikolov., 2014).
Neuroscience: The episodic memory in humans stores specific experiences in their spatial and temporal context. For instance, it might contain the first memory somebody has of flying a hang glider. Eichenbaum and Cohen have argued that episodic memories represent a form of relationship (i.e., relations between spatial, sensory and temporal information) and that the hippocampus is responsible for general relational learning (Eichenbaum & Cohen, 2004). Interestingly, it also appears that the hippocampus is active during transitive inference (Heckers et al., 2004), and disruption of the hippocampus impairs this ability (Dusek & Eichenbaum, 1997).
The episodic memory module in the DMN is inspired by these findings. It retrieves specific temporal states that are related to or triggered by a question. Furthermore, we found that the GRU in this module was able to do some transitive inference over the simple facts in the bAbI dataset. This module also has similarities to the Temporal Context Model (Howard & Kahana, 2002) and its Bayesian extensions (Socher et al., 2009) which were developed to analyze human behavior in word recall experiments.
We include experiments on question answering, part-of-speech tagging, and sentiment analysis. The model is trained independently for each problem, while the architecture remains the same except for the answer module and input fact subsampling (words vs sentences). The answer module, as described in Section 2.4, is triggered either once at the end or for each token.
For all datasets we used either the official train, development, test splits or if no development set was defined, we used 10% of the training set for development. Hyper-parameter tuning and model selection (with early stopping) is done on the development set. The DMN is trained via backpropagation and Adam (Kingma & Ba, 2014). We employ regularization, and dropout on the word embeddings. Word vectors are pre-trained using GloVe (Pennington et al., 2014).
The Facebook bAbI dataset is a synthetic dataset for testing a model’s ability to retrieve facts and reason over them. Each task tests a different skill that a question answering model ought to have, such as coreference resolution, deduction, and induction. Showing an ability exists here is not sufficient to conclude a model would also exhibit it on real world text data. It is, however, a necessary condition.
|1: Single Supporting Fact||100||100|
|2: Two Supporting Facts||100||98.2|
|3: Three Supporting Facts||100||95.2|
|4: Two Argument Relations||100||100|
|5: Three Argument Relations||98||99.3|
|6: Yes/No Questions||100||100|
|9: Simple Negation||100||100|
|10: Indefinite Knowledge||98||97.5|
|11: Basic Coreference||100||99.9|
|13: Compound Coreference||100||99.8|
|14: Time Reasoning||99||100|
|15: Basic Deduction||100||100|
|16: Basic Induction||100||99.4|
|17: Positional Reasoning||65||59.6|
|18: Size Reasoning||95||95.3|
|19: Path Finding||36||34.5|
|20: Agent’s Motivations||100||100|
|Mean Accuracy (%)||93.3||93.6|
Training on the bAbI dataset uses the following objective function: , where is the standard cross-entropy cost and and are hyperparameters. In practice, we begin training with set to 1 and set to 0, and then later switch to 1 while keeping at 1. As described in Section 2.1, the input module outputs fact representations by taking the encoder hidden states at time steps corresponding to the end-of-sentence tokens. The gate supervision aims to select one sentence per pass; thus, we also experimented with modifying Eq. 8 to a simple instead of a GRU. Here, we compute the final episode vector via: , where , and here is the value of the gate before the sigmoid. This setting achieves better results, likely because the softmax encourages sparsity and is better suited to picking one sentence at a time.
We list results in Table 1
. The DMN does worse than the Memory Network, which we refer to from here on as MemNN, on tasks 2 and 3, both tasks with long input sequences. We suspect that this is due to the recurrent input sequence model having trouble modeling very long inputs. The MemNN does not suffer from this problem as it views each sentence separately. The power of the episodic memory module is evident in tasks 7 and 8, where the DMN significantly outperforms the MemNN. Both tasks require the model to iteratively retrieve facts and store them in a representation that slowly incorporates more of the relevant information of the input sequence. Both models do poorly on tasks 17 and 19, though the MemNN does better. We suspect this is due to the MemNN using n-gram vectors and sequence position features.
The Stanford Sentiment Treebank (SST) (Socher et al., 2013)
is a popular dataset for sentiment classification. It provides phrase-level fine-grained labels, and comes with a train/development/test split. We present results on two formats: fine-grained root prediction, where all full sentences (root nodes) of the test set are to be classified as either very negative, negative, neutral, positive, or very positive, and binary root prediction, where all non-neutral full sentences of the test set are to be classified as either positive or negative. To train the model, we use all full sentences as well as subsample 50% of phrase-level labels every epoch. During evaluation, the model is only evaluated on the full sentences (root setup). In binary classification, neutral phrases are removed from the dataset. The DMN achieves state-of-the-art accuracy on the binary classification task, as well as on the fine-grained classification task.
In all experiments, the DMN was trained with GRU sequence models. It is easy to replace the GRU sequence model with any of the models listed above, as well as incorporate tree structure in the retrieval process.
Part-of-speech tagging is traditionally modeled as a sequence tagging problem: every word in a sentence is to be classified into its part-of-speech class (see Fig. 1). We evaluate on the standard Wall Street Journal dataset (Marcus et al., 1993). We use the standard splits of sections 0-18 for training, 19-21 for development and 22-24 for test sets (Søgaard, 2011). Since this is a word level tagging task, DMN memories are classified at each time step corresponding to each word. This is described in detail in Section 2.4’s discussion of sequence modeling.
|Suzuki et al.||97.40|
|Spoustova et al.||97.44|
We compare the DMN with the results in (Søgaard, 2011). The DMN achieves state-of-the-art accuracy with a single model, reaching a development set accuracy of 97.5. Ensembling the top 4 development models, the DMN gets to 97.58 dev and 97.56 test accuracies, achieving a slightly higher new state-of-the-art (Table 3).
The main novelty of the DMN architecture is in its episodic memory module. Hence, we analyze how important the episodic memory module is for NLP tasks and in particular how the number of passes over the input affect accuracy.
Table 4 shows the accuracies on a subset of bAbI tasks as well as on the Stanford Sentiment Treebank. We note that for several of the hard reasoning tasks, multiple passes over the inputs are crucial to achieving high performance. For sentiment the differences are smaller. However, two passes outperform a single pass or zero passes. In the latter case, there is no episodic memory at all and outputs are passed directly from the input module to the answer module. We note that, especially complicated examples are more often correctly classified with 2 passes but many examples in sentiment contain only simple sentiment words and no negation or misleading expressions. Hence the need to have a complicated architecture for them is small. The same is true for POS tagging. Here, differences in accuracy are less than 0.1 between different numbers of passes.
Next, we show that the additional correct classifications are hard examples with mixed positive/negative vocabulary.
Apart from a quantitative analysis, we also show qualitatively what happens to the attention during multiple passes. We present specific examples from the experiments to illustrate that the iterative nature of the episodic memory module enables the model to focus on relevant parts of the input. For instance, Table 5 shows an example of what the DMN focuses on during each pass of a three-iteration scan on a question from the bAbI dataset.
Question: Where was Mary before the Bedroom?
|Facts||Episode 1||Episode 2||Episode 3|
|Yesterday Julie traveled to the school.|
|Yesterday Marie went to the cinema.|
|This morning Julie traveled to the kitchen.|
|Bill went back to the cinema yesterday.|
|Mary went to the bedroom this morning.|
|Julie went back to the bedroom this afternoon.|
We also evaluate the episodic memory module for sentiment analysis. Given that the DMN performs well with both one iteration and two iterations, we study test examples where the one-iteration DMN is incorrect and the two-episode DMN is correct. Looking at the sentences in Fig. 4 and 5, we make the following observations:
The attention of the two-iteration DMN is generally much more focused compared to that of the one-iteration DMN. We believe this is due to the fact that with fewer iterations over the input, the hidden states of the input module encoder have to capture more of the content of adjacent time steps. Hence, the attention mechanism cannot only focus on a few key time steps. Instead, it needs to pass all necessary information to the answer module from a single pass.
During the second iteration of the two-iteration DMN, the attention becomes significantly more focused on relevant key words and less attention is paid to strong sentiment words that lose their sentiment in context. This is exemplified by the sentence in Fig. 5 that includes the very positive word ”best.” In the first iteration, the word ”best” dominates the attention scores (darker color means larger score). However, once its context, ”is best described”, is clear, its relevance is diminished and ”lukewarm” becomes more important.
We conclude that the ability of the episodic memory module to perform multiple passes over the data is beneficial. It provides significant benefits on harder bAbI tasks, which require reasoning over several pieces of information or transitive reasoning. Increasing the number of passes also slightly improves the performance on sentiment analysis, though the difference is not as significant. We did not attempt more iterations for sentiment analysis as the model struggles with overfitting with three passes.
The DMN model is a potentially general architecture for a variety of NLP applications, including classification, question answering and sequence modeling. A single architecture is a first step towards a single joint model for multiple NLP problems. The DMN is trained end-to-end with one, albeit complex, objective function. Future work will explore additional tasks, larger multi-task models and multimodal inputs and questions.
Dynamic Pooling and Unfolding Recursive Autoencoders for Paraphrase Detection.In NIPS, 2011.