机器学习与深度学习系列连载: 第二部分 深度学习(十九) 注意力机制 Attention

注意力机制 Attention

我们以机器翻译为例,来探究引入注意力机制Attention:

当我们使用Seq2Seq 进行机器翻译的过程中,最后的输入对结果影响响度比较大,因为Encode的输出是在尾部(图中粉红色部分)。直觉上想,一段话的翻译的的过程中,输入句子的每一个部分都可能对正在翻译的内容会有不同的影响。
在这里插入图片描述

1. 模型定义

(1) 我们定义z与h进行match,二者先看一下模型示意图:
在这里插入图片描述
(2) z与h进行match 产生a, 究竟Macht是什么,可以自己定义:

  • Cosine similarity of z and h
  • Small NN whose input is z and h, output a scalar

在这里插入图片描述
(3) 然后将不同timestep的a进行softmax, 得到c,
在这里插入图片描述
c为Decoder 的输入
在这里插入图片描述
(4) Attention 机制随着时间处理的示意图
在这里插入图片描述

2. 应用场景

(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
中,我们可以看好很好的效果。
在这里插入图片描述

3. 使用建议

在论文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课程。在这里,感谢这些经典课程,向他们致敬!

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值