机器之心编译
参与:黄小天、Smith
谷歌前不久在 arXiv 上揭橥论文《Attention Is All You Need》,提出一种完备基于 attention 的翻译架构 Transformer,实现了机器翻译的新打破;近日,Github 上的一个项目给出了 Transformer 模型的 TensorFlow 实现,在官方代码公布之前共享了自己的代码。机器之心对该文进行了编译,项目地址见文中。

需求
NumPy >= 1.11.1
TensorFlow >= 1.2(1.1 很可能也可以,但是我不测试它)
regex
nltk
项目来由
我试图在论文《Attention Is All You Need》中实现我的想法。该论文的作者声称其模型,即 Transformer,在机器翻译方面的表现优于当前任何的模型;它仅利用 attention,而没有 CNN 和 RNN,这酷极了。论文末了,作者承诺将很快公开代码,但是目前为止并没有。我的这一项目有两个目标,一是我想要全面理解这篇论文,如果不理解就开始写代码对我来讲很困难;二是在官方代码公布之前,与感兴趣的人共享我写的代码。
内容,而是要实现论文的核心思想,并作出大略快速的验证。由于这个缘故原由,我的部分代码与原论文有所不同。这些不同之处有:
我利用了 IWSLT 2016 de-en 数据集,而不是 wmt 数据集,由于前者更小,且不须要分外的预处理。
出于大略考虑,我构建了带有单词的词汇,而不是子单词。当然,如果乐意你可以考试测验 bpe 或者 word-piece。
我参数化了位置编码。论文利用了一些正弦公式,但是论文的作者之一 Noam 说它们都有效。详见 reddit 谈论(https://www.reddit.com/r/MachineLearning/comments/6gwqiw/r_170603762_attention_is_all_you_need_sota_nmt/)。
论文调节了所有步骤的学习率。我把学习大略地固定在了一个小数 0.0001 上,由于利用小数据集,演习的速率已经足够快(利用单块 GTX 1060 仅需几个小时!
!
)
文件描述
hyperparams.py 包括全部所需的超参数
prepro.py 可为源和目标创建词汇文件夹(vocabulary file)
data_load.py 包括装载和批处理数据的干系函数
modules.py 拥有全部编码/解码网络的构建模块
train.py 包含模型
eval.py 是为了进行评估
演习
第一步:下载 IWSLT 2016 German–English parallel corpus (https://wit3.fbk.eu/download.php?release=2016-01&type=texts&slang=de&tlang=en) 并且把它放在 corpora/文件夹
第二步:如果必要的话在 hyperparams.py 下调度超参数(hyper parameters)
第三步:运行 prepro.py,在 preprocessed 文件下天生词汇文件
第四步:运行 train.py 或下载预演习好的文件(pretrained files (https://u42868014.dl.dropboxusercontent.com/u/42868014/transformer/logdir.zip))
演习丢失和精度
演习丢失
演习精度
评估
运行 eval.py.
结果
我的 BLEU 得分为 17.14。(我用小数据集、有限的词汇进行演习)一些评估结果如下所示。详见文件夹 results
source: Sie war eine jährige Frau namens Alex
expected: She was a yearold woman named Alex
got: She was a woman named yearold name
source: Und als ich das hörte war ich erleichtert
expected: Now when I heard this I was so relieved
got: And when I heard that I was an
source: Meine Kommilitonin bekam nämlich einen Brandstifter als ersten Patienten
expected: My classmate got an arsonist for her first client
got: Because my first came from an in patients
source: Das kriege ich hin dachte ich mir
expected: This I thought I could handle
got: I'll go ahead and I thought
source: Aber ich habe es nicht hingekriegt
expected: But I didn't handle it
got: But I didn't it
source: Ich hielt dagegen
expected: I pushed back
got: I thought about it
source: Das ist es was Psychologen einen AhaMoment nennen
expected: That's what psychologists call an Aha moment
got: That's what a like a
source: Meldet euch wenn ihr in euren ern seid
expected: Raise your hand if you're in your s
got: Get yourself in your s
source: Ich möchte ein paar von euch sehen
expected: I really want to see some twentysomethings here
got: I want to see some of you
source: Oh yeah Ihr seid alle unglaublich
expected: Oh yay Y'all's awesome
got: Oh yeah you all are incredibly
source: Dies ist nicht meine Meinung Das sind Fakten
expected: This is not my opinion These are the facts
got: This is not my opinion These are facts