知行编程网知行编程网  2022-06-25 13:00 知行编程网 隐藏边栏 |   抢沙发  24 
文章评分 0 次,平均分 0.0

只需几行代码,改进Transformer自注意力机制(几乎不增加计算量)

来自 | 知乎   作者 | PENG Bo

链接 | https://zhuanlan.zhihu.com/p/191393788

编辑 | 深度学习这件小事

本文仅作学术交流,如有侵权,请联系后台删除。

只需几行代码,改进Transformer自注意力机制(几乎不增加计算量)
本文提出两种技巧,提升 NLP 任务中自注意力的效果,同时均有一定道理。

方法只需几行代码,即插即用,几乎不增加运算量和不增加参数量,而且训练速度更快。

我估计修改后的 1 层相当于从前的 ~1.2 层。

这里和后续的改进会放到这个项目:

https://github.com/BlinkDL/minGPT-tuned

   进一:不妨称为 "Time-weighting"
方法是,在计算 softmax(Q dot K) 后,对每个点做一次加权(这个很明显,估计肯定有人提出过,不过本文后面的改进二应该就是全新的了)。
Pytorch 代码如下,只增加少量参数:
这个改进,有两个原因。
第一,不同距离的 token,对于我们所关注位置的贡献,理应不同。
第二,对于训练时靠近开头的 token,由于观察窗口较小,信息量相对低,理应降低自注意力的整体权重。
下图是典型的训练出的 time_weighting,很光滑:
只需几行代码,改进Transformer自注意力机制(几乎不增加计算量)
右边的凸起是 local context 效应,左边的凸起是 global context 效应。有趣的是中间略低,说明在距离20个字左右时,写作者会避免累赘重复。
进一步思考,可以精确计算出通用的加权曲线(这有人做过吗?)。留作后续研究。

   改进二:不妨称为 "Time-mixing"

这个操作很特别,应该没有人提出过。它来自于我对自注意力机制的思考。
我认为,自注意力机制,其实在做三种事情:
第一,把 global context 加到每个字上。
第二,让每个字的意图逐渐统一。
第三,重复之前出现过的字组合。例如,如果最近出现了AB,我们在再遇到A时,下一个字是B的概率显然在Bayesian意义上更大了。这是一种常见的语言现象,对应语言的长程关联中的 burst 性质。
然而,如果仔细观察目前的自注意力模块的设计,会发现,它并不擅长直接完成任务三,而是只能用拐弯抹角的方法完成。这会降低学习效率,网络还可能会用过拟合的错误方式完成此任务。
通过使用这里的 "Time-mixing" 机制,可让模块直接学会任务三。
我用一个特别的 trick 解决了这个问题,代码也很简单:
你能看出来它在干什么吗?
这不但解决了任务三,而且相当于引入了额外的 local attention 层,效果也很明显。

   改进后的效果
Perplexity 曲线,训练更快,最终效果更好:
只需几行代码,改进Transformer自注意力机制(几乎不增加计算量)
欢迎关注项目:
https://github.com/BlinkDL/minGPT-tuned
<section style="white-space: normal;line-height: 1.75em;text-align: center;"><strong style="color: rgb(0, 0, 0);font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;widows: 1;background-color: rgb(255, 255, 255);max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;letter-spacing: 0.5px;font-size: 14px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><strong style="max-width: 100%;font-size: 16px;letter-spacing: 0.544px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;letter-spacing: 0.5px;font-size: 14px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><strong style="max-width: 100%;font-size: 16px;letter-spacing: 0.544px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;letter-spacing: 0.5px;box-sizing: border-box !important;overflow-wrap: break-word !important;"></span></strong></span></strong></span></strong></section><pre><pre style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section style="max-width: 100%;letter-spacing: 0.544px;white-space: normal;font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;widows: 1;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section powered-by="xiumi.us" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section style="margin-top: 15px;margin-bottom: 25px;max-width: 100%;opacity: 0.8;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section style="max-width: 100%;letter-spacing: 0.544px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section powered-by="xiumi.us" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section style="margin-top: 15px;margin-bottom: 25px;max-width: 100%;opacity: 0.8;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section><section style="margin-bottom: 15px;padding-right: 0em;padding-left: 0em;max-width: 100%;color: rgb(127, 127, 127);font-size: 12px;font-family: sans-serif;line-height: 25.5938px;letter-spacing: 3px;text-align: center;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;color: rgb(0, 0, 0);box-sizing: border-box !important;overflow-wrap: break-word !important;"><strong style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 16px;font-family: 微软雅黑;caret-color: red;box-sizing: border-box !important;overflow-wrap: break-word !important;">为您推荐</span></strong></span></section><section style="margin-top: 5px;margin-bottom: 5px;padding-right: 0em;padding-left: 0em;max-width: 100%;min-height: 1em;font-family: sans-serif;letter-spacing: 0px;opacity: 0.8;line-height: normal;text-align: center;box-sizing: border-box !important;overflow-wrap: break-word !important;">吴恩达推荐笔记:22 张图总结深度学习全部知识</section><section style="margin-top: 5px;margin-bottom: 5px;padding-right: 0em;padding-left: 0em;max-width: 100%;min-height: 1em;font-family: sans-serif;letter-spacing: 0px;opacity: 0.8;line-height: normal;text-align: center;color: rgb(0, 0, 0);box-sizing: border-box !important;overflow-wrap: break-word !important;">图灵奖得主Yann LeCun《深度学习》春季课程</section><section style="margin-top: 5px;margin-bottom: 5px;padding-right: 0em;padding-left: 0em;max-width: 100%;min-height: 1em;font-family: sans-serif;letter-spacing: 0px;opacity: 0.8;line-height: normal;text-align: center;color: rgb(0, 0, 0);box-sizing: border-box !important;overflow-wrap: break-word !important;">你一定从未看过如此通俗易懂的YOLO系列解读 (下)</section><section style="margin-top: 5px;margin-bottom: 5px;padding-right: 0em;padding-left: 0em;max-width: 100%;min-height: 1em;font-family: sans-serif;letter-spacing: 0px;opacity: 0.8;line-height: normal;text-align: center;color: rgb(0, 0, 0);box-sizing: border-box !important;overflow-wrap: break-word !important;">22课时、19大主题,CS 231n进阶版课程视频上线</section><section style="margin-top: 5px;margin-bottom: 5px;padding-right: 0em;padding-left: 0em;max-width: 100%;min-height: 1em;font-family: sans-serif;letter-spacing: 0px;opacity: 0.8;line-height: normal;text-align: center;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="color: rgb(87, 107, 149);font-size: 14px;">震惊!这个街道办招8人,全是清华北大博士硕士!</span><br  /></section></section></section></section></section></section></section></section></section>
只需几行代码,改进Transformer自注意力机制(几乎不增加计算量)

本篇文章来源于: 深度学习这件小事

本文为原创文章,版权归所有,欢迎分享本文,转载请保留出处!

知行编程网
知行编程网 关注:1    粉丝:1
这个人很懒,什么都没写

发表评论

表情 格式 链接 私密 签到
扫一扫二维码分享