Sequence-to-Sequence Learning as Beam-Search Optimization

Sam Wiseman, Alexander M. Rush

Introduction

Sequence-to-Sequence learning with deep neural networks (herein, seq2seq) [Sutskever et al. (2011, Sutskever et al. (2014] has rapidly become a very useful and surprisingly general-purpose tool for natural language processing. In addition to demonstrating impressive results for machine translation [Bahdanau et al. (2015], roughly the same model and training have also proven to be useful for sentence compression [Filippova et al. (2015], parsing [Vinyals et al. (2015], and dialogue systems [Serban et al. (2016], and they additionally underlie other text generation applications, such as image or video captioning [Venugopalan et al. (2015, Xu et al. (2015].

The dominant approach to training a seq2seq system is as a conditional language model, with training maximizing the likelihood of each successive target word conditioned on the input sequence and the gold history of target words. Thus, training uses a strictly word-level loss, usually cross-entropy over the target vocabulary. This approach has proven to be very effective and efficient for training neural language models, and seq2seq models similarly obtain impressive perplexities for word-generation tasks.

Notably, however, seq2seq models are not used as conditional language models at test-time; they must instead generate fully-formed word sequences. In practice, generation is accomplished by searching over output sequences greedily or with beam search. In this context, ?) note that the combination of the training and generation scheme just described leads to at least two major issues:

Exposure Bias: the model is never exposed to its own errors during training, and so the inferred histories at test-time do not resemble the gold training histories.

Loss-Evaluation Mismatch: training uses a word-level loss, while at test-time we target improving sequence-level evaluation metrics, such as BLEU [Papineni et al. (2002].

We might additionally add the concern of label bias [Lafferty et al. (2001] to the list, since word-probabilities at each time-step are locally normalized, guaranteeing that successors of incorrect histories receive the same mass as do the successors of the true history.

In this work we develop a non-probabilistic variant of the seq2seq model that can assign a score to any possible target sequence, and we propose a training procedure, inspired by the learning as search optimization (LaSO) framework of ?), that defines a loss function in terms of errors made during beam search. Furthermore, we provide an efficient algorithm to back-propagate through the beam-search procedure during seq2seq training.

This approach offers a possible solution to each of the three aforementioned issues, while largely maintaining the model architecture and training efficiency of standard seq2seq learning. Moreover, by scoring sequences rather than words, our approach also allows for enforcing hard-constraints on sequence generation at training time. To test out the effectiveness of the proposed approach, we develop a general-purpose seq2seq system with beam search optimization. We run experiments on three very different problems: word ordering, syntactic parsing, and machine translation, and compare to a highly-tuned seq2seq system with attention [Luong et al. (2015]. The version with beam search optimization shows significant improvements on all three tasks, and particular improvements on tasks that require difficult search.

Related Work

The issues of exposure bias and label bias have received much attention from authors in the structured prediction community, and we briefly review some of this work here. One prominent approach to combating exposure bias is that of SEARN [Daumé III et al. (2009], a meta-training algorithm that learns a search policy in the form of a cost-sensitive classifier trained on examples generated from an interpolation of an oracle policy and the model’s current (learned) policy. Thus, SEARN explicitly targets the mismatch between oracular training and non-oracular (often greedy) test-time inference by training on the output of the model’s own policy. DAgger [Ross et al. (2011] is a similar approach, which differs in terms of how training examples are generated and aggregated, and there have additionally been important refinements to this style of training over the past several years [Chang et al. (2015]. When it comes to training RNNs, SEARN/DAgger has been applied under the name “scheduled sampling” [Bengio et al. (2015], which involves training an RNN to generate the t + 1t\,{+}\,1’st token in a target sequence after consuming either the true tt’th token, or, with probability that increases throughout training, the predicted tt’th token.

Though technically possible, it is uncommon to use beam search when training with SEARN/DAgger. The early-update [Collins and Roark (2004] and LaSO [Daumé III and Marcu (2005] training strategies, however, explicitly account for beam search, and describe strategies for updating parameters when the gold structure becomes unreachable during search. Early update and LaSO differ primarily in that the former discards a training example after the first search error, whereas LaSO resumes searching after an error from a state that includes the gold partial structure. In the context of feed-forward neural network training, early update training has been recently explored in a feed-forward setting by ?) and ?). Our work differs in that we adopt a LaSO-like paradigm (with some minor modifications), and apply it to the training of seq2seq RNNs (rather than feed-forward networks). We also note that ?) apply maximum-violation training [Huang et al. (2012], which is similar to early-update, to a parsing model with recurrent components, and that ?) use beam-search in training a discriminative, locally normalized dependency parser with recurrent components.

Recently authors have also proposed alleviating exposure bias using techniques from reinforcement learning. ?) follow this approach to train RNN decoders in a seq2seq model, and they obtain consistent improvements in performance, even over models trained with scheduled sampling. As ?) note, LaSO is similar to reinforcement learning, except it does not require “exploration” in the same way. Such exploration may be unnecessary in supervised text-generation, since we typically know the gold partial sequences at each time-step. ?) use minimum risk training (approximated by sampling) to address the issues of exposure bias and loss-evaluation mismatch for seq2seq MT, and show impressive performance gains.

Whereas exposure bias results from training in a certain way, label bias results from properties of the model itself. In particular, label bias is likely to affect structured models that make sub-structure predictions using locally-normalized scores. Because the neural and non-neural literature on this point has recently been reviewed by ?), we simply note here that RNN models are typically locally normalized, and we are unaware of any specifically seq2seq work with RNNs that does not use locally-normalized scores. The model we introduce here, however, is not locally normalized, and so should not suffer from label bias. We also note that there are some (non-seq2seq) exceptions to the trend of locally normalized RNNs, such as the work of ?) and ?), who train LSTMs in the context of HMMs for speech recognition using sequence-level objectives; their work does not consider search, however.

Background and Notation

In the simplest seq2seq scenario, we are given a collection of source-target sequence pairs and tasked with learning to generate target sequences from source sequences. For instance, we might view machine translation in this way, where in particular we attempt to generate English sentences from (corresponding) French sentences. Seq2seq models are part of the broader class of “encoder-decoder” models [Cho et al. (2014], which first use an encoding model to transform a source object into an encoded representation x\boldsymbol{x}. Many different sequential (and non-sequential) encoders have proven to be effective for different source domains. In this work we are agnostic to the form of the encoding model, and simply assume an abstract source representation x\boldsymbol{x}.

Once the input sequence is encoded, seq2seq models generate a target sequence using a decoder. The decoder is tasked with generating a target sequence of words from a target vocabulary V\mathcal{V}. In particular, words are generated sequentially by conditioning on the input representation x\boldsymbol{x} and on the previously generated words or history. We use the notation w1:Tw_{1:{T}} to refer to an arbitrary word sequence of length TT, and the notation y1:Ty_{1:{T}} to refer to the gold (i.e., correct) target word sequence for an input x\boldsymbol{x}.

RNN decoders are typically trained to act as conditional language models. That is, one attempts to model the probability of the tt’th target word conditioned on x\boldsymbol{x} and the target history by stipulating that p(wt∣w1:t−1,x) = g(wt,ht−1,x)p(w_{t}|w_{1:{t-1}},\boldsymbol{x})\,{=}\,g(w_{t},\boldsymbol{h}_{t-1},\boldsymbol{x}), for some parameterized function gg typically computed with an affine layer followed by a softmax. In computing these probabilities, the state ht−1\boldsymbol{h}_{t-1} represents the target history, and h0\boldsymbol{h}_{0} is typically set to be some function of x\boldsymbol{x}. The complete model (including encoder) is trained, analogously to a neural language model, to minimize the cross-entropy loss at each time-step while conditioning on the gold history in the training data. That is, the model is trained to minimize −ln⁡∏t=1Tp(yt∣y1:t−1,x)-\ln\prod_{t=1}^{T}p(y_{t}|y_{1:{t-1}},\boldsymbol{x}).

Beam Search Optimization

We begin by making one small change to the seq2seq modeling framework. Instead of predicting the probability of the next word, we instead learn to produce (non-probabilistic) scores for ranking sequences. Define the score of a sequence consisting of history w1:t−1w_{1:{t-1}} followed by a single word wtw_{t} as f(wt,ht−1,x)f(w_{t},\boldsymbol{h}_{t-1},\boldsymbol{x}), where ff is a parameterized function examining the current hidden-state of the relevant RNN at time t − 1t\,{-}\,1 as well as the input representation x\boldsymbol{x}. In experiments, our ff will have an identical form to gg but without the final softmax transformation (which transforms unnormalized scores into probabilities), thereby allowing the model to avoid issues associated with the label bias problem.

More importantly, we also modify how this model is trained. Ideally we would train by comparing the gold sequence to the highest-scoring complete sequence. However, because finding the argmax sequence according to this model is intractable, we propose to adopt a LaSO-like [Daumé III and Marcu (2005] scheme to train, which we will refer to as beam search optimization (BSO). In particular, we define a loss that penalizes the gold sequence falling off the beam during training.Using a non-probabilistic model further allows us to incur no loss (and thus require no update to parameters) when the gold sequence is on the beam; this contrasts with models based on a CRF loss, such as those of ?) and ?), though in training those models are simply not updated when the gold sequence remains on the beam. The proposed training approach is a simple way to expose the model to incorrect histories and to match the training procedure to test generation. Furthermore we show that it can be implemented efficiently without changing the asymptotic run-time of training, beyond a factor of the beam size KK.

We now formalize this notion of a search-based loss for RNN training. Assume we have a set StS_{t} of KK candidate sequences of length tt. We can calculate a score for each sequence in StS_{t} using a scoring function ff parameterized with an RNN, as above, and we define the sequence y^1:t(K) ∈ St\hat{y}_{1:{t}}^{({K})}\,{\in}\,S_{t} to be the KK’th ranked sequence in StS_{t} according to ff. That is, assuming distinct scores,

where y^t(k)\hat{y}_{t}^{(k)} is the tt’th token in y^1:t(k)\hat{y}_{1:{t}}^{({k})}, h^t−1(k)\hat{\boldsymbol{h}}_{t-1}^{(k)} is the RNN state corresponding to its t − 1t\,{-}\,1’st step, and where we have omitted the x\boldsymbol{x} argument to ff for brevity.

We now define a loss function that gives loss each time the score of the gold prefix y1:ty_{1:{t}} does not exceed that of y^1:t(K)\hat{y}_{1:{t}}^{({K})} by a margin:

Above, the Δ(y^1:t(K))\Delta(\hat{y}_{1:{t}}^{({K})}) term denotes a mistake-specific cost-function, which allows us to scale the loss depending on the severity of erroneously predicting y^1:t(K)\hat{y}_{1:{t}}^{({K})}; it is assumed to return 0 when the margin requirement is satisfied, and a positive number otherwise. It is this term that allows us to use sequence- rather than word-level costs in training (addressing the 2nd issue in the introduction). For instance, when training a seq2seq model for machine translation, it may be desirable to have Δ(y^1:t(K))\Delta(\hat{y}_{1:{t}}^{({K})}) be inversely related to the partial sentence-level BLEU score of y^1:t(K)\hat{y}_{1:{t}}^{({K})} with y1:ty_{1:{t}}; we experiment along these lines in Section 5.3.

Finally, because we want the full gold sequence to be at the top of the beam at the end of search, when t = Tt\,{=}\,T we modify the loss to require the score of y1:Ty_{1:{T}} to exceed the score of the highest ranked incorrect prediction by a margin.

We can optimize the loss L\mathcal{L} using a two-step process: (1) in a forward pass, we compute candidate sets StS_{t} and record margin violations (sequences with non-zero loss); (2) in a backward pass, we back-propagate the errors through the seq2seq RNNs. Unlike standard seq2seq training, the first-step requires running search (in our case beam search) to find margin violations. The second step can be done by adapting back-propagation through time (BPTT). We next discuss the details of this process.

2 Forward: Find Violations

In order to minimize this loss, we need to specify a procedure for constructing candidate sequences y^1:t(k)\hat{y}_{1:{t}}^{({k})} at each time step tt so that we find margin violations. We follow LaSO (rather than early-update We found that training with early-update rather than (delayed) LaSO did not work well, even after pre-training. Given the success of early-update in many NLP tasks this was somewhat surprising. We leave this question to future work.; see Section 2) and build candidates in a recursive manner. If there was no margin violation at t−1t{-}1, then StS_{t} is constructed using a standard beam search update. If there was a margin violation, StS_{t} is constructed as the KK best sequences assuming the gold history y1:t−1y_{1:t-1} through time-step t−1t{-}1.

Formally, assume the function succ⁡\operatorname{succ} maps a sequence w1:t−1 ∈ Vt−1w_{1:{t-1}}\,{\in}\,\mathcal{V}^{t-1} to the set of all valid sequences of length tt that can be formed by appending to it a valid word w ∈ Vw\,{\in}\,\mathcal{V}. In the simplest, unconstrained case, we will have

As an important aside, note that for some problems it may be preferable to define a succ⁡\operatorname{succ} function which imposes hard constraints on successor sequences. For instance, if we would like to use seq2seq models for parsing (by emitting a constituency or dependency structure encoded into a sequence in some way), we will have hard constraints on the sequences the model can output, namely, that they represent valid parses. While hard constraints such as these would be difficult to add to standard seq2seq at training time, in our framework they can naturally be added to the succ⁡\operatorname{succ} function, allowing us to train with hard constraints; we experiment along these lines in Section 5.3, where we refer to a model trained with constrained beam search as ConBSO.

Having defined an appropriate succ⁡\operatorname{succ} function, we specify the candidate set as:

where we have a margin violation at t−1t{-}1 iff f(yt−1,ht−2)<f(y^t−1(K),h^t−2(K))+1f(y_{t-1},\boldsymbol{h}_{t-2})<f(\hat{y}_{t-1}^{(K)},\hat{\boldsymbol{h}}_{t-2}^{(K)})+1, and where topK⁡\operatorname{topK} considers the scores given by ff. This search procedure is illustrated in the top portion of Figure 1.

In the forward pass of our training algorithm, shown as the first part of Algorithm 1, we run this version of beam search and collect all sequences and their hidden states that lead to losses.

3 Backward: Merge Sequences

Once we have collected margin violations we can run backpropagation to compute parameter updates. Assume a margin violation occurs at time-step tt between the predicted history y^1:t(K)\hat{y}_{1:{t}}^{({K})} and the gold history y1:ty_{1:{t}}. As in standard seq2seq training we must back-propagate this error through the gold history; however, unlike seq2seq we also have a gradient for the wrongly predicted history.

We informally illustrate the process in Figure 1. The top of the diagram shows a possible sequence of y^1:t(k)\hat{y}_{1:{t}}^{({k})} formed during search with a beam of size 3 for the target sequence y=y= “a red dog runs quickly today.” When the gold sequence falls off the beam at t = 4t\,{=}\,4, search resumes with S5 = succ⁡(y1:4)S_{5}\,{=}\,\operatorname{succ}(y_{1:{4}}), and so all subsequent predicted sequences have y1:4y_{1:{4}} as a prefix and are thus functions of h4\boldsymbol{h}_{4}. Moreover, because our loss function only involves the scores of the gold prefix and the violating prefix, we end up with the relatively simple computation tree shown at the bottom of Figure 1. It is evident that we can backpropagate in a single pass, accumulating gradients from sequences that diverge from the gold at the time-step that precedes their divergence. The second half of Algorithm 1 shows this explicitly for a single sequence, though it is straightforward to extend the algorithm to operate in batch.We also note that because we do not update the parameters until after the TT’th search step, our training procedure differs slightly from LaSO (which is online), and in this aspect is essentially equivalent to the “delayed LaSO update” of ?).

Data and Methods

We run experiments on three different tasks, comparing our approach to the seq2seq baseline, and to other relevant baselines.

While the method we describe applies to seq2seq RNNs in general, for all experiments we use the global attention model of ?) — which consists of an LSTM [Hochreiter and Schmidhuber (1997] encoder and an LSTM decoder with a global attention model — as both the baseline seq2seq model (i.e., as the model that computes the gg in Section 3) and as the model that computes our sequence-scores f(wt,ht−1,x)f(w_{t},\boldsymbol{h}_{t-1},\boldsymbol{x}). As in ?), we also use “input feeding,” which involves feeding the attention distribution from the previous time-step into the decoder at the current step. This model architecture has been found to be highly performant for neural machine translation and other seq2seq tasks.

To distinguish the models we refer to our system as BSO (beam search optimization) and to the baseline as seq2seq. When we apply constrained training (as discussed in Section 4.2), we refer to the model as ConBSO. In providing results we also distinguish between the beam size KtrK_{tr} with which the model is trained, and the beam size KteK_{te} which is used at test-time. In general, if we plan on evaluating with a beam of size KteK_{te} it makes sense to train with a beam of size Ktr=Kte + 1K_{tr}=K_{te}\,{+}\,1, since our objective requires the gold sequence to be scored higher than the last sequence on the beam.

2 Methodology

Here we detail additional techniques we found necessary to ensure the model learned effectively. First, we found that the model failed to learn when trained from a random initialization.This may be because there is relatively little signal in the sparse, sequence-level gradient, but this point requires further investigation. We therefore found it necessary to pre-train the model using a standard, word-level cross-entropy loss as described in Section 3. The necessity of pre-training in this instance is consistent with the findings of other authors who train non-local neural models [Kingsbury (2009, Sak et al. (2014, Andor et al. (2016, Ranzato et al. (2016].?) found, however, that pre-training only increased convergence-speed, but was not necessary for obtaining good results.

Similarly, it is clear that the smaller the beam used in training is, the less room the model has to make erroneous predictions without running afoul of the margin loss. Accordingly, we also found it useful to use a “curriculum beam” strategy in training, whereby the size of the beam is increased gradually during training. In particular, given a desired training beam size KtrK_{tr}, we began training with a beam of size 2, and increased it by 1 every 2 epochs until reaching KtrK_{tr}.

Finally, it has been established that dropout [Srivastava et al. (2014] regularization improves the performance of LSTMs [Pham et al. (2014, Zaremba et al. (2014], and in our experiments we run beam search under dropout.However, it is important to ensure that the same mask applied at each time-step of the forward search is also applied at the corresponding step of the backward pass. We accomplish this by pre-computing masks for each time-step, and sharing them between the partial sequence LSTMs.

For all experiments, we trained both seq2seq and BSO models with mini-batch Adagrad [Duchi et al. (2011] (using batches of size 64), and we renormalized all gradients so they did not exceed 5 before updating parameters. We did not extensively tune learning-rates, but we found initial rates of 0.02 for the encoder and decoder LSTMs, and a rate of 0.1 or 0.2 for the final linear layer (i.e., the layer tasked with making word-predictions at each time-step) to work well across all the tasks we considered. Code implementing the experiments described below can be found at https://github.com/harvardnlp/BSO.Our code is based on Yoon Kim’s seq2seq code, https://github.com/harvardnlp/seq2seq-attn.

3 Tasks and Results

Our experiments are primarily intended to evaluate the effectiveness of beam search optimization over standard seq2seq training. As such, we run experiments with the same model across three very different problems: word ordering, dependency parsing, and machine translation. While we do not include all the features and extensions necessary to reach state-of-the-art performance, even the baseline seq2seq model is generally quite performant.

The task of correctly ordering the words in a shuffled sentence has recently gained some attention as a way to test the (syntactic) capabilities of text-generation systems [Zhang and Clark (2011, Zhang and Clark (2015, Liu et al. (2015, Schmaltz et al. (2016]. We cast this task as seq2seq problem by viewing a shuffled sentence as a source sentence, and the correctly ordered sentence as the target. While word ordering is a somewhat synthetic task, it has two interesting properties for our purposes. First, it is a task which plausibly requires search (due to the exponentially many possible orderings), and, second, there is a clear hard constraint on output sequences, namely, that they be a permutation of the source sequence. For both the baseline and BSO models we enforce this constraint at test-time. However, we also experiment with constraining the BSO model during training, as described in Section 4.2, by defining the succ⁡\operatorname{succ} function to only allow successor sequences containing un-used words in the source sentence.

For experiments, we use the same PTB dataset (with the standard training, development, and test splits) and evaluation procedure as in ?) and later work, with performance reported in terms of BLEU score with the correctly ordered sentences. For all word-ordering experiments we use 2-layer encoder and decoder LSTMs, each with 256 hidden units, and dropout with a rate of 0.2 between LSTM layers. We use simple 0/1 costs in defining the Δ\Delta function.

We show our test-set results in Table 1. We see that on this task there is a large improvement at each beam size from switching to BSO, and a further improvement from using the constrained model.

Inspired by a similar analysis in ?), we further examine the relationship between KtrK_{tr} and KteK_{te} when training with ConBSO in Table 2. We see that larger KtrK_{tr} hurt greedy inference, but that results continue to improve, at least initially, when using a KteK_{te} that is (somewhat) bigger than Ktr−1K_{tr}-1.

Dependency Parsing

We next apply our model to dependency parsing, which also has hard constraints and plausibly benefits from search. We treat dependency parsing with arc-standard transitions as a seq2seq task by attempting to map from a source sentence to a target sequence of source sentence words interleaved with the arc-standard, reduce-actions in its parse. For example, we attempt to map the source sentence

But it was @L_SBJ @L_DEP the Quotron problems @L_NMOD @L_NMOD that …

We use the standard Penn Treebank dataset splits with Stanford dependency labels, and the standard UAS/LAS evaluation metric (excluding punctuation) following ?). All models thus see only the words in the source and, when decoding, the actions it has emitted so far; no other features are used. We use 2-layer encoder and decoder LSTMs with 300 hidden units per layer and dropout with a rate of 0.3 between LSTM layers. We replace singleton words in the training set with an UNK token, normalize digits to a single symbol, and initialize word embeddings for both source and target words from the publicly available word2vec [Mikolov et al. (2013] embeddings. We use simple 0/1 costs in defining the Δ\Delta function.

As in the word-ordering case, we also experiment with modifying the succ⁡\operatorname{succ} function in order to train under hard constraints, namely, that the emitted target sequence be a valid parse. In particular, we constrain the output at each time-step to obey the stack constraint, and we ensure words in the source are emitted in order.

We show results on the test-set in Table 3. BSO and ConBSO both show significant improvements over seq2seq, with ConBSO improving most on UAS, and BSO improving most on LAS. We achieve a reasonable final score of 91.57 UAS, which lags behind the state-of-the-art, but is promising for a general-purpose, word-only model.

Translation

We finally evaluate our model on a small machine translation dataset, which allows us to experiment with a cost function that is not 0/1, and to consider other baselines that attempt to mitigate exposure bias in the seq2seq setting. We use the dataset from the work of ?), which uses data from the German-to-English portion of the IWSLT 2014 machine translation evaluation campaign [Cettolo et al. (2014]. The data comes from translated TED talks, and the dataset contains roughly 153K training sentences, 7K development sentences, and 7K test sentences. We use the same preprocessing and dataset splits as ?), and like them we also use a single-layer LSTM decoder with 256 units. We also use dropout with a rate of 0.2 between each LSTM layer. We emphasize, however, that while our decoder LSTM is of the same size as that of ?), our results are not directly comparable, because we use an LSTM encoder (rather than a convolutional encoder as they do), a slightly different attention mechanism, and input feeding [Luong et al. (2015].

We further investigate the utility of these sequence-level costs in Table 5, which compares using sentence-level BLEU costs in defining Δ\Delta with using 0/1 costs.

We see that the more sophisticated sequence-level costs have a moderate effect on BLEU score.

Timing

Given Algorithm 1, we would expect training time to increase linearly with the size of the beam. On the above MT task, our highly tuned seq2seq baseline processes an average of 13,038 tokens/second (including both source and target tokens) on a GTX 970 GPU. For beams of size KtrK_{tr} = 2, 3, 4, 5, and 6, our implementation processes on average 1,985, 1,768, 1,709, 1,521, and 1,458 tokens/second, respectively. Thus, we appear to pay an initial constant factor of ≈3.3\approx 3.3 due to the more complicated forward and backward passes, and then training scales with the size of the beam. Because we batch beam predictions on a GPU, however, we find that in practice training time scales sub-linearly with the beam-size.

Conclusion

We have introduced a variant of seq2seq and an associated beam search training scheme, which addresses exposure bias as well as label bias, and moreover allows for both training with sequence-level cost functions as well as with hard constraints. Future work will examine scaling this approach to much larger datasets.

Acknowledgments

We thank Yoon Kim for helpful discussions and for providing the initial seq2seq code on which our implementations are based. We thank Allen Schmaltz for help with the word ordering experiments. We also gratefully acknowledge the support of a Google Research Award.

References