Neural networks are surprisingly good at interpolating and perform remarkably well when the training set examples resemble those in the test set. However, they are often unable to extrapolate patterns beyond the seen data, even when the abstractions required for such patterns are simple. In this paper, we first review the notion of extrapolation, why it is important, and how one could hope to tackle it. We then focus on a specific type of extrapolation, which is especially useful for natural language processing: generalization to sequences longer than those seen during training. We hypothesize that models with a separate content-and location-based attention are more likely to extrapolate than those with common attention mechanisms. We empirically support our claim for recurrent seq2seq models with our proposed attention on variants of the Lookup Table task. This sheds light on some striking failures of neural models for sequences and on possible methods to approaching such issues. It is indisputable that, in recent years, neural network research has made stunning progress on a wide variety of tasks that require to process sequential inputs, such as machine translation (Sutskever et al., 2014) and speech recognition (Graves et al., 2013). However, many researchers have questioned the forms of generalization that neural networks exhibit, which significantly diverges from human-like generalization (Lake and Baroni, 2017; Geirhos et al., 2018). This discrepancy with human-like generalization is particularly true when it comes to extrapolating outside the training space (DeLosh et al., 1997; Marcus, 1998). statistical cues (Jo and Bengio, 2017), testing extrapolation and generalization to samples from the long tails of a distribution might be the only way of quantifying their capacity of abstract reasoning (Santoro et al., 2018). Despite this benefit, little work has been done in extrapolation. A possible explanation is that the probability of encountering a test example in the extrapolation setting seems low when the training set D is large. 1 However, such an argument fails to consider the high cost of error in extrapolation settings, and this can be a barrier for real-world scenarios (e.g., self-driving cars). In this paper, we focus on extrapolation in sequences. More precisely, how to generalize sequence-to-sequence predictors to inputs of length n  > n D , where n D denotes the length of the longest sequence in the training set. Such extrapolation is crucial for language acquisition, where humans have limited learning resources to account for the unbounded nature of language. To successfully generalize, a language learner needs to process new and potentially longer sentences than previously encountered ones (Chomsky, 1956). Accounting for this unbounded nature of language is challenging for neural networks. This issue has recently been uncovered for seq2seq models by looking at simple artificial tasks (Lake and Baroni, 2018; Liska et al., 2018; Weber et al., 2018). Liska et al. (2018) find that seq2seq architectures can converge to local minima that generalize, but rarely do. This suggests that neural networks could generalize but lack inductive biases that favor extrapolatable behavior. 1 Extrapolation is still prevalent in practical scenarios as high-dimensional problems would typically require an exponentially large D to be representative, and the underlying distribution may vary over time (Hooker, 2004). current attention mechanisms, which are mainly responsible for recent successes in natural language processing (NLP), are unlikely to extrapolate as they depend on the content of trained embeddings. This leads us to introduce a novel location -based attention that is loosely inspired by human visual attention. To avoid gaining extrapolation capabilities at the cost of expressivity, we introduce an attention mixer that combines contentand position-based attentions. Finally, we show that recurrent models equipped with this new attention mechanism can extrapolate to longer sequences. Extrapolation is often used but rarely formally de-fined. Ebert et al. (2014) have found that when extrapolation is explicitly defined, it often refers to points outside a hull delimited by the training set. E.g., rectangular hull, concave hull, or convex hull. In this work we use the rectangle hull definition (Brooks et al., 1988), as any model which is extrapolatable for this region would also be extrapolatable for the convex and concave definition. Given any finite training dataset D := { x ( n ) } Nn =1  R d , we define the interpolation domain to be the d -dimensional interval I inter := (cid:81) di =1 [min n x ( n ) i , max n x ( n ) i ] and the extrapolation domain its complement I extra := R d \ I inter . In other words, we define a test example x  to be in the extrapolation setting if at least one of its features x  j is larger or smaller than any values it took during training (Figure 1). Throughout this paper, we assume that neural networks with inputs or temporary representations in I extra will break. Indeed, for a given target function t : R d  R to approximate, there is an infinite amount of predictors that satisfy f ( x ) = t ( x ) ,  x  I inter  R d . Without any additional constraints, it is thus extremely unlikely that f ( x ) = t ( x ) ,  x  R d . This could explain why neural networks have empirically been found to break in extrapolation settings (Lohninger, 1999; Hettiarachchi et al., 2005; Mitchell et al., 2018). The rest of the paper discusses how to constrain representations used by our neural models 2 to be in I inter regardless of the source sentence length, without decreasing their expressivity. First and foremost, we would like a model that can extrapolate to sequences longer than the longest training one n D ( Extrapolation Constraint ). As previously discussed, models with inputs or temporary representations in I extra will very likely break. To satisfy the extrapolation constraint, neural models should thus not depend on features that take values in I extra for sequences longer than n D . Second, our model should be able to learn very complex positional attention patterns ( Positional Patterns Constraint ). Finally, although the position of words in a sentence is important, many tasks depend on their semantics. The model should thus still be able to learn content-based attention patterns ( Content Patterns Constraint ). In the following section, we review previously proposed attention-mechanism and discuss why they do not fulfill the three aforementioned desired properties. An attention mechanism (or attender) takes as input a matrix of keys K := { k Ts } n s s =1  R n s  d and a query q t  R d , and outputs a probability mass function  t  R n s that will weight a set of values V := { v Ts } n s s =1  R n s  d v to generate a glimpse vector g t  R d v used for downstream tasks. Following Graves et al. (2014), it is useful to think of the attender as a memory access module,  t as the soft address and g t as the accessed vector. Figure 2 illustrates attention in a recurrent seq2seq (Cho et al., 2014), which we will use for our experiments. Both the keys and the values correspond to the set of encoder hidden states 2 Although the sentence length is a scalar, the temporary representations (outputs of a hidden layer) are high dimensional. K = V = E := { e Ts } n s s =1 , while the query corresponds to the current decoder hidden state q t = d t . Most attention mechanisms compute content-based addressing (associative memory) that depend on (partial) matches of the key and query. They take as input K and q t and output a semantic-based attention  t  R n s . For example, if you wanted to translate a scientific paper, you could understand the main point of the text without remembering the specific technical terms that were used. When translating, you would go back to the text and translate the jargon by knowing what to look for. A number of content-based have been proposed, they usually differ in a score that quantifies the match between k s , q t through multinomial logits:  t := { softmax(score( k s , q t )) } n s  1 s =0 (2) score( k s , q t ) :=   u T tanh([ (cid:101) k s ; (cid:101) q t ]) Additive Bahdanau et al. (2015) k T s (cid:101) q t Multiplicative Luong et al. (2015) k Ts q t  d S. Dot Prod. Vaswani et al. (2017) (3) Where (cid:101) x is a shorthand for W x . A location (or position) attention mechanism computes location-based addressing (random access memory) that depend on the index of the key. It takes as input q t and outputs a location attention  t  R n s . Intuitively, it decides which value to retrieve based on its index. For example, in German sentences, the verb goes at the end of the sentence, after a subordinate clause. When translating from German to English, it might thus make sense to directly attend to the last word in the German source sentence after encoding a subordinate clause. There are many other cases where attending to words based on their positions seems important. E.g. translating from subject-object-verb to subject-verb-object languages, or understanding the emphasis in some languages. Despite the importance of word ordering in natural language, location-based attention is not common in seq2seq frameworks. This is probably because content-based attention can emulate location-based attention in the usual interpolation setting. Indeed, it can learn to encode a positional embedding in the hidden states of the encoder through some internal counter. This counter is unlikely to work in the extrapolation regime, 3 we, therefore, investigate other types of location-attention that could satisfy the extrapolation constraint. Luong et al. (2015) proposed a location-based attention by using Equation 2 with a score that is independent of the key score( k s , q t ) = w T q t . They restrict themselves to sequences of the same length, which is not of interest to our work. Such a mechanism could be extended to sequences of varying lengths but would still lack extrapolation capacity as the model still has to learn to embed the location of the index it wants to retrieve. The Neural Turing Machine (Graves et al., 2014), post-processes the content attention by shifting its location by a predicted number of steps. We use a similar mechanism, which is extrapolatable due to the independence of the sequence length. Nevertheless, on its own, it does not allow positional-only patterns in variable-length sentences. For example, it cannot attend to the i th word irrespective of the sentence length. The same argument holds for other location-based attention developed for architectures with an external memory (Sukhbaatar et al., 2015). More recently, many location-based attention have been proposed in self-attention mechanism. These methods are usually based on sinusoidal encodings (SE), which have been proposed to take into account the word positions while bypassing the need for recurrences in encoder-decoder frameworks. In this paper, we will consider the transformer and transformerXL (relative SE) attention, 3 This assumption can depend on the architecture and the inductive bias it provides (Weiss et al., 2018). For our task, we found that the assumption held for both LSTM and GRU. Transformer (Vaswani et al., 2017) Where p t is a positional encoding with sinu-soidals of different frequencies at every dimension. Although powerful, the sinusoidal encoding and its variants (Shaw et al., 2018; Dai et al., 2019) lack the ability to model location patterns that depend on general word position such as look at the i th word (after ...) in the extrapolation setting. Indeed, the sinusoidal encoding for any fixed offset p t + k is linear in p t but not in k . Location-based processing of attention has also been proposed as a way of constraining content-based attention to some (soft) window. Yang et al. (2018) achieve it by multiplying the content attention by the weights of a predicted Gaussian such that the model has an inductive bias towards attending to words that are close to each other. Sukhbaatar et al. (2019) use a piece-wise window to decrease the computational complexity of the model. These methods nevertheless solve a fundamentally different problem and do not allow location-only extrapolatable patterns of attention. In this section, we propose a location attender that can satisfy the extrapolation and positional patterns constraint. We then discuss how to incorporate content attention to satisfy the content patterns constraint. We would like our position attention to be loosely reminiscent of human attention, whereby we sequentially focus on a single area of the input (e.g., words or pixels) but vaguely perceive neighboring inputs due to the eccentricity effect (Carrasco et al., 1995). The visual acuity of humans is uni-modal, symmetric, and spikes at the fovea, which corresponds to a 0  retinal eccentricity. We model this visual acuity using a Gaussian Probability Density Function (PDF) similarly to Mnih et al. (2014). 4 4 Visual acuity is distributed in a Laplace-like distribution, but initial experiments were more encouraging using a Gaussian. I.e. for each step, the Location Attender models a Gaussian attention over the relative word positions. Specifically, it generates a mean  t and standard deviation  t , which are used to compute the location attention given the values of the PDFs at the relative indices r s := s n s  1 of the keys:  t := { PDF  t , t ( r s ) } n s  1 s =0 Using relative indices r s instead of the absolute ones s is crucial such that the generated  t is bounded (in [0 , 1] ), thereby satisfying the extrapolation constraint. This model, unfortunately, fails to satisfy the positional patterns constraint, as it only allows patterns of attention based on percentile positions. E.g., it can decide to attend to the 10%-percentile word but not to the 2 nd word. This incapacity to satisfy the position pattern constraint is a general issue with commonly used attention mechanisms (including sinusoidal-based) that only becomes apparent when dealing with complex extrapolation patterns. To have a general attention mechanism, we need a  t that can: i) attend to locations based on absolute positions; ii) attend to locations based on percentile positions; iii) attend to positions based on the previous attention. We achieve this by defining one building block for each of those requirements ( b t ) such that their weighted average forms  t , and the weights  t are bounded outputs of the model. The three building blocks are:  The step size 1 n s  1 between words allows the attention mechanism to depend on absolute positions. The generated weight is an integer, which dictates the additional number of steps to take. The bias term 1 enables the model to use percentile positions. The generated weight gates it (on or off). The average position of the previous attention   t  1 that is gated by the generated weight. This ensures that the model can attend using absolute positions to words at indices not seen during training. E.g., attending to index n D + 5 by first attending to n D then   t  1 + 5 . The weights  t are generated using a Gated Recurrent Unit (GRU) (Cho et al., 2014). t is clamped to [0 , 1] by a linear function to yield interpretable and extrapolatable behaviour. We also Figure 3: Proposed Location Attender. force  t > min  and normalize it by n s which respectively avoids division by 0 and makes  t comparable regardless of n s . A graphical overview of the Location Attender can be seen in Figure 3. Formally:  t := GRU (cid:16) ReLU (cid:16) W ( resize ) q t (cid:17)(cid:17)  t := ReLU(W (  )  t ) + min  n s  t := a (W (  )  t ) b t := {   t  1 ; 1 n s  1; 1 }  t := clamp(  Tt b t )  st := 1 (cid:112) 2  2 t exp (cid:32)  ( s n s  1   t ) 2 2  2 t (cid:33) Where clamp is a leaky clamping (2 leaky ReLUs) and min  = 0 . 27 . a is the activation function that forces each of the three dimensions of  t to take on the desired values. Namely a sigmoid activation for the gates, and the following soft-staircase 5 to force the weights of the step size to be approximately integers (Figure 4): softstair ( x ) := (cid:98) x (cid:99) +sigmoid(20( x  0 . 5 (cid:98) x (cid:99) )) 5.2 Mix Attender We enforce the content patterns constraint, by using a convex combination of content and location attention (Figure 5):  t := % (  ) t  t + (1  % (  ) t )  t % (  ) t := sigmoid(W (%) q t ) 5 Straight-through estimators (Bengio et al., 2013) and Gumbel-Softmax (Jang et al., 2017; Maddison et al., 2017) performed slightly worst and required predefining the maximum number of steps. The fact that humans generate and understand unbounded sentences with a finite experience is often used as proof of the principle of compositionality (Szab, 2017). Following this argument, methods that can extrapolate to longer sequences should exhibit some compositionality. Based on this observation, we evaluate on a compositionality-specific artificial task, lookup tables (Liska et al., 2018), but extend it to better quantify extrapolation. 6 This task is especially interesting to us, as there is a clear notion of what a good attention pattern should look like, making it easy to qualitatively and quantitatively analyze attentive models. It is a well-controlled task, which allows us to uncover challenges that prevent models from extrapolating on real-world data. The lookup tables task consists in sequentially applying k pre-defined lookup table functions. The lookup tables are bijective mappings on the set of 6 The extended datasets as well as scripts to generate them can be found at https://github.com/ i-machine-think/machine-tasks/tree/master/LongLookupTables Input Target Target Attention 000 t1 . all 3-bit strings t i : { 0 , 1 } 3  { 0 , 1 } 3 . For example, if t 1 (000) = 110 and t 2 (110) = 100 then t 2 ( t 1 (000)) = t 2 (110) = 100 . Following Hupkes et al. (2018), we write the operations from left to right, as well as add the inputs and temporary steps to the targets. E.g. the previous example corresponds to the input 000 t1 t2 and the target 000 110 100 . General extrapolatable seq2seq models should be able to terminate by outputting an end of sentence token <eos> . We thus append <eos> to the targets and a full stop . to the inputs. 7 At each decoding step, the target only depends on the previous output and the current lookup table. E.g. the last decoding step of 000 t1 t2 , only depends on the previous output 110 = t 1 (000) and the current table t 2 . The network thus has to learn the lookup table mappings and use the correct one at each step. The gold standard attention, therefore, corresponds to the position of the current lookup table. Table 1 illustrates a longer example and its correct attention. The various train and test sets are generated by composing 6 random lookup tables t 1 , . . . , t 6 that have as input and output one of the 2 3 = 8 possible 3-bit strings. Specifically, we use k = 1 . . . 4 composed tables in the training set, k = 2 . . . 4 for the interpolation test sets, and k = 5 . . . 9 for the extrapolation test sets. There are 5 different extrapolation test sets, depending on their additional lengths compared to the maximum training examples ( long 1 , . . . , long 5 ). We randomly select only 5000 possible examples for each of these test sets. For the interpolation test sets, we select 3000 examples from all possible input-output pairs. The training set contains all other possible input-output pairs, approximately 10000 examples. To test whether the attention can generate more complex patterns (investigating the Positional Patterns Constraint), we also introduce a dataset which reverses the order of the inputs in the previous dataset. E.g. the last example in Table 1, would be written as t2 t1 t1 000 . , the target would not change, and the attention pattern should be 3 2 1 0 4 (attend to . when outputting <eos> ). Although the change seems minor, we hypothesize that such a setting will be much more complicated as the attention pattern is not monotonic and does not follow the encoding nor the decoding steps. Indeed, in the previous task, the model only needs to learn to match the i th decoding step with the i th encoding step. Finally, we introduce another variant that also requires content attention (investigating the Content Patterns Constraint). To do so, we augment each training example with a start token  !  between the input and the tables in the source sequence. We then add m  U{ 0 , 10 } tables t i before the start token. The target outputs were not modified and are thus independent of the added tables. Solving this task requires to first attend to the input, then to the token which follows  !  (content attention) and finally proceed with incremental location attention. Examples of the training data are given in Table 2. The main metric is sequence accuracy ( seqAcc ), which corresponds to the accuracy of predicting the entire sequence correctly (including its length). To get insights about how the model works, we will also use two other losses. Sequence Accuracy Before Eos ( seqAccBE ), which only evaluates the accuracy of the subsequence before the model generated a <eos> . Attention Loss ( attnLoss ), which quantifies the quality of the attention pattern before <eos> . It is computed as the mean squared error between the predicted and gold standard attention. 8 The attention loss gives an indication of how far the 8 The loss is overly simplistic as it is symmetric around   t even though errors in the temporal direction are less serious as the embeddings contain past information. Concerning baselines, we use three content attention: additive, multiplicative, scaled dot product (Eq.3). We also have two mixed content-location attention baselines: Transformer and TransformerXL (Eq.4). To focus on the attention mechanisms, our model and the baselines all use a smaller version of the best performing recurrent seq2seq architecture on the lookup table task (Hupkes et al., 2018). The model has never been modified during our experimentation and is schematized in Figure 2. The embeddings are of dimension 64 , the recurrent network is a GRU (Cho et al., 2014) with a hidden size of 128 , 50% dropout (Srivastava et al., 2014) is applied on the encoder-decoder bottleneck, and a residual connection is used between the inputs (embeddings) and outputs of the encoder. Training consists of 50 epochs with the Adam (Kingma and Ba, 2015) optimizer. For sanity check, we tested all the baselines and our models (with and without attention mix) on the interpolation setting of the three tasks. Our models and the best baseline (transformer attention) achieved 100% sequence accuracy ( seqAcc ). The major desired property of our model is to be able to extrapolate. We tested the extrapolation capacity of our location attender by evaluating its seqAcc on the long lookup table extrapolation test sets. Figure 6 shows the seqAcc of the location attender against the strongest baseline (transformer attention). As hypothesized, the transformer attention has some extrapolation capacity, but our location attender substantially outperforms it in this simple task. Importantly, the loss in performance in the extrapolation setting for the best baseline is abrupt and goes from 100% to 0% by adding only three tokens to the inputs. This suggests that commonly used models are brittle and cannot even extrapolate by a small amount. To do so, we computed the sequence accuracy before <eos> ( SeqAccBE ). Figure 7 shows that the model outputs are always correct but that it often terminates decoding too soon, which we will refer to as the <eos> problem . This suggests that the decoder keeps an internal counter to increase the probability of outputting <eos> when the decoding step is greater than the ones seen at training time. The model learns this heuristic, which is always correct during training time and can be thought of as a metric hacking. Importantly, it is not a hard boundary: the model is often able to extrapolate a couple of steps but usually stops before the correct number of steps. Having shown that our model can extrapolate well on a simple task, we would like to investigate whether it can do so for tasks that require more complicated attention patterns such as the reversed and noisy task. Although the Mix Attender, outperformed all baselines on both tasks, it was not able to get more than 40% and 5% sequence accuracy for long 1 and long 2 respectively. Figure 8 shows that when considering seqAccBE , the Mix Attender is able to extrapolate well in the noisy setting and a little in the reverse setting. This suggests that it is not able to extrapolate well when considering sequence accuracy because it strongly suffers from the <eos> problem. This is a recurrent problem in our experiments and is more likely to happen in harder tasks and larger models. As previously discussed, variants of the lookup table task are especially interesting as we know the gold standard attention pattern. This enables evaluation of attention patterns through the MSE attention loss ( attnLoss ). Table 3 shows the attention loss averaged over the three tasks. Although not perfect, the Mix Attender performs on average the best across all set-Attention Interp. tings. 9 Crucially, it performs similarly in an interpolation setting and simple extrapolation setting ( long 1 ), while all other baselines perform significantly worse after adding a single token. Even in long 2 , it is competitive with all other attention mechanisms in their interpolation domain. This indicates that the model is indeed able to extrapolate by being more precise with its attention pattern. In addition to enabling extrapolation, the temporary variables such as the weight given to each building block are very helpful for debugging the model and improving interpretability. Figure 9 shows the output of a Mix Attender for the lookup tables with noisy start task. The input was sampled from the Long 4 test set. The top-left image shows the final attention. The top-right table shows the value of some interpretable variables at every decoding step. The bottom images correspond to the content and location attention. The first decoding step uses location attention to attend to the first input. For the next three steps, the model outputs a mixing weight % (  )  0 to focus on content attention. The content attention successfully finds the first non-noisy table (after ! ). 10 It then goes back to using the location attention with  (  ) = 1 and  (1 /n ) = 1 to generate a diagonal attention. Finally, it predicts <eos> when attending to the end of the input  . . At each step,  = min  as it does not need to attend to neighboring words for this task. % (  ) is never exactly 0 or 1, such that the model can easily learn to switch between content and location attention as it does not collapse to using a single form of attention. A single step of content attention should be sufficient, but the model seems to consistently use three steps. In this paper, we focused on one type of extrapolation, which is especially important in NLP: generalization to longer sequences. We propose a new location-based attention, and show that it can extrapolate better than previous models while learning various attention patterns. Despite promising initial results, our model is still unable to extrapolate perfectly for harder tasks. By analyzing its behavior, we uncovered an interesting heuristic used by seq2seq models, namely that they keep track of a decoding counter to know when to output the <eos> token. This is a bottleneck for extrapolation, suggesting that removing this heuristic is key to reaching perfect extrapolation and should be investigated in future work. Once the <eos> problem is solved, we could test the model on real-world datasets. It would also be interesting to test such attention mechanisms in self-attentive seq2seq models without recurrence. Finally, as the location attender is not model dependent, it could be pretrained on complex location patterns and incorporated as a plug-and-play module to get extrapolatable position attention. Taking a step back, we have shown that current deep learning models with common attention mechanisms are unable to extrapolate well on seemingly straightforward tasks. This tends to be overlooked by the field due to standard benchmarks that can be solved using only interpolation. We hope that this paper acts as a reminder that extrapolation is a hard setting that has not been much investigated by the machine learning community. As current methods that memorize and learn superficial cues are unable to extrapolate while humans are, we believe that such a setting might help (and force) the field to come up with more human-like computational models that are capable of abstract reasoning. Dieuwke Hupkes is funded by the Netherlands Organization for Scientific Research (NWO), through a Gravitation Grant 024.001.006 to the Language in Interaction Consortium. Elia Bruni is funded by the European Unions Horizon 2020 research and innovation program under the Marie Sklodowska-Curie grant agreement No 790369 (MAGIC).
abstain abstain objective result objective objective abstain abstain abstain abstain abstain abstain abstain abstain method abstain abstain abstain abstain abstain abstain abstain abstain abstain objective method objective abstain abstain abstain method method method method abstain abstain abstain method abstain abstain abstain abstain abstain abstain abstain other abstain method abstain abstain abstain abstain abstain abstain other abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain method abstain abstain abstain abstain abstain method abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain method abstain method abstain abstain other abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain other abstain abstain abstain method abstain abstain abstain abstain abstain abstain other abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain result abstain abstain abstain abstain abstain abstain method abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain method abstain result abstain abstain abstain abstain abstain result abstain abstain method other other