19.5 Natural Language Inference: Using Attention

We introduced the natural language inference task and the SNLI dataset in Section 19.4. In view of many models that are based on complex and deep architectures, Parikh et al. (2016) proposed to address natural language inference with attention mechanisms and called it a "decomposable attention model". This results in a model without recurrent or convolutional layers, achieving the best result at the time on the SNLI dataset with much fewer parameters. In this section, we will describe and implement this attention-based method (with MLPs) for natural language inference, as depicted in Figure 19.5.1.

Figure 19.5.1:

This section feeds pretrained GloVe to an architecture based on attention and MLPs for natural language inference.

19.5.1 The Model#

Rather than preserving the order of tokens in premises and hypotheses, we can simply align tokens in one text sequence to every token in the other, and vice versa, then compare and aggregate such information to predict the logical relationships between premises and hypotheses. Similar to alignment of tokens between source and target sentences in machine translation, the alignment of tokens between premises and hypotheses can be neatly accomplished by attention mechanisms.

Figure 19.5.2:

Natural language inference using attention mechanisms.

Figure 19.5.2 depicts the natural language inference method using attention mechanisms. At a high level, it consists of three jointly trained steps: attending, comparing, and aggregating. We will illustrate them step by step in the following.

19.5.1.1 Attending#

The first step is to align tokens in one text sequence to each token in the other sequence. Suppose that the premise is "i do need sleep" and the hypothesis is "i am tired". Due to semantical similarity, we may wish to align "i" in the hypothesis with "i" in the premise, and align "tired" in the hypothesis with "sleep" in the premise. Likewise, we may wish to align "i" in the premise with "i" in the hypothesis, and align "need" and "sleep" in the premise with "tired" in the hypothesis. Note that such alignment is soft using weighted average, where ideally large weights are associated with the tokens to be aligned. For ease of demonstration, Figure 19.5.2 shows such alignment in a hard way.

Now we describe the soft alignment using attention mechanisms in more detail. Denote by A=(a1,…,am)\mathbf{A} = (\mathbf{a}_1, \ldots, \mathbf{a}_m) and B=(b1,…,bn)\mathbf{B} = (\mathbf{b}_1, \ldots, \mathbf{b}_n) the premise and hypothesis, whose number of tokens are mm and nn, respectively, where ai,bj∈Rd\mathbf{a}_i, \mathbf{b}_j \in \mathbb{R}^{d} (i=1,…,m,j=1,…,ni = 1, \ldots, m, j = 1, \ldots, n) is a dd-dimensional word vector. For soft alignment, we compute the attention weights eij∈Re_{ij} \in \mathbb{R} as

eij=f(ai)⊤f(bj),(19.5.1)e_{ij} = f(\mathbf{a}_i)^\top f(\mathbf{b}_j), \tag{19.5.1}

where the function ff is an MLP defined in the following mlp function. The output dimension of ff is specified by the num_hiddens argument of mlp.

def mlp(num_inputs, num_hiddens, flatten):
    net = []
    net.append(nn.Dropout(0.2))
    net.append(nn.Linear(num_inputs, num_hiddens))
    net.append(nn.ReLU())
    if flatten:
        net.append(nn.Flatten(start_dim=1))
    net.append(nn.Dropout(0.2))
    net.append(nn.Linear(num_hiddens, num_hiddens))
    net.append(nn.ReLU())
    if flatten:
        net.append(nn.Flatten(start_dim=1))
    return nn.Sequential(*net)

It should be highlighted that, in (19.5.1) ff takes inputs ai\mathbf{a}_i and bj\mathbf{b}_j separately rather than taking a pair of them together as input. This decomposition trick leads to only m+nm + n applications (linear complexity) of ff rather than mnmn applications (quadratic complexity).

Normalizing the attention weights in (19.5.1), we compute the weighted average of all the token vectors in the hypothesis to obtain representation of the hypothesis that is softly aligned with the token indexed by ii in the premise:

