Self-Attention详解

11. Self-Attention

11.1 Introduction

Seq2Seq 模型有两个RNN网络,一个 Encoder,一个 Decoder。

Self-Attention 将 Attention 用在一个 RNN 网络上。

Self-Attention详解

11.2 Training

11.2.1 初始化

c 0 c_0 c0 = 0, h 0 h_0 h0 = 0

Self-Attention详解

11.2.2 计算 h 1 h_1 h1

首先,我们回忆一下 SimpleRNN 中的计算过程,这里没有 c 0 c_0 c0

Self-Attention详解

在 SimpleRNN + Attention 中,计算过程如下。使用的是 c i c_i ci 而不是 h i h_i hi。计算得到 h 1 h_1 h1

Self-Attention详解

11.2.3 计算 c 1 c_1 c1

此时计算 c 1 c_1 c1。记住,c 是根据所有 h 计算得到的。关键词:所有h。此时, h 0 h_0 h0 被初始化为 0。c 是根据所有 h 计算得到的,所以 c 1 = h 1 c_1 = h_1 c1=h1

Self-Attention详解

11.2.4 计算 h 2 h_2 h2

Self-Attention详解

11.2.5 计算 c 2 c_2 c2

首先,根据当前得到的 h i h_i hi 计算她对所有 h 的权重 α i \alpha_i αi。注意:这里的所有 h 也包括当前的 h i h_i hi。现在是计算 c 2 c_2 c2,所以,这里是用 h 2 h_2 h2 与当前的所有 h 计算,也就是与 h 1 , h 2 h_1, h_2 h1,h2 计算。

Self-Attention详解

最后得到 c 2 c_2 c2

Self-Attention详解

11.2.6 计算 h 3 h_3 h3

Self-Attention详解

11.2.7 计算 c 3 c_3 c3

再强调一遍,每次计算 α i \alpha_i αi 是对所有 h。这里就是 3 个h。

Self-Attention详解

Self-Attention详解

11.3 Summary

Self-Attention 将 Attention 用在一个 RNN 网络上。不再是 Seq2Seq 的 Encoder 和 Decoder 架构。

Self-Attention详解