Nematus: a Toolkit for Neural Machine Translation

Rico Sennrich, Orhan Firat, Kyunghyun Cho, Alexandra Birch, Barry Haddow, Julian Hitschler, Marcin Junczys-Dowmunt, Samuel Läubli, Antonio Valerio Miceli Barone, Jozef Mokry, Maria Nădejde

Introduction

Neural Machine Translation (NMT) [Bahdanau et al., 2015, Sutskever et al., 2014] has recently established itself as a new state-of-the art in machine translation. We present Nematusavailable at https://github.com/rsennrich/nematus, a new toolkit for Neural Machine Translation.

Nematus has its roots in the dl4mt-tutorial.https://github.com/nyu-dl/dl4mt-tutorial We found the codebase of the tutorial to be compact, simple and easy to extend, while also producing high translation quality. These characteristics make it a good starting point for research in NMT. Nematus has been extended to include new functionality based on recent research, and has been used to build top-performing systems to last year’s shared translation tasks at WMT [Sennrich et al., 2016] and IWSLT [Junczys-Dowmunt and Birch, 2016].

Nematus is implemented in Python, and based on the Theano framework [Theano Development Team, 2016]. It implements an attentional encoder–decoder architecture similar to ?). Our neural network architecture differs in some aspect from theirs, and we will discuss differences in more detail. We will also describe additional functionality, aimed to enhance usability and performance, which has been implemented in Nematus.

Neural Network Architecture

Nematus implements an attentional encoder–decoder architecture similar to the one described by ?), but with several implementation differences. The main differences are as follows:

We initialize the decoder hidden state with the mean of the source annotation, rather than the annotation at the last position of the encoder backward RNN.

We implement a novel conditional GRU with attention.

In the decoder, we use a feedforward hidden layer with tanh\tanh non-linearity rather than a maxout before the softmax layer.

In both encoder and decoder word embedding layers, we do not use additional biases.

Compared to Look, Generate, Update decoder phases in ?), we implement Look, Update, Generate which drastically simplifies the decoder implementation (see Table 1).

Optionally, we perform recurrent Bayesian dropout [Gal, 2015].

Instead of a single word embedding at each source position, our input representations allows multiple features (or “factors”) at each time step, with the final embedding being the concatenation of the embeddings of each feature [Sennrich and Haddow, 2016].

We allow tying of embedding matrices [Press and Wolf, 2017, Inan et al., 2016].

We will here describe some differences in more detail:

Given a source sequence (x1,,xTx)(x_{1},\dots,x_{T_{x}}) of length TxT_{x} and a target sequence (y1,,yTy)(y_{1},\dots,y_{T_{y}}) of length TyT_{y}, let hi\mathbf{h}_{i} be the annotation of the source symbol at position ii, obtained by concatenating the forward and backward encoder RNN hidden states, hi=[hi;hi]\mathbf{h}_{i}=[\overrightarrow{\mathbf{h}}_{i};\overleftarrow{\mathbf{h}}_{i}], and sj\mathbf{s}_{j} be the decoder hidden state at position jj.

?) initialize the decoder hidden state s\mathbf{s} with the last backward encoder state.

with Winit\mathbf{W}_{init} as trained parameters.All the biases are omitted for simplicity. We use the average annotation instead:

conditional GRU with attention

Nematus implements a novel conditional GRU with attention, cGRUatt{}_{\text{att}}. A cGRUatt{}_{\text{att}} uses its previous hidden state sj1\mathbf{s}_{j-1}, the whole set of source annotations C={h1,,hTx}\text{C}=\{\mathbf{h}_{1},\dots,\mathbf{h}_{T_{x}}\} and the previously decoded symbol yj1y_{j-1} in order to update its hidden state sj\mathbf{s}_{j}, which is further used to decode symbol yjy_{j} at position jj,

Our conditional GRU layer with attention mechanism, cGRUatt{}_{\text{att}}, consists of three components: two GRU state transition blocks and an attention mechanism ATT in between. The first transition block, GRU1\text{GRU}_{1}, combines the previous decoded symbol yj1y_{j-1} and previous hidden state sj1\mathbf{s}_{j-1} in order to generate an intermediate representation sj\mathbf{s}^{\prime}_{j} with the following formulations:

where E\mathbf{E} is the target word embedding matrix, sj\underline{\mathbf{s}}_{j}^{\prime} is the proposal intermediate representation, rj\mathbf{r}_{j}^{\prime} and zj\mathbf{z}_{j}^{\prime} being the reset and update gate activations. In this formulation, W\mathbf{W}^{\prime}, U\mathbf{U}^{\prime}, Wr\mathbf{W}_{r}^{\prime}, Ur\mathbf{U}_{r}^{\prime}, Wz\mathbf{W}_{z}^{\prime}, Uz\mathbf{U}_{z}^{\prime} are trained model parameters; σ\sigma is the logistic sigmoid activation function.