βi=∑j=1nexp⁡(eij)∑k=1nexp⁡(eik)bj.\boldsymbol{\beta}_i = \sum_{j=1}^{n}\frac{\exp(e_{ij})}{ \sum_{k=1}^{n} \exp(e_{ik})} \mathbf{b}_j.

Likewise, we compute soft alignment of premise tokens for each token indexed by jj in the hypothesis:

αj=∑i=1mexp⁡(eij)∑k=1mexp⁡(ekj)ai.\boldsymbol{\alpha}_j = \sum_{i=1}^{m}\frac{\exp(e_{ij})}{ \sum_{k=1}^{m} \exp(e_{kj})} \mathbf{a}_i.

Below we define the Attend class to compute the soft alignment of hypotheses (beta) with input premises A and soft alignment of premises (alpha) with input hypotheses B.

class Attend(nn.Module):
    def __init__(self, num_inputs, num_hiddens, **kwargs):
        super(Attend, self).__init__(**kwargs)
        self.f = mlp(num_inputs, num_hiddens, flatten=False)

    def forward(self, A, B):
        # Shape of `A`/`B`: (`batch_size`, no. of tokens in sequence A/B,
        # `embed_size`)
        # Shape of `f_A`/`f_B`: (`batch_size`, no. of tokens in sequence A/B,
        # `num_hiddens`)
        f_A = self.f(A)
        f_B = self.f(B)
        # Shape of `e`: (`batch_size`, no. of tokens in sequence A,
        # no. of tokens in sequence B)
        e = torch.bmm(f_A, f_B.permute(0, 2, 1))
        # Shape of `beta`: (`batch_size`, no. of tokens in sequence A,
        # `embed_size`), where sequence B is softly aligned with each token
        # (axis 1 of `beta`) in sequence A
        beta = torch.bmm(F.softmax(e, dim=-1), B)
        # Shape of `alpha`: (`batch_size`, no. of tokens in sequence B,
        # `embed_size`), where sequence A is softly aligned with each token
        # (axis 1 of `alpha`) in sequence B
        alpha = torch.bmm(F.softmax(e.permute(0, 2, 1), dim=-1), A)
        return beta, alpha

19.5.1.2 Comparing#

In the next step, we compare a token in one sequence with the other sequence that is softly aligned with that token. Note that in soft alignment, all the tokens from one sequence, though with probably different attention weights, will be compared with a token in the other sequence. For ease of demonstration, Figure 19.5.2 pairs tokens with aligned tokens in a hard way. For example, suppose that the attending step determines that "need" and "sleep" in the premise are both aligned with "tired" in the hypothesis, the pair "tired--need sleep" will be compared.

In the comparing step, we feed the concatenation (operator [⋅,⋅][\cdot, \cdot]) of tokens from one sequence and aligned tokens from the other sequence into a function gg (an MLP):

vA,i=g([ai,βi]),i=1,…,mvB,j=g([bj,αj]),j=1,…,n.(19.5.2)\begin{aligned} \mathbf{v}_{A,i} &= g([\mathbf{a}_i, \boldsymbol{\beta}_i]), i = 1, \ldots, m\\ \mathbf{v}_{B,j} &= g([\mathbf{b}_j, \boldsymbol{\alpha}_j]), j = 1, \ldots, n. \end{aligned} \tag{19.5.2}

In (19.5.2), vA,i\mathbf{v}_{A,i} is the comparison between token ii in the premise and all the hypothesis tokens that are softly aligned with token ii; while vB,j\mathbf{v}_{B,j} is the comparison between token jj in the hypothesis and all the premise tokens that are softly aligned with token jj. The following Compare class defines such a comparing step.

