ℹ️ Skipped - page is already crawled
| Filter | Status | Condition | Details |
|---|---|---|---|
| HTTP status | PASS | download_http_code = 200 | HTTP 200 |
| Age cutoff | PASS | download_stamp > now() - 6 MONTH | 0.2 months ago |
| History drop | PASS | isNull(history_drop_reason) | No drop reason |
| Spam/ban | PASS | fh_dont_index != 1 AND ml_spam_score = 0 | ml_spam_score=0 |
| Canonical | PASS | meta_canonical IS NULL OR = '' OR = src_unparsed | Not set |
| Property | Value |
|---|---|
| URL | https://ptorch.com/news/58.html |
| Last Crawled | 2026-04-05 11:59:33 (5 days ago) |
| First Indexed | 2017-12-01 12:41:38 (8 years ago) |
| HTTP Status Code | 200 |
| Meta Title | pytorch使用fairseq-py实现实现快速机器翻译(翻译的速度提高了80%,训练速度提升近50%) - pytorch中文网 |
| Meta Description | 最近,Facebook又开源了fairseq的PyTorch版:fairseq-py。大家从最新的文章可以看出,用CNN来做机器翻译,达到顶尖的准确率,速度则是RNN的9倍;同时,Facebook还开... |
| Meta Canonical | null |
| Boilerpipe Text | 最近,Facebook又开源了fairseq的PyTorch版:fairseq-py。大家从最新的文章可以看出,用CNN来做机器翻译,达到顶尖的准确率,速度则是RNN的9倍;同时,Facebook还开放了seq2seq学习工具包fairseq的Torch源代码和已训练的系统。
fairseq-py优势与介绍
fairseq-py包含论文中描述的全卷积模型,支持在一台机器上用多GPU进行训练,以及CPU和GPU上的快速beam search生成。
fairseq-py可以用来里实现机器翻译,也能用于其他seq2seq的NLP任务。
这个开源工具包同时还包含英译法、英译德的预训练机器翻译模型。
fairseq-py比之前的Torch版更高效,翻译的速度提高了80%,训练速度提升近50%。
介绍
FAIR序列到序列工具包(PyTorch)
这是一个PyTorch版本的
fairseq
,一个从序列到序列学习工具包从Facebook的AI研究。这个重新实现的原始作者(没有特别的顺序)Sergey Edunov,Myle Ott和Sam Gross。该工具包实现
卷积序列到序列学习中
描述的完全卷积模型,并在单个机器上实现多GPU训练,并在CPU和GPU上实现快速波束搜索生成。我们提供英语到法语和英语到德语翻译的预训练模型。
引文
如果您在论文中使用代码,请将其引用为:
@inproceedings{gehring2017convs2s,
author = {Gehring, Jonas, and Auli, Michael and Grangier, David and Yarats, Denis and Dauphin, Yann N},
title = "{Convolutional Sequence to Sequence Learning}",
booktitle = {Proc. of ICML},
year = 2017,
}
要求和安装
运行macOS或Linux的计算机
对于训练新型号,您还需要一个NVIDIA GPU和
NCCL
Python版本3.6
先进行
PyTorch安装
目前,Fairseq-py需要GitHub存储库中的PyTorch。有多种安装方式。我们建议使用
Miniconda3
和以下说明。
从
https://conda.io/miniconda.html
安装Miniconda3 ; 创建并激活Python 3环境。
安装PyTorch:
conda install gcc numpy cudnn nccl
conda install magma-cuda80 -c soumith
pip install cmake
pip install cffi
git clone https://github.com/pytorch/pytorch.git
cd pytorch
git reset --hard a03e5cb40938b6b3f3e6dbddf9cff8afdff72d1b
git submodule update --init
pip install -r requirements.txt
NO_DISTRIBUTED=1 python setup.py install
通过克隆GitHub存储库并运行:安装fairseq-py
pip install -r requirements.txt
python setup.py build
python setup.py develop
快速开始
以下命令行工具可用:
python preprocess.py
:数据预处理:构建词汇和二值化培训数据
python train.py
:在一个或多个GPU上训练新的模型
python generate.py
:用训练有素的模型翻译预处理的数据
python generate.py -i
:用训练有素的模型翻译原始文本
python score.py
:BLEU生成的翻译与参考翻译的得分
评估预先训练的模型
首先,下载一个预先训练的模型及其词汇:
$ curl https://s3.amazonaws.com/fairseq-py/models/wmt14.en-fr.fconv-py.tar.bz2 | tar xvjf -
我们python generate.py -i用来生成翻译。在这里,我们使用5:
$ MODEL_DIR=wmt14.en-fr.fconv-py
$ python generate.py -i \
--path $MODEL_DIR/model.pt $MODEL_DIR \
--beam 5
| [en] dictionary: 44206 types
| [fr] dictionary: 44463 types
| model fconv_wmt_en_fr
| loaded checkpoint /private/home/edunov/wmt14.en-fr.fconv-py/model.pt (epoch 37)
> Why is it rare to discover new marine mam@@ mal species ?
S Why is it rare to discover new marine mam@@ mal species ?
O Why is it rare to discover new marine mam@@ mal species ?
H -0.08662842959165573 Pourquoi est-il rare de découvrir de nouvelles espèces de mammifères marins ?
A 0 1 3 3 5 6 6 10 8 8 8 11 12
这一代脚本产生四种类型的输出:以S为前缀的行显示了应用词汇后提供的源语句; O是原始来源句的副本; H是假设以及平均对数似然; 而A是假设中每个单词的注意最大值,包括从文本中省略的句末标记。
请查看
这里
的完整的预培训模型列表。
培养新模式
数据预处理
Fairseq-py源码分发包含了一个用于IWSLT 2014德语 - 英语语料库的预处理脚本示例。预处理和二值化数据如下:
$ cd data/
$ bash prepare-iwslt14.sh
$ cd ..
$ TEXT=data/iwslt14.tokenized.de-en
$ python preprocess.py --source-lang de --target-lang en \
--trainpref $TEXT/train --validpref $TEXT/valid --testpref $TEXT/test \
--thresholdtgt 3 --thresholdsrc 3 --destdir data-bin/iwslt14.tokenized.de-en
这将编写可用于模型训练的二进制数据
data-bin/iwslt14.tokenized.de-en
。
训练
使用
python train.py
培养的新模式。这里有几个适用于IWSLT 2014数据集的示例设置:
$ mkdir -p checkpoints/fconv
$ CUDA_VISIBLE_DEVICES=0 python train.py data-bin/iwslt14.tokenized.de-en \
--lr 0.25 --clip-norm 0.1 --dropout 0.2 --max-tokens 4000 \
--arch fconv_iwslt_de_en --save-dir checkpoints/fconv
默认情况下,
python train.py
将使用您机器上的所有可用GPU。使用CUDA_VISIBLE_DEVICES环境变量来选择特定的GPU和/或更改将要使用的GPU设备的数量。
还要注意,批量大小是根据每批次的最大令牌数量来指定的
--max-tokens
。您可能需要使用较小的值,具体取决于系统上可用的GPU内存。
Generation
一旦您的模型被训练,您可以使用
python generate.py
(对于二进制化数据)或
python generate.py -i
(对于原始文本)生成翻译:
$ python generate.py data-bin/iwslt14.tokenized.de-en \
--path checkpoints/fconv/checkpoint_best.pt \
--batch-size 128 --beam 5
| [de] dictionary: 35475 types
| [en] dictionary: 24739 types
| data-bin/iwslt14.tokenized.de-en test 6750 examples
| model fconv
| loaded checkpoint trainings/fconv/checkpoint_best.pt
S-721 danke .
T-721 thank you .
...
要仅使用CPU生成翻译,请使用
--cpu
标志。可以用
--remove-bpe
标志移除BPE连续标记。
预先训练的模型
我们提供以下预训练的完全卷积序列到序列模型:
wmt14.en-fr.fconv-py.tar.bz2
:
WMT14英语 - 法语的
预训练模型,包括词汇
wmt14.en-de.fconv-py.tar.bz2
:
WMT14英语 - 德语的
预训练模型,包括词汇
此外,我们还提供了上述型号的预处理和二值化测试仪:
wmt14.en-fr.newstest2014.tar.bz2
:newstest2014测试集为WMT14英语 - 法语
wmt14.en-fr.ntst1213.tar.bz2
:newstest2012和newstest2013 WMT14测试集英文 - 法文
wmt14.en-de.newstest2014.tar.bz2
:newstest2014测试集为WMT14英语 - 德语
使用二进制化测试集的生成可以按批处理模式运行,例如,对于GTX-1080ti上的英语 - 法语:
$ curl https://s3.amazonaws.com/fairseq-py/models/wmt14.en-fr.fconv-py.tar.bz2 | tar xvjf - -C data-bin
$ curl https://s3.amazonaws.com/fairseq-py/data/wmt14.en-fr.newstest2014.tar.bz2 | tar xvjf - -C data-bin
$ python generate.py data-bin/wmt14.en-fr.newstest2014 \
--path data-bin/wmt14.en-fr.fconv-py/model.pt \
--beam 5 --batch-size 128 --remove-bpe | tee /tmp/gen.out
...
| Translated 3003 sentences (95451 tokens) in 81.3s (1174.33 tokens/s)
| Generate test with beam=5: BLEU4 = 40.23, 67.5/46.4/33.8/25.0 (BP=0.997, ratio=1.003, syslen=80963, reflen=81194)
# Scoring with score.py:
$ grep ^H /tmp/gen.out | cut -f3- > /tmp/gen.out.sys
$ grep ^T /tmp/gen.out | cut -f2- > /tmp/gen.out.ref
$ python score.py --sys /tmp/gen.out.sys --ref /tmp/gen.out.ref
BLEU4 = 40.23, 67.5/46.4/33.8/25.0 (BP=0.997, ratio=1.003, syslen=80963, reflen=81194)
Facebook页面:
https
:
//www.facebook.com/groups/fairseq.users
Google群组:
https
:
//groups.google.com/forum/#!forum/fairseq-users
执照
fairseq-py是BSD许可的。许可证也适用于预培训的模型。我们还提供额外的专利授权。
原创文章,转载请注明 :
pytorch使用fairseq-py实现实现快速机器翻译(翻译的速度提高了80%,训练速度提升近50%) - pytorch中文网
原文出处: https://ptorch.com/news/58.html
问题交流群 :168117787 |
| Markdown | python优先的端到端深度学习平台
[Pytorch中文文档](https://ptorch.com/docs/1/) [Torch中文文档](https://ptorch.com/docs/2/) [Pytorch视频教程](https://ptorch.com/docs/4/) [Matplotlib中文文档](https://ptorch.com/docs/5/) [OpenCV-Python中文文档](https://ptorch.com/docs/6/) [pytorch0.4中文文档](https://ptorch.com/docs/8/) [Numpy中文文档](https://ptorch.com/docs/9/) [mitmproxy](https://ptorch.com/docs/10/)
- [首页](https://ptorch.com/ "首页")
- [Pytorch中文文档](https://ptorch.com/docs/8/ "Pytorch中文文档")
- [Torch中文文档](https://ptorch.com/docs/2/ "Torch中文文档")
- [问答社区](https://discuss.ptorch.com/ "问答社区")
- [提问/投稿](https://ptorch.com/submission "提问/投稿")
- [关于我们](https://ptorch.com/specials/1 "关于我们")
# pytorch使用fairseq-py实现实现快速机器翻译(翻译的速度提高了80%,训练速度提升近50%)
[Song](https://ptorch.com/news/58.html) • 17397 次浏览 • 0 个回复 • 2017年09月21日

最近,Facebook又开源了fairseq的PyTorch版:fairseq-py。大家从最新的文章可以看出,用CNN来做机器翻译,达到顶尖的准确率,速度则是RNN的9倍;同时,Facebook还开放了seq2seq学习工具包fairseq的Torch源代码和已训练的系统。
### fairseq-py优势与介绍
fairseq-py包含论文中描述的全卷积模型,支持在一台机器上用多GPU进行训练,以及CPU和GPU上的快速beam search生成。
fairseq-py可以用来里实现机器翻译,也能用于其他seq2seq的NLP任务。
这个开源工具包同时还包含英译法、英译德的预训练机器翻译模型。
fairseq-py比之前的Torch版更高效,翻译的速度提高了80%,训练速度提升近50%。
### 介绍
FAIR序列到序列工具包(PyTorch)
这是一个PyTorch版本的[fairseq](https://github.com/facebookresearch/fairseq),一个从序列到序列学习工具包从Facebook的AI研究。这个重新实现的原始作者(没有特别的顺序)Sergey Edunov,Myle Ott和Sam Gross。该工具包实现[卷积序列到序列学习中](https://arxiv.org/abs/1705.03122)描述的完全卷积模型,并在单个机器上实现多GPU训练,并在CPU和GPU上实现快速波束搜索生成。我们提供英语到法语和英语到德语翻译的预训练模型。

### 引文
如果您在论文中使用代码,请将其引用为:
```
@inproceedings{gehring2017convs2s,
author = {Gehring, Jonas, and Auli, Michael and Grangier, David and Yarats, Denis and Dauphin, Yann N},
title = "{Convolutional Sequence to Sequence Learning}",
booktitle = {Proc. of ICML},
year = 2017,
}
```
### 要求和安装
- 运行macOS或Linux的计算机
- 对于训练新型号,您还需要一个NVIDIA GPU和[NCCL](https://github.com/NVIDIA/nccl)
- Python版本3.6
- 先进行[PyTorch安装](http://pytorch.org/)
目前,Fairseq-py需要GitHub存储库中的PyTorch。有多种安装方式。我们建议使用[Miniconda3](https://conda.io/miniconda.html)和以下说明。
- 从<https://conda.io/miniconda.html>安装Miniconda3 ; 创建并激活Python 3环境。
- 安装PyTorch:
```
conda install gcc numpy cudnn nccl
conda install magma-cuda80 -c soumith
pip install cmake
pip install cffi
git clone https://github.com/pytorch/pytorch.git
cd pytorch
git reset --hard a03e5cb40938b6b3f3e6dbddf9cff8afdff72d1b
git submodule update --init
pip install -r requirements.txt
NO_DISTRIBUTED=1 python setup.py install
```
- 通过克隆GitHub存储库并运行:安装fairseq-py
```
pip install -r requirements.txt
python setup.py build
python setup.py develop
```
### 快速开始
以下命令行工具可用:
- `python preprocess.py`:数据预处理:构建词汇和二值化培训数据
- `python train.py`:在一个或多个GPU上训练新的模型
- `python generate.py`:用训练有素的模型翻译预处理的数据
- `python generate.py -i`:用训练有素的模型翻译原始文本
- `python score.py`:BLEU生成的翻译与参考翻译的得分
#### 评估预先训练的模型
首先,下载一个预先训练的模型及其词汇:
```
$ curl https://s3.amazonaws.com/fairseq-py/models/wmt14.en-fr.fconv-py.tar.bz2 | tar xvjf -
```
我们python generate.py -i用来生成翻译。在这里,我们使用5:
```
$ MODEL_DIR=wmt14.en-fr.fconv-py
$ python generate.py -i \
--path $MODEL_DIR/model.pt $MODEL_DIR \
--beam 5
| [en] dictionary: 44206 types
| [fr] dictionary: 44463 types
| model fconv_wmt_en_fr
| loaded checkpoint /private/home/edunov/wmt14.en-fr.fconv-py/model.pt (epoch 37)
> Why is it rare to discover new marine mam@@ mal species ?
S Why is it rare to discover new marine mam@@ mal species ?
O Why is it rare to discover new marine mam@@ mal species ?
H -0.08662842959165573 Pourquoi est-il rare de découvrir de nouvelles espèces de mammifères marins ?
A 0 1 3 3 5 6 6 10 8 8 8 11 12
```
这一代脚本产生四种类型的输出:以S为前缀的行显示了应用词汇后提供的源语句; O是原始来源句的副本; H是假设以及平均对数似然; 而A是假设中每个单词的注意最大值,包括从文本中省略的句末标记。
请查看[这里](https://github.com/facebookresearch/fairseq-py#pre-trained-models)的完整的预培训模型列表。
### 培养新模式
#### 数据预处理
Fairseq-py源码分发包含了一个用于IWSLT 2014德语 - 英语语料库的预处理脚本示例。预处理和二值化数据如下:
```
$ cd data/
$ bash prepare-iwslt14.sh
$ cd ..
$ TEXT=data/iwslt14.tokenized.de-en
$ python preprocess.py --source-lang de --target-lang en \
--trainpref $TEXT/train --validpref $TEXT/valid --testpref $TEXT/test \
--thresholdtgt 3 --thresholdsrc 3 --destdir data-bin/iwslt14.tokenized.de-en
```
这将编写可用于模型训练的二进制数据`data-bin/iwslt14.tokenized.de-en`。
#### 训练
使用`python train.py`培养的新模式。这里有几个适用于IWSLT 2014数据集的示例设置:
```
$ mkdir -p checkpoints/fconv
$ CUDA_VISIBLE_DEVICES=0 python train.py data-bin/iwslt14.tokenized.de-en \
--lr 0.25 --clip-norm 0.1 --dropout 0.2 --max-tokens 4000 \
--arch fconv_iwslt_de_en --save-dir checkpoints/fconv
```
默认情况下,`python train.py`将使用您机器上的所有可用GPU。使用CUDA\_VISIBLE\_DEVICES环境变量来选择特定的GPU和/或更改将要使用的GPU设备的数量。
还要注意,批量大小是根据每批次的最大令牌数量来指定的`--max-tokens`。您可能需要使用较小的值,具体取决于系统上可用的GPU内存。
#### Generation
一旦您的模型被训练,您可以使用`python generate.py` (对于二进制化数据)或`python generate.py -i` (对于原始文本)生成翻译:
```
$ python generate.py data-bin/iwslt14.tokenized.de-en \
--path checkpoints/fconv/checkpoint_best.pt \
--batch-size 128 --beam 5
| [de] dictionary: 35475 types
| [en] dictionary: 24739 types
| data-bin/iwslt14.tokenized.de-en test 6750 examples
| model fconv
| loaded checkpoint trainings/fconv/checkpoint_best.pt
S-721 danke .
T-721 thank you .
...
```
要仅使用CPU生成翻译,请使用`--cpu`标志。可以用`--remove-bpe`标志移除BPE连续标记。
### 预先训练的模型
我们提供以下预训练的完全卷积序列到序列模型:
- [wmt14.en-fr.fconv-py.tar.bz2](https://s3.amazonaws.com/fairseq-py/models/wmt14.en-fr.fconv-py.tar.bz2):[WMT14英语 - 法语的](http://statmt.org/wmt14/translation-task.html#Download)预训练模型,包括词汇
- [wmt14.en-de.fconv-py.tar.bz2](https://s3.amazonaws.com/fairseq-py/models/wmt14.en-de.fconv-py.tar.bz2):[WMT14英语 - 德语的](https://nlp.stanford.edu/projects/nmt)预训练模型,包括词汇
此外,我们还提供了上述型号的预处理和二值化测试仪:
- [wmt14.en-fr.newstest2014.tar.bz2](https://s3.amazonaws.com/fairseq-py/data/wmt14.en-fr.newstest2014.tar.bz2):newstest2014测试集为WMT14英语 - 法语
- [wmt14.en-fr.ntst1213.tar.bz2](https://s3.amazonaws.com/fairseq-py/data/wmt14.en-fr.ntst1213.tar.bz2):newstest2012和newstest2013 WMT14测试集英文 - 法文
- [wmt14.en-de.newstest2014.tar.bz2](https://s3.amazonaws.com/fairseq-py/data/wmt14.en-de.newstest2014.tar.bz2):newstest2014测试集为WMT14英语 - 德语
使用二进制化测试集的生成可以按批处理模式运行,例如,对于GTX-1080ti上的英语 - 法语:
```
$ curl https://s3.amazonaws.com/fairseq-py/models/wmt14.en-fr.fconv-py.tar.bz2 | tar xvjf - -C data-bin
$ curl https://s3.amazonaws.com/fairseq-py/data/wmt14.en-fr.newstest2014.tar.bz2 | tar xvjf - -C data-bin
$ python generate.py data-bin/wmt14.en-fr.newstest2014 \
--path data-bin/wmt14.en-fr.fconv-py/model.pt \
--beam 5 --batch-size 128 --remove-bpe | tee /tmp/gen.out
...
| Translated 3003 sentences (95451 tokens) in 81.3s (1174.33 tokens/s)
| Generate test with beam=5: BLEU4 = 40.23, 67.5/46.4/33.8/25.0 (BP=0.997, ratio=1.003, syslen=80963, reflen=81194)
# Scoring with score.py:
$ grep ^H /tmp/gen.out | cut -f3- > /tmp/gen.out.sys
$ grep ^T /tmp/gen.out | cut -f2- > /tmp/gen.out.ref
$ python score.py --sys /tmp/gen.out.sys --ref /tmp/gen.out.ref
BLEU4 = 40.23, 67.5/46.4/33.8/25.0 (BP=0.997, ratio=1.003, syslen=80963, reflen=81194)
```
- Facebook页面:[https](https://www.facebook.com/groups/fairseq.users):[//www.facebook.com/groups/fairseq.users](https://www.facebook.com/groups/fairseq.users)
- Google群组:[https](https://groups.google.com/forum/#!forum/fairseq-users) : [//groups.google.com/forum/\#!forum/fairseq-users](https://groups.google.com/forum/#!forum/fairseq-users)
### 执照
fairseq-py是BSD许可的。许可证也适用于预培训的模型。我们还提供额外的专利授权。
原创文章,转载请注明 :[pytorch使用fairseq-py实现实现快速机器翻译(翻译的速度提高了80%,训练速度提升近50%) - pytorch中文网](https://ptorch.com/news/58.html)
原文出处: https://ptorch.com/news/58.html
问题交流群 :168117787
*提交评论*
要回复文章请先[登录](https://ptorch.com/login)或[注册](https://ptorch.com/register)
*用户评论*
- 没有评论
[Pytorch是什么?关于Pytorch!](https://ptorch.com/news/1.html) [pytorch使用view(\*args)在不改变张量数据的情况下随意改变张量的大小和形状](https://ptorch.com/news/59.html)
- [](https://u.jd.com/tzf3lTm)
- [](https://jq.qq.com/?_wv=1027&k=4AWXVpH)扫码加入QQ群
- [](https://discuss.ptorch.com/m/)手机浏览社区
- [用Python实现Linux系统占用指定内存,CPU满载,服务器压力测试,场景设计](https://ptorch.com/news/273.html)2022年05月17日
- [\[新手教程\] 最新Mac搭建Python+Appium实现自动化测试教程](https://ptorch.com/news/272.html)2021年05月21日
- [mitmproxy/mitmdump线上部署占用CPU/内存过高解决方案](https://ptorch.com/news/271.html)2021年04月21日
- [pytorch入门](https://discuss.ptorch.com/topic/pytorch%E5%85%A5%E9%97%A8)
- [python](https://discuss.ptorch.com/topic/pytorch)
- [Torch](https://discuss.ptorch.com/topic/torch)
- [Pytorch文档](https://ptorch.com/docs/1/)
- [Torch中文文档](https://ptorch.com/docs/2/)
- [Pytorch中文教程](https://ptorch.com/docs/3/)
- [laravel学习网](https://phpartisan.cn/) [oldpan博客](https://oldpan.me/)
- [天天外链](https://www.moreqifu.com/product/dlj) [摩尔企服](https://www.moreqifu.com/) [摩尔短链接](https://www.moreqifu.com/product/dlj) [摩尔数据](https://www.moreqifu.com/product/doyea) [摩尔活码](https://www.moreqifu.com/product/hm)
Copyright © [pytorch中文网]() \| [京ICP备17031240号-1](https://beian.miit.gov.cn/) \| Song技术支持 |
| Readable Markdown | 最近,Facebook又开源了fairseq的PyTorch版:fairseq-py。大家从最新的文章可以看出,用CNN来做机器翻译,达到顶尖的准确率,速度则是RNN的9倍;同时,Facebook还开放了seq2seq学习工具包fairseq的Torch源代码和已训练的系统。
### fairseq-py优势与介绍
fairseq-py包含论文中描述的全卷积模型,支持在一台机器上用多GPU进行训练,以及CPU和GPU上的快速beam search生成。
fairseq-py可以用来里实现机器翻译,也能用于其他seq2seq的NLP任务。
这个开源工具包同时还包含英译法、英译德的预训练机器翻译模型。
fairseq-py比之前的Torch版更高效,翻译的速度提高了80%,训练速度提升近50%。
### 介绍
FAIR序列到序列工具包(PyTorch)
这是一个PyTorch版本的[fairseq](https://github.com/facebookresearch/fairseq),一个从序列到序列学习工具包从Facebook的AI研究。这个重新实现的原始作者(没有特别的顺序)Sergey Edunov,Myle Ott和Sam Gross。该工具包实现[卷积序列到序列学习中](https://arxiv.org/abs/1705.03122)描述的完全卷积模型,并在单个机器上实现多GPU训练,并在CPU和GPU上实现快速波束搜索生成。我们提供英语到法语和英语到德语翻译的预训练模型。

### 引文
如果您在论文中使用代码,请将其引用为:
```
@inproceedings{gehring2017convs2s,
author = {Gehring, Jonas, and Auli, Michael and Grangier, David and Yarats, Denis and Dauphin, Yann N},
title = "{Convolutional Sequence to Sequence Learning}",
booktitle = {Proc. of ICML},
year = 2017,
}
```
### 要求和安装
- 运行macOS或Linux的计算机
- 对于训练新型号,您还需要一个NVIDIA GPU和[NCCL](https://github.com/NVIDIA/nccl)
- Python版本3.6
- 先进行[PyTorch安装](http://pytorch.org/)
目前,Fairseq-py需要GitHub存储库中的PyTorch。有多种安装方式。我们建议使用[Miniconda3](https://conda.io/miniconda.html)和以下说明。
- 从<https://conda.io/miniconda.html>安装Miniconda3 ; 创建并激活Python 3环境。
- 安装PyTorch:
```
conda install gcc numpy cudnn nccl
conda install magma-cuda80 -c soumith
pip install cmake
pip install cffi
git clone https://github.com/pytorch/pytorch.git
cd pytorch
git reset --hard a03e5cb40938b6b3f3e6dbddf9cff8afdff72d1b
git submodule update --init
pip install -r requirements.txt
NO_DISTRIBUTED=1 python setup.py install
```
- 通过克隆GitHub存储库并运行:安装fairseq-py
```
pip install -r requirements.txt
python setup.py build
python setup.py develop
```
### 快速开始
以下命令行工具可用:
- `python preprocess.py`:数据预处理:构建词汇和二值化培训数据
- `python train.py`:在一个或多个GPU上训练新的模型
- `python generate.py`:用训练有素的模型翻译预处理的数据
- `python generate.py -i`:用训练有素的模型翻译原始文本
- `python score.py`:BLEU生成的翻译与参考翻译的得分
#### 评估预先训练的模型
首先,下载一个预先训练的模型及其词汇:
```
$ curl https://s3.amazonaws.com/fairseq-py/models/wmt14.en-fr.fconv-py.tar.bz2 | tar xvjf -
```
我们python generate.py -i用来生成翻译。在这里,我们使用5:
```
$ MODEL_DIR=wmt14.en-fr.fconv-py
$ python generate.py -i \
--path $MODEL_DIR/model.pt $MODEL_DIR \
--beam 5
| [en] dictionary: 44206 types
| [fr] dictionary: 44463 types
| model fconv_wmt_en_fr
| loaded checkpoint /private/home/edunov/wmt14.en-fr.fconv-py/model.pt (epoch 37)
> Why is it rare to discover new marine mam@@ mal species ?
S Why is it rare to discover new marine mam@@ mal species ?
O Why is it rare to discover new marine mam@@ mal species ?
H -0.08662842959165573 Pourquoi est-il rare de découvrir de nouvelles espèces de mammifères marins ?
A 0 1 3 3 5 6 6 10 8 8 8 11 12
```
这一代脚本产生四种类型的输出:以S为前缀的行显示了应用词汇后提供的源语句; O是原始来源句的副本; H是假设以及平均对数似然; 而A是假设中每个单词的注意最大值,包括从文本中省略的句末标记。
请查看[这里](https://github.com/facebookresearch/fairseq-py#pre-trained-models)的完整的预培训模型列表。
### 培养新模式
#### 数据预处理
Fairseq-py源码分发包含了一个用于IWSLT 2014德语 - 英语语料库的预处理脚本示例。预处理和二值化数据如下:
```
$ cd data/
$ bash prepare-iwslt14.sh
$ cd ..
$ TEXT=data/iwslt14.tokenized.de-en
$ python preprocess.py --source-lang de --target-lang en \
--trainpref $TEXT/train --validpref $TEXT/valid --testpref $TEXT/test \
--thresholdtgt 3 --thresholdsrc 3 --destdir data-bin/iwslt14.tokenized.de-en
```
这将编写可用于模型训练的二进制数据`data-bin/iwslt14.tokenized.de-en`。
#### 训练
使用`python train.py`培养的新模式。这里有几个适用于IWSLT 2014数据集的示例设置:
```
$ mkdir -p checkpoints/fconv
$ CUDA_VISIBLE_DEVICES=0 python train.py data-bin/iwslt14.tokenized.de-en \
--lr 0.25 --clip-norm 0.1 --dropout 0.2 --max-tokens 4000 \
--arch fconv_iwslt_de_en --save-dir checkpoints/fconv
```
默认情况下,`python train.py`将使用您机器上的所有可用GPU。使用CUDA\_VISIBLE\_DEVICES环境变量来选择特定的GPU和/或更改将要使用的GPU设备的数量。
还要注意,批量大小是根据每批次的最大令牌数量来指定的`--max-tokens`。您可能需要使用较小的值,具体取决于系统上可用的GPU内存。
#### Generation
一旦您的模型被训练,您可以使用`python generate.py` (对于二进制化数据)或`python generate.py -i` (对于原始文本)生成翻译:
```
$ python generate.py data-bin/iwslt14.tokenized.de-en \
--path checkpoints/fconv/checkpoint_best.pt \
--batch-size 128 --beam 5
| [de] dictionary: 35475 types
| [en] dictionary: 24739 types
| data-bin/iwslt14.tokenized.de-en test 6750 examples
| model fconv
| loaded checkpoint trainings/fconv/checkpoint_best.pt
S-721 danke .
T-721 thank you .
...
```
要仅使用CPU生成翻译,请使用`--cpu`标志。可以用`--remove-bpe`标志移除BPE连续标记。
### 预先训练的模型
我们提供以下预训练的完全卷积序列到序列模型:
- [wmt14.en-fr.fconv-py.tar.bz2](https://s3.amazonaws.com/fairseq-py/models/wmt14.en-fr.fconv-py.tar.bz2):[WMT14英语 - 法语的](http://statmt.org/wmt14/translation-task.html#Download)预训练模型,包括词汇
- [wmt14.en-de.fconv-py.tar.bz2](https://s3.amazonaws.com/fairseq-py/models/wmt14.en-de.fconv-py.tar.bz2):[WMT14英语 - 德语的](https://nlp.stanford.edu/projects/nmt)预训练模型,包括词汇
此外,我们还提供了上述型号的预处理和二值化测试仪:
- [wmt14.en-fr.newstest2014.tar.bz2](https://s3.amazonaws.com/fairseq-py/data/wmt14.en-fr.newstest2014.tar.bz2):newstest2014测试集为WMT14英语 - 法语
- [wmt14.en-fr.ntst1213.tar.bz2](https://s3.amazonaws.com/fairseq-py/data/wmt14.en-fr.ntst1213.tar.bz2):newstest2012和newstest2013 WMT14测试集英文 - 法文
- [wmt14.en-de.newstest2014.tar.bz2](https://s3.amazonaws.com/fairseq-py/data/wmt14.en-de.newstest2014.tar.bz2):newstest2014测试集为WMT14英语 - 德语
使用二进制化测试集的生成可以按批处理模式运行,例如,对于GTX-1080ti上的英语 - 法语:
```
$ curl https://s3.amazonaws.com/fairseq-py/models/wmt14.en-fr.fconv-py.tar.bz2 | tar xvjf - -C data-bin
$ curl https://s3.amazonaws.com/fairseq-py/data/wmt14.en-fr.newstest2014.tar.bz2 | tar xvjf - -C data-bin
$ python generate.py data-bin/wmt14.en-fr.newstest2014 \
--path data-bin/wmt14.en-fr.fconv-py/model.pt \
--beam 5 --batch-size 128 --remove-bpe | tee /tmp/gen.out
...
| Translated 3003 sentences (95451 tokens) in 81.3s (1174.33 tokens/s)
| Generate test with beam=5: BLEU4 = 40.23, 67.5/46.4/33.8/25.0 (BP=0.997, ratio=1.003, syslen=80963, reflen=81194)
# Scoring with score.py:
$ grep ^H /tmp/gen.out | cut -f3- > /tmp/gen.out.sys
$ grep ^T /tmp/gen.out | cut -f2- > /tmp/gen.out.ref
$ python score.py --sys /tmp/gen.out.sys --ref /tmp/gen.out.ref
BLEU4 = 40.23, 67.5/46.4/33.8/25.0 (BP=0.997, ratio=1.003, syslen=80963, reflen=81194)
```
- Facebook页面:[https](https://www.facebook.com/groups/fairseq.users):[//www.facebook.com/groups/fairseq.users](https://www.facebook.com/groups/fairseq.users)
- Google群组:[https](https://groups.google.com/forum/#!forum/fairseq-users) : [//groups.google.com/forum/\#!forum/fairseq-users](https://groups.google.com/forum/#!forum/fairseq-users)
### 执照
fairseq-py是BSD许可的。许可证也适用于预培训的模型。我们还提供额外的专利授权。
原创文章,转载请注明 :[pytorch使用fairseq-py实现实现快速机器翻译(翻译的速度提高了80%,训练速度提升近50%) - pytorch中文网](https://ptorch.com/news/58.html)
原文出处: https://ptorch.com/news/58.html
问题交流群 :168117787 |
| Shard | 19 (laksa) |
| Root Hash | 18433012970862929819 |
| Unparsed URL | com,ptorch!/news/58.html s443 |