0
雷鋒網(wǎng) AI科技評論按:今年5月,F(xiàn)acebook AI研究院(FAIR)發(fā)表了他們的研究成果fairseq,在fairseq中,他們使用了一種新型的卷積神經(jīng)網(wǎng)絡(luò)來做語言翻譯,比循環(huán)神經(jīng)網(wǎng)絡(luò)的速度快了9倍,而且準(zhǔn)確性也是現(xiàn)有模型中最高的。此外,他們在GitHub公布了fair序列建模工具包的源代碼和訓(xùn)練好的系統(tǒng),其他的研究者可以在此基礎(chǔ)上建立自己的關(guān)于翻譯、文本總結(jié)和其他任務(wù)的模型。
詳情可參見快9倍!Facebook開源機(jī)器學(xué)習(xí)翻譯項(xiàng)目fairseq一文。
日前,F(xiàn)acebook AI研究團(tuán)隊(duì)又在GitHub上開源了fairseq PyTorch版本。
相關(guān)介紹
fairseq是Facebook AI研究院發(fā)布的一個(gè)序列到序列的學(xué)習(xí)工具,它的原作者(排名不分先后)是Sergey Edunov、Myle Ott和Sam Gross。該工具包能實(shí)現(xiàn) Convolutional Sequence to Sequence Learning(地址:https://arxiv.org/abs/1705.03122)中描述的全卷積模型,并能在一臺機(jī)器上進(jìn)行多GPU訓(xùn)練,也能在CPU和GPU上快速產(chǎn)生束搜索(beam search)。在開源的數(shù)據(jù)中,他們提供了英譯法和英譯德的預(yù)訓(xùn)練模型。
引用
如果你的論文中用了FAIR的相關(guān)代碼,可以這樣引用:
@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系統(tǒng)的電腦
要是想訓(xùn)練新的模型,需要用到NVIDIA GPU和NCCL(https://github.com/NVIDIA/nccl)
Python 3.6
目前的fairseq-py需要從GitHub庫中獲得PyTorch,有多種方式安裝它。我們建議利用Miniconda3,執(zhí)行如下的步驟。
1、安裝Miniconda3(https://conda.io/miniconda.html);激活 Python 3環(huán)境
2、安裝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
3、在GitHub中復(fù)制和執(zhí)行如下代碼來安裝fairseq-py
pip install -r requirements.txt
python setup.py build
python setup.py develop
快速開始
你將需要使用到如下的命令:
python preprocess.py: 數(shù)據(jù)預(yù)處理: 構(gòu)造詞匯和二進(jìn)制訓(xùn)練數(shù)據(jù)
python train.py: 在一個(gè)或多個(gè)GPU上訓(xùn)練新的模型
python generate.py: 用訓(xùn)練好的模型翻譯預(yù)處理之后的數(shù)據(jù)
python generate.py -i:用訓(xùn)練好的模型翻譯新的文本
python score.py: 通過與參考譯文對比,給出生成譯文的BLEU分?jǐn)?shù)
評估預(yù)訓(xùn)練模型:
首先,下載預(yù)訓(xùn)練好的模型和詞匯:
$ curl https://s3.amazonaws.com/fairseq-py/models/wmt14.en-fr.fconv-py.tar.bz2 | tar xvjf -
模型中用的是BPE詞匯(https://arxiv.org/abs/1508.07909),用戶必須在翻譯之前將編碼應(yīng)用到源文本??梢杂?a target="_blank" rel=nofollow>apply_bpe.py 腳本中的wmt14.en-fr.fconv-cuda/bpecodes文件。@@是延續(xù)標(biāo)記,原始文本可以通過sed s/@@ //g來恢復(fù),此外把 --remove-bpe 標(biāo)記傳遞到generate.py也有同樣的作用。在生成BPE詞匯之前。輸入文本需要用mosesdecoder中的tokenizer.perl來標(biāo)記。
下面是利用python generate.py -i產(chǎn)生翻譯的例子, beam size為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
訓(xùn)練新模型
數(shù)據(jù)預(yù)處理
fairseq-py工具包中包含用于IWSLT 2014德轉(zhuǎn)英語料庫的一個(gè)預(yù)處理腳本樣例。先將數(shù)據(jù)進(jìn)行預(yù)處理和二進(jìn)制編碼:
$ 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
這將會(huì)得到能夠用于訓(xùn)練模型的二進(jìn)制數(shù)據(jù)。
訓(xùn)練
用python train.py來訓(xùn)練新的模型,下面是能很好的適于 IWSLT 2014數(shù)據(jù)集中的一些樣例設(shè)置。
$ 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
默認(rèn)情況下,python train.py會(huì)占用電腦中所有可用的GPU,可以用CUDA_VISIBLE_DEVICES環(huán)境來選擇特定的GPU,或者改變使用的GPU數(shù)目。
有一點(diǎn)需要注意,batch大小是基于每個(gè)batch的最大token數(shù)來設(shè)置的,你需要基于系統(tǒng)中可用的GPU內(nèi)存,選取一個(gè)稍小的值。
生成翻譯
模型訓(xùn)練好之后就能利用python generate.py(用于二進(jìn)制數(shù)據(jù))或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 .
...
如果只想用一個(gè)CPU,加入--cpu標(biāo)記??梢酝ㄟ^--remove-bpe移除掉BPE標(biāo)記。
訓(xùn)練好的模型
目前開源的全卷積序列到序列模型如下:
wmt14.en-fr.fconv-py.tar.bz2(https://s3.amazonaws.com/fairseq-py/models/wmt14.en-fr.fconv-py.tar.bz2): 用于WMT14英譯法的模型,包含詞匯
wmt14.en-de.fconv-py.tar.bz2(https://s3.amazonaws.com/fairseq-py/models/wmt14.en-de.fconv-py.tar.bz2): 用于WMT14英譯德的模型,包含詞匯
針對以上模型,已經(jīng)預(yù)處理和編碼過的測試集如下:
wmt14.en-fr.newstest2014.tar.bz2(https://s3.amazonaws.com/fairseq-py/data/wmt14.en-fr.newstest2014.tar.bz2): 用于WMT14英譯法的newstest2014測試集
wmt14.en-fr.ntst1213.tar.bz2(https://s3.amazonaws.com/fairseq-py/data/wmt14.en-fr.ntst1213.tar.bz2): 用于WMT14英譯法的newstest2012和newstest2013測試集
wmt14.en-de.newstest2014.tar.bz2(https://s3.amazonaws.com/fairseq-py/data/wmt14.en-de.newstest2014.tar.bz2): 用于WMT14英譯德的newstest2014測試集
下面是在一塊GTX-1080ti上利用測試集產(chǎn)生結(jié)果的樣例(英譯德),運(yùn)行在batch模式下:
$ 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)
via:GitHub(https://github.com/facebookresearch/fairseq-py)
雷鋒網(wǎng)AI科技評論編譯整理。雷鋒網(wǎng)
雷峰網(wǎng)版權(quán)文章,未經(jīng)授權(quán)禁止轉(zhuǎn)載。詳情見轉(zhuǎn)載須知。