class Compare(nn.Module):
    def __init__(self, num_inputs, num_hiddens, **kwargs):
        super(Compare, self).__init__(**kwargs)
        self.g = mlp(num_inputs, num_hiddens, flatten=False)

    def forward(self, A, B, beta, alpha):
        V_A = self.g(torch.cat([A, beta], dim=2))
        V_B = self.g(torch.cat([B, alpha], dim=2))
        return V_A, V_B

19.5.1.3 Aggregating#

With two sets of comparison vectors vA,i\mathbf{v}_{A,i} (i=1,…,mi = 1, \ldots, m) and vB,j\mathbf{v}_{B,j} (j=1,…,nj = 1, \ldots, n) on hand, in the last step we will aggregate such information to infer the logical relationship. We begin by summing up both sets:

vA=∑i=1mvA,i,vB=∑j=1nvB,j.\mathbf{v}_A = \sum_{i=1}^{m} \mathbf{v}_{A,i}, \quad \mathbf{v}_B = \sum_{j=1}^{n}\mathbf{v}_{B,j}.

Next we feed the concatenation of both summarization results into function hh (an MLP) to obtain the classification result of the logical relationship:

y^=h([vA,vB]).\hat{\mathbf{y}} = h([\mathbf{v}_A, \mathbf{v}_B]).

The aggregation step is defined in the following Aggregate class.

class Aggregate(nn.Module):
    def __init__(self, num_inputs, num_hiddens, num_outputs, **kwargs):
        super(Aggregate, self).__init__(**kwargs)
        self.h = mlp(num_inputs, num_hiddens, flatten=True)
        self.linear = nn.Linear(num_hiddens, num_outputs)

    def forward(self, V_A, V_B):
        # Sum up both sets of comparison vectors
        V_A = V_A.sum(dim=1)
        V_B = V_B.sum(dim=1)
        # Feed the concatenation of both summarization results into an MLP
        Y_hat = self.linear(self.h(torch.cat([V_A, V_B], dim=1)))
        return Y_hat

19.5.1.4 Putting It All Together#

By putting the attending, comparing, and aggregating steps together, we define the decomposable attention model to jointly train these three steps.

class DecomposableAttention(nn.Module):
    def __init__(self, vocab, embed_size, num_hiddens, num_inputs_attend=100,
                 num_inputs_compare=200, num_inputs_agg=400, **kwargs):
        super(DecomposableAttention, self).__init__(**kwargs)
        self.embedding = nn.Embedding(len(vocab), embed_size)
        self.attend = Attend(num_inputs_attend, num_hiddens)
        self.compare = Compare(num_inputs_compare, num_hiddens)
        # There are 3 possible outputs: entailment, contradiction, and neutral
        self.aggregate = Aggregate(num_inputs_agg, num_hiddens, num_outputs=3)

    def forward(self, X):
        premises, hypotheses = X
        A = self.embedding(premises)
        B = self.embedding(hypotheses)
        beta, alpha = self.attend(A, B)
        V_A, V_B = self.compare(A, B, beta, alpha)
        Y_hat = self.aggregate(V_A, V_B)
        return Y_hat

19.5.2 Training and Evaluating the Model#

Now we will train and evaluate the defined decomposable attention model on the SNLI dataset. We begin by reading the dataset.

19.5.2.1 Reading the dataset#

We download and read the SNLI dataset using the function defined in Section 19.4. Most implementations use minibatches of 256256 examples padded or truncated to 5050 tokens. The JAX implementation uses minibatches of 512512 and length 3232 to avoid spending most of its time on padding. With the tokenization performed by read_snli, about 1.2% of the labeled training premises and 0.02% of the hypotheses exceed 32 tokens. This substantially reduces computation while introducing little additional truncation.

batch_size, num_steps = 256, 50
train_iter, test_iter, vocab = d2l.load_data_snli(batch_size, num_steps)
read 549367 examples
read 9824 examples

19.5.2.2 Creating the Model#

