This is a Pytorch port of OpenNMT, an open-source (MIT) neural machine translation system. It is designed to be research friendly to try out new ideas in translation, summary, image-to-text, morphology, and many other domains.
Codebase is relatively stable, but PyTorch is still evolving. We currently only support PyTorch 0.4.1 and recommend forking if you need to have stable code.
OpenNMT-py is run as a collaborative open-source project. It is maintained by Sasha Rush (Cambridge, MA), Ben Peters (Lisbon), and Jianyu Zhan (Shanghai). The original code was written by Adam Lerer (NYC). We love contributions. Please consult the Issues page for any Contributions Welcome tagged post.
All dependencies can be installed via:
pip install -r requirements.txt
Note that we currently only support PyTorch 0.4.1
The following OpenNMT features are implemented:
- data preprocessing
- Inference (translation) with batching and beam search
- Multiple source and target RNN (lstm/gru) types and attention (dotprod/mlp) types
- TensorBoard
- Source word features
- Pretrained Embeddings
- Copy and Coverage Attention
- Image-to-text processing
- Speech-to-text processing
- "Attention is all you need"
- Multi-GPU
- Inference time loss functions.
Beta Features (committed):
- Structured attention
- [Conv2Conv convolution model]
- SRU "RNNs faster than CNN" paper
python preprocess.py -train_src data/src-train.txt -train_tgt data/tgt-train.txt -valid_src data/src-val.txt -valid_tgt data/tgt-val.txt -save_data data/demo
We will be working with some example data in data/
folder.
The data consists of parallel source (src
) and target (tgt
) data containing one sentence per line with tokens separated by a space:
src-train.txt
tgt-train.txt
src-val.txt
tgt-val.txt
Validation files are required and used to evaluate the convergence of the training. It usually contains no more than 5000 sentences.
After running the preprocessing, the following files are generated:
demo.train.pt
: serialized PyTorch file containing training datademo.valid.pt
: serialized PyTorch file containing validation datademo.vocab.pt
: serialized PyTorch file containing vocabulary data
Internally the system never touches the words themselves, but uses these indices.
python train.py -data data/demo -save_model demo-model
The main train command is quite simple. Minimally it takes a data file
and a save file. This will run the default model, which consists of a
2-layer LSTM with 500 hidden units on both the encoder/decoder.
If you want to train on GPU, you need to set, as an example:
CUDA_VISIBLE_DEVICES=1,3
-world_size 2 -gpu_ranks 0 1
to use (say) GPU 1 and 3 on this node only.
To know more about distributed training on single or multi nodes, read the FAQ section.
python translate.py -model demo-model_acc_XX.XX_ppl_XXX.XX_eX.pt -src data/src-test.txt -output pred.txt -replace_unk -verbose
Now you have a model which you can use to predict on new data. We do this by running beam search. This will output predictions into pred.txt
.
!!! note "Note" The predictions are going to be quite terrible, as the demo dataset is small. Try running on some larger datasets! For example you can download millions of parallel sentences for translation or summarization.
Click this button to open a Workspace on FloydHub for training/testing your code.
Go to tutorial: How to use GloVe pre-trained embeddings in OpenNMT-py
The following pretrained models can be downloaded and used with translate.py.
OpenNMT: Neural Machine Translation Toolkit
@inproceedings{opennmt,
author = {Guillaume Klein and
Yoon Kim and
Yuntian Deng and
Jean Senellart and
Alexander M. Rush},
title = {Open{NMT}: Open-Source Toolkit for Neural Machine Translation},
booktitle = {Proc. ACL},
year = {2017},
url = {https://doi.org/10.18653/v1/P17-4012},
doi = {10.18653/v1/P17-4012}
}