The attention mechanism, ATT, inputs the entire context set C along with intermediate hidden state sj\mathbf{s}_{j}^{\prime} in order to compute the context vector cj\mathbf{c}_{j} as follows:

where αij\alpha_{ij} is the normalized alignment weight between source symbol at position ii and target symbol at position jj and va,Ua,Wa\mathbf{v}_{a},\mathbf{U}_{a},\mathbf{W}_{a} are the trained model parameters.

Finally, the second transition block, GRU2\text{GRU}_{2}, generates sj\mathbf{s}_{j}, the hidden state of the cGRUatt\text{cGRU}_{\text{att}}, by looking at intermediate representation sj\mathbf{s}_{j}^{\prime} and context vector cj\mathbf{c}_{j} with the following formulations:

similarly, sj\underline{\mathbf{s}}_{j} being the proposal hidden state, rj\mathbf{r}_{j} and zj\mathbf{z}_{j} being the reset and update gate activations with the trained model parameters W,U,Wr,Ur,Wz,Uz\mathbf{W},\mathbf{U},\mathbf{W}_{r},\mathbf{U}_{r},\mathbf{W}_{z},\mathbf{U}_{z}.

Note that the two GRU blocks are not individually recurrent, recurrence only occurs at the level of the whole cGRU layer. This way of combining RNN blocks is similar to what is referred in the literature as deep transition RNNs [Pascanu et al., 2014, Zilly et al., 2016] as opposed to the more common stacked RNNs [Schmidhuber, 1992, El Hihi and Bengio, 1995, Graves, 2013].

deep output

Given sj\mathbf{s}_{j}, yj1y_{j-1}, and cj\mathbf{c}_{j}, the output probability p(yjsj,yj1,cj)p(y_{j}|\mathbf{s}_{j},y_{j-1},\mathbf{c}_{j}) is computed by a softmax activation, using an intermediate representation tj\mathbf{t}_{j}.

Wt1,Wt2,Wt3,Wo\mathbf{W}_{t1},\mathbf{W}_{t2},\mathbf{W}_{t3},\mathbf{W}_{o} are the trained model parameters.

Training Algorithms

By default, the training objective in Nematus is cross-entropy minimization on a parallel training corpus. Training is performed via stochastic gradient descent, or one of its variants with adaptive learning rate (Adadelta [Zeiler, 2012], RmsProp [Tieleman and Hinton, 2012], Adam [Kingma and Ba, 2014]).

Additionally, Nematus supports minimum risk training (MRT) [Shen et al., 2016] to optimize towards an arbitrary, sentence-level loss function. Various MT metrics are supported as loss function, including smoothed sentence-level Bleu [Chen and Cherry, 2014], METEOR [Denkowski and Lavie, 2011], BEER [Stanojevic and Sima’an, 2014], and any interpolation of implemented metrics.

To stabilize training, Nematus supports early stopping based on cross entropy, or an arbitrary loss function defined by the user.

Usability Features

In addition to the main algorithms to train and decode with an NMT model, Nematus includes features aimed towards facilitating experimentation with the models, and their visualisation. Various model parameters are configurable via a command-line interface, and we provide extensive documentation of options, and sample set-ups for training systems.

Nematus provides support for applying single models, as well as using multiple models in an ensemble – the latter is possible even if the model architectures differ, as long as the output vocabulary is the same. At each time step, the probability distribution of the ensemble is the geometric average of the individual models’ probability distributions. The toolkit includes scripts for beam search decoding, parallel corpus scoring and n-best-list rescoring.

Nematus includes utilities to visualise the attention weights for a given sentence pair, and to visualise the beam search graph. An example of the latter is shown in Figure 1. Our demonstration will cover how to train a model using the command-line interface, and showing various functionalities of Nematus, including decoding and visualisation, with pre-trained models.Pre-trained models for 8 translation directions are available at http://statmt.org/rsennrich/wmt16_systems/

Conclusion

We have presented Nematus, a toolkit for Neural Machine Translation. We have described implementation differences to the architecture by ?); due to the empirically strong performance of Nematus, we consider these to be of wider interest.

We hope that researchers will find Nematus an accessible and well documented toolkit to support their research. The toolkit is by no means limited to research, and has been used to train MT systems that are currently in production [WIPO, 2016].

Nematus is available under a permissive BSD license.

Acknowledgments

This project has received funding from the European Union’s Horizon 2020 research and innovation programme under grant agreements 645452 (QT21), 644333 (TraMOOC), 644402 (HimL) and 688139 (SUMMA).

References