Self-attention原理 以及 与CNN,RNN对比

Self-attention

整体架构

每次输入的Sequence长度不一样,但输出数量要与输入数量相同 (例如:词性标注)

  • 目的:考虑整个input sequence的上下文资讯
  • Self-attention输出也要接入FC得到结果

图中用红颜色括号标起来的vector考虑了整个sequence的内容,此时Fully-connect考虑了整个Sequence的内容

可以将网络进行叠加:Fully-Connect与Self-attention交替使用

​ Self-attention处理整个Sequence的内容

​ Fully-Connect专注处理某个位置的内容

相关论文链接:Attention Is All You Need

运行原理

image-20211026214118669

  • 输入:可以是整个网络的input,也可以是做过某些处理
  • 输出:考虑了整个Sequence产生的
  • 先计算Q,K,V,后计算attention score,再根据其score对v做weight sum来抽取重要信息

计算attention score

以求出 a1 的输出 b1 为例子

image-20211026214643786

Self-attention目的是为了考虑整个Sequence,但是又不是所有的Sequence的内容都是重要的,所以需要进行判断相关性

计算两个向量的关联程度 α \alpha α的方法

image-20211026214834709

左边的Dot-product常用在了Tranformer中

在Self-attention中计算attention score: α \alpha α

image-20211026215501217

可以不用Soft-max,有时会用ReLU

  • 此时得到attention score α ‘ \alpha^‘ α (计算过程是上面的:Dot-product)
  • 经过softmax归一化后,我们可以将 α ‘ \alpha^‘ α称作:注意力权重

根据attention score来抽取重要的信息

image-20211026215959247

  • 由公式:当关联性越高,对应的 α \alpha α很大,最后得到的b就越接近关联程度越高的对应的 v 向量

从矩阵乘法的角度思考

  1. q,k,v的计算

image-20211026220735086

由之前的每个图中计算q,k,v向量的公式,从矩阵乘法角度看,整体可以简化

  1. 计算attention score

image-20211026221156382

由之前图中的公式可以得到: α = k T ∗ q \alpha=k^T * q α=kTq,对全部的 α \alpha α使用矩阵乘法进行简化即可得图中计算

  1. 抽取信息

image-20211026223221263

  • 注意vi 与 ki 产生的 α ‘ \alpha^` α相乘
  1. 总结

先产生q,k,v;再根据q找出相关的位置;再对v做weight sum

image-20211026222220305

进阶版本 - Multi-head Self-attention

定义相关性时,是使用q去找相关的k

但相关这件事有不同的定义,所以需要不同的q负责不同的相关性

2个heads 为例子:

以下进行求解结果矩阵O中一个元素 b i b^i bi
image-20211026222838760

image-20211026222905120

不同的 head 分别计算,计算过程如之前内容一样

b i , j b^{i,j} bi,j

j 表示:第 j 个heads产生的b;
i 表示:第 j 个heads中第i个q (即 q i , j q^{i,j} qi,j) 去与对应的k计算产生的b

此时会得到两个结果矩阵,再将两个拼接起来,再乘以一个矩阵的转置得到最后的结果

image-20211026223342557

相当将 b i , j 1 b^{i,j1} bi,j1 b i , j 2 b^{i,j2} bi,j2进行了拼接(即:对应 i i i 相同的 b 都拼接在一起),再经过计算后得到了 b i b^i bi


最后的结果O是 多个由 b b b 组成 (如1heads中的图示)

  • 上述拼接操作可以理解为:将第 i i i个q ( q i q^i qi) 所在多个heads产生的b (b^{i,…}) 全部进行拼接

Positional Encoding - 加入位置信息

  • Self-attention的过程可以看到,并没有位置相关的信息

为每一个位置设置不同的positional vector,将其加到输入a上

但产生positional vector是一个在研究的问题

参考论文:Learning to Encode Position for Transformer with Continuous Dynamical Model

应用

  1. NLP

image-20211026224220505

  1. Self-attention for Speech - Truncated Self-attention

image-20211026224547208

  1. for Image

image-20211026224714074

对于Self-attention处理:图中矩阵可以看成5*10个vector,每个vector是3维向量

image-20211026224845523

Self-attention VS. CNN

CNN是简化版的Self-attention

CNN考虑的是一个receptive field的信息

Self-attention考虑的是整个图片的信息

Self-attention会考虑每个向量的相关性,所以其receptive field可以看出自己学习出来的

而CNN的receptive field是自己指定的

相关论文:

image-20211027200305208

Self-attention相比CNN更加的flexible,需要更多的data,否则更可能over fitting

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-OsoiaNK8-1635337818356)(C:/Users/pc/AppData/Roaming/Typora/typora-user-images/image-20211027200700993.png)]

Self-attention VS. RNN

image-20211027201504974

  1. RNN没有办法并行化产生输出,只能一个接一个输出;

    Self-attention可以并行化输出

  2. RNN当结果要考虑比相对较远输入的位置时,比较难以考虑到;

    Self-attention可以很好的对输入位置比较远的向量进行考虑

  3. RNN每个输出只考虑了其左边的输入,没有考虑右边的输入

    Self-attention则考虑了整个Sequence

learn more

image-20211027202436125

Reference

(强推)李宏毅2021春机器学习课程

  • 3
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Training deep models for lane detection is challenging due to the very subtle and sparse supervisory signals in- herent in lane annotations. Without learning from much richer context, these models often fail in challenging sce- narios, e.g., severe occlusion, ambiguous lanes, and poor lighting conditions. In this paper, we present a novel knowl- edge distillation approach, i.e., Self Attention Distillation (SAD), which allows a model to learn from itself and gains substantial improvement without any additional supervision or labels. Specifically, we observe that attention maps ex- tracted from a model trained to a reasonable level would encode rich contextual information. The valuable contex- tual information can be used as a form of ‘free’ supervision for further representation learning through performing top- down and layer-wise attention distillation within the net- work itself. SAD can be easily incorporated in any feed- forward convolutional neural networks (CNN) and does not increase the inference time. We validate SAD on three pop- ular lane detection benchmarks (TuSimple, CULane and BDD100K) using lightweight models such as ENet, ResNet- 18 and ResNet-34. The lightest model, ENet-SAD, per- forms comparatively or even surpasses existing algorithms. Notably, ENet-SAD has 20 × fewer parameters and runs 10 × faster compared to the state-of-the-art SCNN [16], while still achieving compelling performance in all bench- marks. Our code is available at https://github. com/cardwing/Codes-for-Lane-Detection.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值