我们以机器翻译为例,来探究引入注意力机制Attention:
当我们使用Seq2Seq 进行机器翻译的过程中,最后的输入对结果影响响度比较大,因为Encode的输出是在尾部(图中粉红色部分)。直觉上想,一段话的翻译的的过程中,输入句子的每一个部分都可能对正在翻译的内容会有不同的影响。
(1) 我们定义z与h进行match,二者先看一下模型示意图:
(2) z与h进行match 产生a, 究竟Macht是什么,可以自己定义:
(3) 然后将不同timestep的a进行softmax, 得到c,
c为Decoder 的输入
(4) Attention 机制随着时间处理的示意图
(1) 语音识别
不通对不同声音信号的的注意力情况,生成字母甚至空格,参考 William Chan, Navdeep Jaitly, Quoc
V. Le, Oriol Vinyals, “Listen, Attend and Spell”, ICASSP, 2016
虽然结果没有查过HMM等传统的方法,但是证明attention机制的方法是可以work的
(2) 看图说话 Image Caption Generation
回顾CNN,我们有很多filter,针对filter,可以加入attention机制
计算出来weighted sum 输入Decoder
在论文
Kelvin Xu, Jimmy Ba, Ryan Kiros, Kyunghyun Cho, Aaron Courville, Ruslan Salakhutdinov, Richard Zemel, Yoshua Bengio, “Show, Attend and Tell: Neural Image Caption Generation with Visual Attention”, ICML, 2015
中,我们可以看好很好的效果。
在论文Kelvin Xu, Jimmy Ba, Ryan Kiros, Kyunghyun Cho, Aaron Courville, Ruslan Salakhutdinov, Richard Zemel, Yoshua Bengio, “Show, Attend and Tell: Neural Image Caption Generation with Visual Attention”, ICML, 2015 中针对视频使用attention机制,但是效果不佳,attention总是在某个桢的图像中,其他桢的很少,导致得出的句子很突兀。如图所示,
w2和w4都是woman。
那什么是好的attention机制呢,这篇论文告诉我们
Good Attention: each input component has approximately the same attention weight
于是我们增加Regularization term(这个式子不固定,可以自己设定)
其中i是component(每一帧),t是时间
本专栏图片、公式很多来自台湾大学李弘毅老师、斯坦福大学cs229,斯坦福大学cs231n 、斯坦福大学cs224n课程。在这里,感谢这些经典课程,向他们致敬!