We use the pretrained 100-dimensional GloVe embedding to represent the input tokens. Thus, we predefine the dimension of vectors ai\mathbf{a}_i and bj\mathbf{b}_j in (19.5.1) as 100. The output dimension of functions ff in (19.5.1) and gg in (19.5.2) is set to 200. Then we create a model instance, initialize its parameters, and load the GloVe embedding to initialize vectors of input tokens.

embed_size, num_hiddens, devices = 100, 200, d2l.try_all_gpus()
net = DecomposableAttention(vocab, embed_size, num_hiddens)
glove_embedding = d2l.TokenEmbedding('glove.6b.100d')
embeds = glove_embedding[vocab.idx_to_token]
net.embedding.weight.data.copy_(embeds);

19.5.2.3 Training and Evaluating the Model#

Now we can train and evaluate the model on the SNLI dataset.

lr, num_epochs = 0.001, 4
trainer = torch.optim.Adam(net.parameters(), lr=lr)
loss = nn.CrossEntropyLoss(reduction="none")
d2l.train_ch13(net, train_iter, test_iter, loss, trainer, num_epochs, devices)
loss 0.495, train acc 0.806, test acc 0.821
33529.2 examples/sec on [device(type='cuda', index=0)]

19.5.2.4 Using the Model#

Finally, define the prediction function to output the logical relationship between a pair of premise and hypothesis.

def predict_snli(net, vocab, premise, hypothesis):
    """Predict the logical relationship between the premise and hypothesis."""
    net.eval()
    premise = torch.tensor(vocab[premise], device=d2l.try_gpu())
    hypothesis = torch.tensor(vocab[hypothesis], device=d2l.try_gpu())
    label = torch.argmax(net([premise.reshape((1, -1)),
                           hypothesis.reshape((1, -1))]), dim=1)
    return 'entailment' if label == 0 else 'contradiction' if label == 1 \
            else 'neutral'

We can use the trained model to obtain the natural language inference result for a sample pair of sentences.

predict_snli(net, vocab, ['he', 'is', 'good', '.'], ['he', 'is', 'bad', '.'])
'contradiction'

19.5.3 Summary#

  • The decomposable attention model consists of three steps for predicting the logical relationships between premises and hypotheses: attending, comparing, and aggregating.
  • With attention mechanisms, we can align tokens in one text sequence to every token in the other, and vice versa. Such alignment is soft using weighted average, where ideally large weights are associated with the tokens to be aligned.
  • The decomposition trick leads to a more desirable linear complexity than quadratic complexity when computing attention weights.
  • We can use pretrained word vectors as the input representation for downstream natural language processing tasks such as natural language inference.

19.5.4 Exercises#

  1. Train the model with other combinations of hyperparameters. Can you get better accuracy on the test set?
  2. What are major drawbacks of the decomposable attention model for natural language inference?
  3. Show that the output of DecomposableAttention does not change when the tokens of the premise are permuted, or when those of the hypothesis are permuted (with dropout turned off). Confirm it numerically by comparing the outputs of the trained net for a premise and for its reversal. Then write a premise and two hypotheses that consist of the same words in different orders but should receive different labels, and state which change to the model would let it tell them apart.
  4. Attend normalizes the attention weights over all num_steps positions, padding included, and Aggregate sums the comparison vectors over the padding positions as well, so the prediction for a pair depends on how much padding it carries. Take 1000 test pairs and report the fraction whose predicted label changes when they are fed without padding, as predict_snli does, instead of padded to num_steps. Then mask the padding: compute the valid lengths of premises and hypotheses by counting the positions that differ from vocab['<pad>'], replace the softmax operations in Attend by a masked softmax that gives the padding zero weight (Section 10.2), and set the comparison vectors of padding positions to zero before Aggregate sums them. Retrain the model and report the same fraction and the test accuracy for both models.
  5. Suppose that we want to get the level of semantical similarity (e.g., a continuous value between 0 and 1) for any pair of sentences. How shall we collect and label the dataset? Can you design a model with attention mechanisms?

Discussions