文章目录
摘要
Transformer是一个利用注意力机制来提高模型训练速度的模型,主要有两部分组成:Encoder 和 Decoder。Transformer适用于并行化计算,模型本身的复杂程度导致它在精度和性能上都要高于之前流行的RNN循环神经网络。该模型广泛应用于NLP领域,例如机器翻译,问答系统,文本摘要和语音识别等等方向。
Abstract
Transformer is a model that uses attention mechanism to improve the training speed, which mainly consists of two parts: Encoder and Decoder. Transformer is suitable for parallel computing, and the complexity of the model itself leads to its higher accuracy and performance than the popular RNN. Transformer is widely used in NLP fields, such as machine translation, question answering system, text summarization and speech recognition.
RNN
随着poch增大,RNN的loss不一定会变小,RNN是比较难训练的模型。
举例说明出现上述现象的原因:
Helpful Techniques
LSTM can deal with gradient vanishing(解决梯度过小的问题),not gradient explode.
forgetgate在多数情况下开启,新的方法GRU比LSTM参数更少,避免overfitting。
Transformer
输入一个序列,输出一个序列,该序列由机器自己决定。
例如speech recognition、machine translation、speech translation(适用于language without text)
有些语言没有文字或者文字晦涩难懂,直接进行语音翻译。
Text-to-speech(语言合成,输入文字输出语言)
Seq2seq for Chatbot(收集大量对话训练聊天机器人)
transformer广泛应用于natural language processing. QA问题。
Seq2seq for Syntactic Parsing(文法剖析)
Seq2seq for Multi-label Classification(an object can belong to multiple classes)
Multi-class Classification(从多个种类中选出一个class)
如何操作seq2seq?
Encoder
具体的一个block过程如下,其中多次使用residual和layer norm:
Bert就是使用和transformer encoder相同的network architecture:
Decoder
Autoregressive(AT)
以speech recognition为例:
每一次输入都是把已有的输入一起作为新的输入:
由self-attention到Masked self-attention:
decoder只能考虑左侧已有的结果.
需要再增加一个“stop token”
Non-autoregressive(NAT)
AT对比NAT如下:
NAT的速度比AT快,一次生成整句话,建立在self-attention基础上,但NAT往往不如AT(Multi-modality)
Encoder-Decoder
cross attention具体过程如下:
Training
类似分类,计算distribution的几率和minimize cross-entropy。
使用Teacher Forcing技术来训练,即使用ground truth作为input。
Tips
Copy Mechannism
例如chat-bot聊天机器人从user的话中复制一些人名或不常见的词作为输出。
Summarization摘要时,机器也需要用到复制能力。
最早有复制能力的模型是pointer network(指针网络)
Guided Attention
例如语言合成时,在简短的句子中机器出现漏字现象。
这个时候用guided attention可以强迫机器听完输入的每一个字。
应用于speech recognition,TTS,etc.
Beam Search(寻找一条路径)
有时候找到最佳路径并非最好,decoder需要一些随机性。
Randomness is needed for decoder when generating Sequence in some tasks(e.g,sentence completion,TTS)
Scheduled Samping(偶尔给输入一些错误,机器学的更好)
Concluding Remarks:Transformer
自注意力机制类别总结
How to make self-attention efficient?
处理input是Sequence的情况,N*N的矩阵计算量过大。
Notice
在transformer中,self-attention只占一部分,当N很大时,self-attention影响也很大,通常是在image processing中。
Skip some calculations with human knowledge
Local Attention/Truncated Attention
类似CNN。
Stride Attention
Global Attention
选定一个特殊的token或直接外加token。
Different heads use different patterns
Only focus on critical parts
Clustering
step 1
step 2
Learnable Patterns
Sinkhorn Sorting Network
让机器自己决定跳过哪些grids。
有时也不需要full attention matrix,只需要选择有代表性的key。
不选query,因为会改变sequence length。
Reduce Number of Keys
用CNN扫过或做矩阵乘法:
简化矩阵乘法的运算(省略softmax):
改变乘法次序:
交换次序后所做的乘法次数是不同的:
当N>>d时,应选择先计算V*K的转置。
加上softmax后:
分母:
分子:
只有φ(q1)与b1的位置有关,其他部分计算b2时不需要重复计算。
因此,改进self-attention如下:
具体实现φ(q)函数有很多不同的方法:
Synthesizer(不通过q和k计算attention)
Summary
总结
本周学习RNN、Transformer以及一些self-attention的类别及其改进。下周将在生成模型的基础上继续学习生成对抗网络。