nmtpy is a refactored, extended and Python 3 only version of dl4mt-tutorial 111https://github.com/nyu-dl/dl4mt-tutorial, a Theano (Theano Development Team, 2016) implementation of attentive Neural Machine Translation (NMT) (Bahdanau et al., 2014).
The development of nmtpy project which has been open-sourced222https://github.com/lium-lst/nmtpy under MIT license in March 2017, started in March 2016 as an effort to adapt dl4mt-tutorial to multimodal translation models. nmtpy has now become a powerful toolkit where adding a new model is as simple as deriving from an abstract base class to fill in a set of fundamental methods and (optionally) implementing a custom data iterator. The training and inference utilities are as model-agnostic as possible allowing one to use them for different sequence generation networks such as multimodal NMT and image captioning to name a few. This flexibility and the rich set of provided architectures (Section 3) is what differentiates nmtpy from Nematus (Sennrich et al., 2017) another NMT software derived from dl4mt-tutorial.
Figure 1 describes the general workflow of a training session. An experiment in nmtpy is described with a configuration file (Appendix A) to ensure reusability and reproducibility. A training experiment can be simply launched by providing this configuration file to nmt-train which sets up the environment and starts the training. Specifically nmt-train automatically selects a free GPU, sets the seed for all random number generators and finally creates a model (model_type option) instance. Architecture-specific steps like data loading, weight initialization and graph construction are delegated to the model instance. The corresponding log file and model checkpoints are named in a way to reflect the experiment options determined by the configuration file (Example: model_type-e<embdim>-r<rnndim>-<opt>_<lrate>...).
Once everything is ready, nmt-train starts consuming mini-batches of data from the model’s iterator to perform forward/backward passes along with the weight updates. A validation on held-out corpus is periodically performed to evaluate the generalization performance of the model. Specifically, after each valid_freq updates, nmt-train calls the nmt-translate utility which will perform beam-search decoding, compute the requested metrics and return the results back so that nmt-train can track the progress and save best checkpoints to disk.
Several examples regarding the usage of the utilities are given in Appendix B.
2.1 Adding New Architectures
New architectures can be defined by creating a new file under nmtpy/models/ using a copy of an existing architecture and modifying the following predefined methods:
__init__(): Instantiates a model. Keyword arguments can be used to add options specific to the architecture that will be automatically gathered from the configuration file by nmt-train.
init_params(): Initializes the layers and weights.
build(): Defines the Theano computation graph that will be used during training.
build_sampler(): Defines the Theano computation graph that will be used during beam-search. This is generally very similar to build()
but with sequential RNN steps and non-masked tensors.
load_valid_data(): Loads the validation data for perplexity computation.
load_data(): Loads the training data.
2.2 Building Blocks
In this section, we introduce the currently available components and features of nmtpy that one can use to design their architecture.
nmtpy2012), Adadelta (Zeiler, 2012) and Adam (Kingma & Ba, 2014) to optimize the weights of the trained network. A preliminary support for gradient noise (Neelakantan et al., 2015) is available for Adam. Gradient norm clipping (Pascanu et al., 2013) is enabled by default with a threshold of 5 to avoid exploding gradients. Although the provided architectures all use the cross-entropy objective by their nature, any arbitrary differentiable objective function can be used since the training loop is agnostic to the architecture being trained.
A dropout (Srivastava et al., 2014) layer which can be placed after any arbitrary feed-forward layer in the architecture is available. This layer works in inverse mode where the magnitudes are scaled during training instead of testing. Additionally, L2 regularization loss with a scalar factor defined by decay_c option in the configuration can be added to the training loss.
The following layers are available in the latest version of nmtpy:
Layer normalization (Ba et al., 2016)
, a method that adaptively learns to scale and shift the incoming activations of a neuron, can be enabled for GRU and CGRU blocks.
Parallel and monolingual text iterators with compressed (.gz, .bz2, .xz) file support are available under the names TextIterator and BiTextIterator. Additionally, the multimodal WMTIterator allows using image features and source/target sentences at the same time for multimodal NMT (Section 3.3). We recommend using shuffle_mode:trglen when implemented to speed up the training by efficiently batching same-length sequences.
All decoded translations will be post-processed if filter option is given in the configuration file. This is useful in the case where one would like to compute automatic metrics on surface forms instead of segmented. Currently available filters are bpe and compound for cleaning subword BPE (Sennrich et al., 2016) and German compound-splitting (Sennrich & Haddow, 2015) respectively.
performs a patience based early-stopping using either validation perplexity or one of the following external evaluation metrics:
bleu: Wrapper around Moses multi-bleu BLEU (Papineni et al., 2002)
bleu_v13a: A Python reimplementation of Moses mteval-v13a.pl BLEU
meteor: Wrapper around METEOR (Lavie & Agarwal, 2007)
The above metrics are also available for nmt-translate to immediately score the produced hypotheses. Other metrics can be easily added and made available as early-stopping metrics.
The default NMT architecture (attention) is based on the original dl4mt-tutorial implementation which differs from Bahdanau et al. (2014) in the following major aspects:
CGRU decoder which consists of two GRU layers interleaved with attention mechanism.
The hidden state of the decoder is initialized with a non-linear transformation applied tomean bi-directional encoder state in contrast to last bi-directional encoder state.
The Maxout (Goodfellow et al., 2013) hidden layer before the softmax operation is removed.
In addition, nmtpy offers the following configurable options for this NMT:
layer_norm Enables/disables layer normalization for bi-directional GRU encoder.
init_cgru Allows initializing CGRU with all-zeros instead of mean encoder state.
n_enc_layers Number of additional unidirectional GRU encoders to stack on top of bi-directional encoder.
Dropout probabilities for three dropout layers placed after source embeddings (emb_dropout), encoder hidden states (ctx_dropout) and pre-softmax activations (out_dropout).
3.2 Factored NMT
Factored NMT (FNMT) is an extension of NMT which is able to generate two output symbols. The architecture of such a model is presented in Figure 2. In contrast to multi-task architectures, FNMT outputs share the same recurrence and output symbols are generated in a synchronous fashion333FNMT currently uses a dedicated nmt-translate-factors utility though it will probably be merged in the near future..
Two FNMT variants which differ in how they handle the output layer are currently available:
attention_factors: the lemma and factor embeddings are concatenated to form a single feedback embedding.
attention_factors_seplogits: the output path for lemmas and factors are kept separate with different pre-softmax transformations applied for specialization.
3.3 Multimodal NMT & Captioning
We provide several multimodal architectures (Caglayan et al., 2016a, b) where the probability of a target word is conditioned on source sentence representations and convolutional image features (Figure 3
). More specifically, these architectures extends monomodal CGRU into a multimodal one where the attention mechanism can be shared or separate between input modalities. A late fusion of attended context vectors are done using either by summing or concatenating the modality-specific representations.
3.4 Language Modeling
A GRU-based language model architecture (rnnlm) is available in the repository which can be used with nmt-test-lm to obtain language model scores.
3.5 Image Captioning
A GRU-based reimplementation of Show, Attend and Tell architecture (Xu et al., 2015) which learns to generate a natural language description by applying soft attention over convolutional image features is available under the name img2txt. This architecture is recently used 555http://www.statmt.org/wmt17/multimodal-task.html as a baseline system for the Multilingual Image Description Generation track of WMT’17 Multimodal Machine Translation shared task.
In this section we present translation and rescoring utilities nmt-translate and nmt-rescore. Other auxiliary utilities are briefly described in Appendix C.
nmt-translate is responsible for translation decoding using the beam-search method defined by NMT architecture. This default beam-search supports single and ensemble decoding for both monomodal and multimodal translation models. If a given architecture reimplements the beam-search method in its class, that one will be used instead.
Since the number of CPUs in a single machine is 2x-4x higher than the number of GPUs and we mainly reserve the GPUs for training, nmt-translate makes use of CPU workers for maximum efficiency. More specifically, each worker receives a model instance (or instances when ensembling) and performs the beam-search on samples that it continuously fetches from a shared queue. This queue is filled by the master process using the iterator provided by the model.
One thing to note for parallel CPU decoding is that if the numpy is linked against a BLAS implementation with threading support enabled (as in the case with Anaconda & Intel MKL), each spawned process attempts to use all available threads in the machine leading to a resource conflict. In order for nmt-translate to benefit correctly from parallelism, the number of threads per process is thus limited 666This is achieved by setting X_NUM_THREADS=1 environment variable where X is one of OPENBLAS,OMP,MKL depending on the numpy installation. to 1. The impact of this setting and the overall decoding speed in terms of words/sec (wps) are reported in (Table 1) for a medium-sized EnTr NMT with 10M parameters.
|# BLAS Threads||Tesla K40||4 CPU||8 CPU||16 CPU|
|Default||185 wps||26 wps||25 wps||25 wps|
|Set to 1||185 wps||109 wps||198 wps||332 wps|
A 1-best plain text or -best hypotheses file can be rescored with nmt-rescore using either a single or an ensemble of models. Since rescoring of a given hypothesis simply means computing the negative log-likelihood of it given the source sentence, nmt-rescore uses a single GPU to efficiently compute the scores in batched mode. See Appendix B for examples.
We have presented nmtpy, an open-source sequence-to-sequence framework based on dl4mt-tutorial and refined in many ways to ease the task of integrating new architectures. The toolkit has been internally used in our team for tasks ranging from monomodal, multimodal and factored NMT to image captioning and language modeling to help achieving top-ranked submissions during campaigns like IWSLT and WMT.
This work was supported by the French National Research Agency (ANR) through the CHIST-ERA M2CR project, under the contract number ANR-15-CHR2-0006-01777http://m2cr.univ-lemans.fr.
- Ba et al. (2016) Jimmy Lei Ba, Jamie Ryan Kiros, and Geoffrey E Hinton. Layer normalization. arXiv preprint arXiv:1607.06450, 2016.
- Bahdanau et al. (2014) Dzmitry Bahdanau, Kyunghyun Cho, and Yoshua Bengio. Neural machine translation by jointly learning to align and translate. arXiv preprint arXiv:1409.0473, 2014.
- Caglayan et al. (2016a) Ozan Caglayan, Walid Aransa, Yaxing Wang, Marc Masana, Mercedes García-Martínez, Fethi Bougares, Loïc Barrault, and Joost van de Weijer. Does multimodality help human and machine for translation and image captioning? In Proceedings of the First Conference on Machine Translation, pp. 627–633, Berlin, Germany, August 2016a. Association for Computational Linguistics. URL http://www.aclweb.org/anthology/W/W16/W16-2358.
- Caglayan et al. (2016b) Ozan Caglayan, Loïc Barrault, and Fethi Bougares. Multimodal attention for neural machine translation. arXiv preprint arXiv:1609.03976, 2016b.
- Chen et al. (2015) Xinlei Chen, Hao Fang, Tsung-Yi Lin, Ramakrishna Vedantam, Saurabh Gupta, Piotr Dollár, and C Lawrence Zitnick. Microsoft coco captions: Data collection and evaluation server. arXiv preprint arXiv:1504.00325, 2015.
- Chung et al. (2014) Junyoung Chung, Çaglar Gülçehre, KyungHyun Cho, and Yoshua Bengio. Empirical evaluation of gated recurrent neural networks on sequence modeling. CoRR, abs/1412.3555, 2014. URL http://arxiv.org/abs/1412.3555.
- Elliott et al. (2015) Desmond Elliott, Stella Frank, and Eva Hasler. Multi-language image description with neural sequence models. CoRR, abs/1510.04709, 2015. URL http://arxiv.org/abs/1510.04709.
- Firat & Cho (2016) Orhan Firat and Kyunghyun Cho. Conditional gated recurrent unit with attention mechanism. github.com/nyu-dl/dl4mt-tutorial/blob/master/docs/cgru.pdf, 2016.
- García-Martínez et al. (2016) Mercedes García-Martínez, Loïc Barrault, and Fethi Bougares. Factored neural machine translation architectures. In Proceedings of the International Workshop on Spoken Language Translation, IWSLT’16, Seattle, USA, 2016. URL http://workshop2016.iwslt.org/downloads/IWSLT_2016_paper_2.pdf.
Glorot & Bengio (2010)
Xavier Glorot and Yoshua Bengio.
Understanding the difficulty of training deep feedforward neural networks.In
In Proceedings of the International Conference on Artificial Intelligence and Statistics (AISTATS’10). Society for Artificial Intelligence and Statistics, 2010.
Goodfellow et al. (2013)
Ian Goodfellow, David Warde-Farley, Mehdi Mirza, Aaron Courville, and Yoshua
In Sanjoy Dasgupta and David McAllester (eds.),
Proceedings of the 30th International Conference on Machine Learning, volume 28 of Proceedings of Machine Learning Research, pp. 1319–1327, Atlanta, Georgia, USA, 17–19 Jun 2013. PMLR. URL http://proceedings.mlr.press/v28/goodfellow13.html.
He et al. (2015)
Kaiming He, Xiangyu Zhang, Shaoqing Ren, and Jian Sun.
Delving deep into rectifiers: Surpassing human-level performance on imagenet classification.In Computer Vision (ICCV), 2015 IEEE International Conference on, pp. 1026–1034. IEEE, 2015.
- Inan et al. (2016) Hakan Inan, Khashayar Khosravi, and Richard Socher. Tying word vectors and word classifiers: A loss framework for language modeling. arXiv preprint arXiv:1611.01462, 2016.
- Kingma & Ba (2014) Diederik Kingma and Jimmy Ba. Adam: A method for stochastic optimization. arXiv preprint arXiv:1412.6980, 2014.
- Lavie & Agarwal (2007) Alon Lavie and Abhaya Agarwal. Meteor: an automatic metric for mt evaluation with high levels of correlation with human judgments. In Proceedings of the Second Workshop on Statistical Machine Translation, StatMT ’07, pp. 228–231, Stroudsburg, PA, USA, 2007. Association for Computational Linguistics.
- Lin (2004) Chin-Yew Lin. Rouge: A package for automatic evaluation of summaries. In Stan Szpakowicz Marie-Francine Moens (ed.), Text Summarization Branches Out: Proceedings of the ACL-04 Workshop, pp. 74–81, Barcelona, Spain, July 2004. Association for Computational Linguistics.
- Neelakantan et al. (2015) Arvind Neelakantan, Luke Vilnis, Quoc V Le, Ilya Sutskever, Lukasz Kaiser, Karol Kurach, and James Martens. Adding gradient noise improves learning for very deep networks. arXiv preprint arXiv:1511.06807, 2015.
- Papineni et al. (2002) Kishore Papineni, Salim Roukos, Todd Ward, and Wei-Jing Zhu. Bleu: a method for automatic evaluation of machine translation. In Proceedings of the 40th Annual Meeting on Association for Computational Linguistics, ACL ’02, pp. 311–318, Stroudsburg, PA, USA, 2002.
Pascanu et al. (2013)
Razvan Pascanu, Tomas Mikolov, and Yoshua Bengio.
On the difficulty of training recurrent neural networks.In Proceedings of The 30th International Conference on Machine Learning, pp. 1310–1318, 2013.
- Press & Wolf (2016) Ofir Press and Lior Wolf. Using the output embedding to improve language models. arXiv preprint arXiv:1608.05859, 2016.
- Saxe et al. (2013) Andrew M Saxe, James L McClelland, and Surya Ganguli. Exact solutions to the nonlinear dynamics of learning in deep linear neural networks. arXiv preprint arXiv:1312.6120, 2013.
Sennrich & Haddow (2015)
Rico Sennrich and Barry Haddow.
A joint dependency model of morphological and syntactic structure for
statistical machine translation.
Proceedings of the 2015 Conference on Empirical Methods in Natural Language Processing, pp. 114–121. Association for Computational Linguistics, 2015.
- Sennrich et al. (2016) Rico Sennrich, Barry Haddow, and Alexandra Birch. Neural machine translation of rare words with subword units. In Proceedings of the 54th Annual Meeting of the Association for Computational Linguistics (Volume 1: Long Papers), pp. 1715–1725, Berlin, Germany, August 2016. Association for Computational Linguistics. URL http://www.aclweb.org/anthology/P16-1162.
- Sennrich et al. (2017) Rico Sennrich, Orhan Firat, Kyunghyun Cho, Alexandra Birch-Mayne, Barry Haddow, Julian Hitschler, Marcin Junczys-Dowmunt, Samuel Läubli, Antonio Miceli Barone, Jozef Mokry, and Maria Nadejde. Nematus: a Toolkit for Neural Machine Translation, pp. 65–68. Association for Computational Linguistics (ACL), 4 2017. ISBN 978-1-945626-34-0.
- Specia et al. (2016) Lucia Specia, Stella Frank, Khalil Sima’an, and Desmond Elliott. A shared task on multimodal machine translation and crosslingual image description. In Proceedings of the First Conference on Machine Translation, Berlin, Germany. Association for Computational Linguistics, 2016.
- Srivastava et al. (2014) Nitish Srivastava, Geoffrey E Hinton, Alex Krizhevsky, Ilya Sutskever, and Ruslan Salakhutdinov. Dropout: a simple way to prevent neural networks from overfitting. Journal of Machine Learning Research, 15(1):1929–1958, 2014.
- Srivastava et al. (2015) Rupesh Kumar Srivastava, Klaus Greff, and Jürgen Schmidhuber. Highway networks. arXiv preprint arXiv:1505.00387, 2015.
- Theano Development Team (2016) Theano Development Team. Theano: A Python framework for fast computation of mathematical expressions. arXiv e-prints, abs/1605.02688, 2016. URL http://arxiv.org/abs/1605.02688.
- Tieleman & Hinton (2012) Tijmen Tieleman and Geoffrey Hinton. Lecture 6.5-rmsprop: Divide the gradient by a running average of its recent magnitude. COURSERA: Neural networks for machine learning, 4(2), 2012.
Vedantam et al. (2015)
Ramakrishna Vedantam, C Lawrence Zitnick, and Devi Parikh.
Cider: Consensus-based image description evaluation.
Proceedings of the IEEE Conference on Computer Vision and Pattern Recognition, pp. 4566–4575, 2015.
- Xu et al. (2015) Kelvin Xu, Jimmy Ba, Ryan Kiros, Kyunghyun Cho, Aaron Courville, Ruslan Salakhudinov, Rich Zemel, and Yoshua Bengio. Show, attend and tell: Neural image caption generation with visual attention. In Proceedings of The 32nd International Conference on Machine Learning, pp. 2048–2057, 2015.
- Zeiler (2012) Matthew D Zeiler. Adadelta: an adaptive learning rate method. arXiv preprint arXiv:1212.5701, 2012.
Appendix A Configuration File Example
Appendix B Usage Examples
Appendix C Description of the provided tools
Generates .pkl vocabulary files from preprocessed corpus. A single/combined vocabulary for two or more languages can be created with -s flag.
Extracts arbitrary weights from a model snapshot which can further be used as pre-trained weights of a new experiment or analyzed using visualization techniques (especially for embeddings).
Computes language model perplexity of a given corpus.