模型描述
Transformer 模型最初在论文 Attention Is All You Need 中提出,是一种强大的序列到序列建模架构,能够产生最先进的神经机器翻译 (NMT) 系统。
最近,fairseq 团队探索了使用回译数据对 Transformer 进行大规模半监督训练,在原始模型的基础上进一步提高了翻译质量。更多详情请参见这篇博客文章。
要求
我们只需要一些额外的 Python 依赖项进行预处理
pip install bitarray fastBPE hydra-core omegaconf regex requests sacremoses subword_nmt
英法翻译
使用论文 Scaling Neural Machine Translation 中的模型进行英法翻译
import torch
# Load an En-Fr Transformer model trained on WMT'14 data :
en2fr = torch.hub.load('pytorch/fairseq', 'transformer.wmt14.en-fr', tokenizer='moses', bpe='subword_nmt')
# Use the GPU (optional):
en2fr.cuda()
# Translate with beam search:
fr = en2fr.translate('Hello world!', beam=5)
assert fr == 'Bonjour à tous !'
# Manually tokenize:
en_toks = en2fr.tokenize('Hello world!')
assert en_toks == 'Hello world !'
# Manually apply BPE:
en_bpe = en2fr.apply_bpe(en_toks)
assert en_bpe == 'H@@ ello world !'
# Manually binarize:
en_bin = en2fr.binarize(en_bpe)
assert en_bin.tolist() == [329, 14044, 682, 812, 2]
# Generate five translations with top-k sampling:
fr_bin = en2fr.generate(en_bin, beam=5, sampling=True, sampling_topk=20)
assert len(fr_bin) == 5
# Convert one of the samples to a string and detokenize
fr_sample = fr_bin[0]['tokens']
fr_bpe = en2fr.string(fr_sample)
fr_toks = en2fr.remove_bpe(fr_bpe)
fr = en2fr.detokenize(fr_toks)
assert fr == en2fr.decode(fr_sample)
英德翻译
半监督训练与回译相结合是改进翻译系统的有效方法。在论文 Understanding Back-Translation at Scale 中,我们回译了超过 2 亿句德语句子作为额外的训练数据。由其中五个模型组成的集成模型是 WMT'18 英德新闻翻译竞赛 的获胜提交。
我们可以通过 noisy-channel reranking 进一步改进这种方法。更多详情请参见这篇博客文章。使用这种技术训练的模型集成是 WMT'19 英德新闻翻译竞赛 的获胜提交。
使用获胜提交中的一个模型进行英德翻译
import torch
# Load an En-De Transformer model trained on WMT'19 data:
en2de = torch.hub.load('pytorch/fairseq', 'transformer.wmt19.en-de.single_model', tokenizer='moses', bpe='fastbpe')
# Access the underlying TransformerModel
assert isinstance(en2de.models[0], torch.nn.Module)
# Translate from En-De
de = en2de.translate('PyTorch Hub is a pre-trained model repository designed to facilitate research reproducibility.')
assert de == 'PyTorch Hub ist ein vorgefertigtes Modell-Repository, das die Reproduzierbarkeit der Forschung erleichtern soll.'
我们也可以进行一次往返翻译来创建一个释义
# Round-trip translations between English and German:
en2de = torch.hub.load('pytorch/fairseq', 'transformer.wmt19.en-de.single_model', tokenizer='moses', bpe='fastbpe')
de2en = torch.hub.load('pytorch/fairseq', 'transformer.wmt19.de-en.single_model', tokenizer='moses', bpe='fastbpe')
paraphrase = de2en.translate(en2de.translate('PyTorch Hub is an awesome interface!'))
assert paraphrase == 'PyTorch Hub is a fantastic interface!'
# Compare the results with English-Russian round-trip translation:
en2ru = torch.hub.load('pytorch/fairseq', 'transformer.wmt19.en-ru.single_model', tokenizer='moses', bpe='fastbpe')
ru2en = torch.hub.load('pytorch/fairseq', 'transformer.wmt19.ru-en.single_model', tokenizer='moses', bpe='fastbpe')
paraphrase = ru2en.translate(en2ru.translate('PyTorch Hub is an awesome interface!'))
assert paraphrase == 'PyTorch is a great interface!'
参考
- Attention Is All You Need
- Scaling Neural Machine Translation
- Understanding Back-Translation at Scale
- Facebook FAIR 的 WMT19 新闻翻译任务提交