Transformer
简介
看到这个,不知道你是否会感到奇怪,为什么要单独把一个奇怪的英文单词提出来单独放到一章内容里面来?或者说这个词可以翻译成变形金刚,变形金刚和深度学习有什么联系吗?
这一切要从 2017 年一篇命名都很清奇的文章说起:Attention Is All You Need
他本来是用于机器翻译的文章,属于 NLP 领域,但是他所提出的框架 transformer 一时声名鹊起,首先是在 NLP 领域,Bert 及其无数改进席卷了整个领域,大规模的预训练模型广泛地被应用于各个任务内。
随后在 CV 领域中,VIT 的横空出世标志着 transformer 几乎一统了深度学习的多数领域,直到现在,一篇文章十有八九会用但 transformer 的相关内容。
并且,不少人也尝试去验证他的可解释性,目前为止给我印象最深的就是新加坡的一位教授发的文章,他把其中一个重要模块换成了 POOLING 层,依然有还不错的效果。
在本章节中我会尝试带你走进这个神奇的算法框架,但是在这之前,有几点是需要声明的:
- 鉴于我们视野的局限性,我无法很好的阐述清楚 NLP 领域的知识,但是有些内容是必要的,因此我会留下一下重要的教程供你参考。如果有机会我会进行补充
- 并不是你接触 NLP 或者了解一些任务,你就必须得把 RNN,LSTM 这些经典网络弄得烂熟于心,完全没必要,你可以让你对他们保持一个大体上的理解,希望这可以帮助你节约更多的时间。
- 该算法耗费的算力资源非常大,有时候想跑起来一个模型可能需要更大的显存
必要知识
Sequence to Sequence
序列到序列学习,一个有趣的概念
A ten-minute introduction to sequence-to-sequence learning in Keras
你可以实操一下,它是调包 LSTM 的。你也可以借此了解一下他的概念,不用太精通。
注意力机制
大家不需要觉得它非常神秘,因为他其实本身就是矩阵运算的一种操作罢了,取了个好听的名字,我在很早的目标检测文章上看到过类似的机制,不过那时候没有这个名字罢了。
他很重要的一点在于处理序列数据上的优越性,在这里附上一个视频帮助大家学习
但是在 transformer 内,他使用的是名为 self attention 的东西,这点需要你额外进行学习
论文的优秀讲解
Transformer 中 Self-Attention 以及 Multi-Head Attention 详解_哔哩哔哩_bilibili
除此之外就是相关代码,不要求你可以完全自己复现,但是要保证非常重要的知识都懂
再贴上一个难度稍高的教程
Vision Transformer 超详细解读 (原理分析 + 代码解读) (一)
思考题
我在这里留下一些思考题希望对你有启发
在 Multi-head Self-attention 中所谓 Q,K,V 到底指代的是什么?他们怎么产生的?
位置编码有没有更好的形式产生?
为什么用的是 LN 层?为什么不用 BN 层?
你能不能阅读一下 transformer 的代码并且能明确说出哪些维度变换所对应的操作呢?