Transformer原理详解

Transformer模型是在《Attention is All You Need》中提出的,最初是为了提高机器翻译的效率,它的 Self-Attention 机制和 Position Encoding 可以替代 RNN。因为 RNN 是顺序执行的,t 时刻没有处理完成就不能处理 t+1 时刻,因此很难并行。在介绍Self-Attention 机制之前,我们先介绍 Attention 机制。

1. Attention机制

Attention机制最早是在计算机视觉图像领域提出来的,但是真正火起来应该算是google mind团队的这篇论文 《Recurrent Models of Visual Attention》,他们在RNN模型上使用了attention机制来进行图像分类。随后,Dzmitry Bahdanau 等人在论文 《Neural Machine Translation by Jointly Learning to Align and Translate》中提出,在sequence-to-sequence模型中,只用encoder的最后一个隐层结果作为整个句子的编码或者作为decoder的输入是有缺陷的,尤其是RNN机制实际中存在长程梯度消失的问题,对于较长的句子,我们很难寄希望于将输入的序列转化为定长的向量而保存所有的有效信息,所以随着所需翻译句子的长度的增加,这种结构的效果会显著下降;并且对于decoder的输出结果,不同位置上的输出结果会更加依赖于相对应位置的输入,像在机器翻译中,输出的第一个词一般会基于输入序列的前面部分词语,而输出的最后一个词一般会基于输入序列靠后面的词。



参考文献:

1. http://web.stanford.edu/class/cs224n/

2.  好文推荐:https://jalammar.github.io/illustrated-transformer/

3.  好文推荐:https://www.cnblogs.com/robert-dlut/p/5952032.html

4.  好文推荐:http://fancyerii.github.io/2019/03/09/transformer-illustrated/

5. Neural Machine Translation by Jointly Learning to Align and Translate

6. https://zhuanlan.zhihu.com/p/47063917

7. 代码了解:http://nlp.seas.harvard.edu/2018/04/03/attention.html

8. https://zhuanlan.zhihu.com/p/63191028

9. https://zhuanlan.zhihu.com/p/44731789

10. https://towardsdatascience.com/how-to-code-the-transformer-in-pytorch-24db27c8f9ec 里attention的代码

11. https://www.slideshare.net/gohyunwoong/bert-pretraining-of-deep-bidirectional-transformers-for-language-understanding-186258842

我的公众号:欢迎交流学习


你可能感兴趣的:(Transformer原理详解)