RNN与LSTM系列(一)——LSTM反向传播公式推导

转载自https://blog.csdn.net/wjc1182511338/article/details/79285503

0 LSTM相对于rnn的优势

The Problem of Long-Term Dependencies

One of the appeals of RNNs is the idea that they might be able to connect previous information to the present task, such as using previous video frames might inform the understanding of the present frame. If RNNs could do this, they’d be extremely useful. But can they? It depends.

Sometimes, we only need to look at recent information to perform the present task. For example, consider a language model trying to predict the next word based on the previous ones. If we are trying to predict the last word in “the clouds are in the sky,” we don’t need any further context – it’s pretty obvious the next word is going to be sky. In such cases, where the gap between the relevant information and the place that it’s needed is small, RNNs can learn to use the past information.

RNN与LSTM系列(一)——LSTM反向传播公式推导

But there are also cases where we need more context. Consider trying to predict the last word in the text “I grew up in France… I speak fluent French.” Recent information suggests that the next word is probably the name of a language, but if we want to narrow down which language, we need the context of France, from further back. It’s entirely possible for the gap between the relevant information and the point where it is needed to become very large.

Unfortunately, as that gap grows, RNNs become unable to learn to connect the information.

RNN与LSTM系列(一)——LSTM反向传播公式推导

In theory, RNNs are absolutely capable of handling such “long-term dependencies.” A human could carefully pick parameters for them to solve toy problems of this form. Sadly, in practice, RNNs don’t seem to be able to learn them. The problem was explored in depth by Hochreiter (1991) [German] and Bengio, et al. (1994), who found some pretty fundamental reasons why it might be difficult.

Thankfully, LSTMs don’t have this problem!

1. 结构

1.1 比较

RNN:

RNN与LSTM系列(一)——LSTM反向传播公式推导

LSTM:

RNN与LSTM系列(一)——LSTM反向传播公式推导

其中的notation:

RNN与LSTM系列(一)——LSTM反向传播公式推导

1.2 核心思想:

LSTM在原来的隐藏层上增加了一个

The key to LSTMs is the cell state, the horizontal line running through the top of the diagram.

RNN与LSTM系列(一)——LSTM反向传播公式推导

RNN与LSTM系列(一)——LSTM反向传播公式推导

  1. LSTM比RNN多了一个细胞状态,就是最上面一条线,像一个传送带,它让信息在这条线上传播而不改变信息。

    The cell state is kind of like a conveyor belt. It runs straight down the entire chain, with only some minor linear interactions. It’s very easy for information to just flow along it unchanged.

    RNN与LSTM系列(一)——LSTM反向传播公式推导

  2. LSTM可以自己增加或移除信息,通过“门”的结构控制。

    “门”选择性地让信息是否通过,“门”包括一个sigmoid层和按元素乘。如下图:

    RNN与LSTM系列(一)——LSTM反向传播公式推导

    sigmoid层输出0-1的值,表示让多少信息通过,1表示让所有的信息都通过。

    一个LSTM单元有3个门。

2. 流程

上面一条线CtCt是细胞状态,下面的htht是隐藏状态 。

其实看多少个圆圈、黄框就知道有哪些操作了。

三个sigmoid层是三个门:忘记门、输入门、输出门。

ftitCt~Ctotht=σ(Wf⋅[ht−1,xt]+bf)=σ(Wi⋅[ht−1,xt]+bi)=tanh(WC⋅[ht−1,xt]+bC)=ft∗Ct−1+it∗Ct~=σ(Wo⋅[ht−1,xt]+bo)=ot∗tanh(Ct)ft=σ(Wf⋅[ht−1,xt]+bf)it=σ(Wi⋅[ht−1,xt]+bi)Ct~=tanh(WC⋅[ht−1,xt]+bC)Ct=ft∗Ct−1+it∗Ct~ot=σ(Wo⋅[ht−1,xt]+bo)ht=ot∗tanh(Ct)


RNN与LSTM系列(一)——LSTM反向传播公式推导

 

2.1 忘记门:扔掉信息(细胞状态)

第一步是决定从细胞状态里扔掉什么信息(也就是保留多少信息)。将上一步细胞状态中的信息选择性的遗忘 。

实现方式:通过sigmoid层实现的“忘记门”。以上一步的ht−1ht−1和这一步的xtxt作为输入,然后为Ct−1Ct−1里的每个数字输出一个0-1间的值,表示保留多少信息(1代表完全保留,0表示完全舍弃),然后与Ct−1Ct−1乘。

例子:让我们回到语言模型的例子中来基于已经看到的预测下一个词。在这个问题中,细胞状态可能包含当前主语的类别,因此正确的代词可以被选择出来。当我们看到新的主语,我们希望忘记旧的主语。
例如,他今天有事,所以我…… 当处理到‘’我‘’的时候选择性的忘记前面的’他’,或者说减小这个词对后面词的作用。

RNN与LSTM系列(一)——LSTM反向传播公式推导

2.2 输入层门:存储信息(细胞状态)

第二步是决定在细胞状态里存什么。将新的信息选择性的记录到细胞状态中。

实现方式:包含两部分,1. sigmoid层(输入门层)决定我们要更新什么值;2. tanh层创建一个候选值向量Ct~Ct~,将会被增加到细胞状态中。 我们将会在下一步把这两个结合起来更新细胞状态。

例子:在我们语言模型的例子中,我们希望增加新的主语的类别到细胞状态中,来替代旧的需要忘记的主语。 例如:他今天有事,所以我…… 当处理到‘’我‘’这个词的时候,就会把主语我更新到细胞中去。

RNN与LSTM系列(一)——LSTM反向传播公式推导

2.3 更新细胞状态(细胞状态)

更新旧的细胞状态

实现方式:Ct=ft∗Ct−1+it∗Ct~Ct=ft∗Ct−1+it∗Ct~,ftft表示保留上一次的多少信息,itit表示更新哪些值,Ct~Ct~表示新的候选值。候选值被要更新多少(即itit)放缩。

这一步我们真正实现了移除哪些旧的信息(比如一句话中上一句的主语),增加哪些新信息。

RNN与LSTM系列(一)——LSTM反向传播公式推导

2.4 输出层门:输出(隐藏状态)

最后,我们要决定作出什么样的预测。

实现方式:1. 我们通过sigmoid层(输出层门)来决定输出新的细胞状态的哪些部分;2. 然后我们将细胞状态通过tanh层(使值在-1~1之间),然后与sigmoid层的输出相乘。

所以我们只输出我们想输出的部分。

例子:在语言模型的例子中,因为它就看到了一个 代词,可能需要输出与一个 动词 相关的信息。例如,可能输出是否代词是单数还是复数,这样如果是动词的话,我们也知道动词需要进行的词形变化。
例如:上面的例子,当处理到‘’我‘’这个词的时候,可以预测下一个词,是动词的可能性较大,而且是第一人称。 会把前面的信息保存到隐层中去。

RNN与LSTM系列(一)——LSTM反向传播公式推导

3. 反向传播

RNN与LSTM系列(一)——LSTM反向传播公式推导

如图上的5个不同的阶段反向传播误差。

1. 维度

先介绍下各个变量的维度,htht 的维度是黄框里隐藏层神经元的个数,记为d,即矩阵W∗W∗ 的行数(因为WjiWji是输入层的第ii个神经元指向隐藏层第jj个神经元的参数),xtxt的维度记为n,则[ht−1xt][ht−1xt]的维度是d+nd+n,矩阵的维度都是d∗(d+n)d∗(d+n),其他的向量维度都是d∗1d∗1。(为了表示、更新方便,我们将bias放到矩阵里)

以WfWf举例:
RNN与LSTM系列(一)——LSTM反向传播公式推导

同理:
RNN与LSTM系列(一)——LSTM反向传播公式推导
合并为一个矩阵就是:
RNN与LSTM系列(一)——LSTM反向传播公式推导

2.一些符号

⊙⊙ 是element-wise乘,即按元素乘。其他的为正常的矩阵乘。

为了表示向量和矩阵的元素,我们把时间写在上标。

用δztδzt表示EtEt对ztzt的偏导。

⊗⊗​ 表示外积,即列向量乘以行向量

3.两点疑惑

1. 3.2中 δCt+=δht⊙ot⊙[1−tanh2(Ct)]δCt+=δht⊙ot⊙[1−tanh2(Ct)] 我还没想明白
2. 3.5中下划线的是ht−1ht−1的函数 ,但Ct−1iCit−1 也是ht−1ht−1的函数,不知道为什么不算 。

3.1 δhtδht

我们首先假设∂Et∂ht=δht∂Et∂ht=δht ,这里的EtEt指的是t时刻的误差,对每个时刻的误差都要计算一次。

RNN与LSTM系列(一)——LSTM反向传播公式推导

3.2 δot、δCtδot、δCt

Forward: ht=ot⊙tanh(Ct)ht=ot⊙tanh(Ct)

已知:δhtδht

求:δot、δCtδot、δCt

解:由于
RNN与LSTM系列(一)——LSTM反向传播公式推导
所以
RNN与LSTM系列(一)——LSTM反向传播公式推导

但是 这里提到 δCt+=δht⊙ot⊙[1−tanh2(Ct)]δCt+=δht⊙ot⊙[1−tanh2(Ct)] 我还没想明白

3.3 δft、δit、δCt~、δCt−1δft、δit、δCt~、δCt−1

Forward:Ct=ft⊙Ct−1+it⊙Ct~Ct=ft⊙Ct−1+it⊙Ct~

已知:δot、δCtδot、δCt

求:δft、δit、δCt~、δCt−1δft、δit、δCt~、δCt−1

解:因为
RNN与LSTM系列(一)——LSTM反向传播公式推导
所以:
RNN与LSTM系列(一)——LSTM反向传播公式推导

3.4 δWf、δWi、δWc、δWoδWf、δWi、δWc、δWo

Forward:ft=Wf⋅st,it=Wi⋅st,ot=Wo⋅st,Ct~=WC⋅stft=Wf⋅st,it=Wi⋅st,ot=Wo⋅st,Ct~=WC⋅st

已知:δft、δit、δCt~、δCt−1δft、δit、δCt~、δCt−1

求:δWf、δWi、δWc、δWoδWf、δWi、δWc、δWo

解:由于
RNN与LSTM系列(一)——LSTM反向传播公式推导

所以:
RNN与LSTM系列(一)——LSTM反向传播公式推导

合并在一起就是:
RNN与LSTM系列(一)——LSTM反向传播公式推导

3.5 δht−1δht−1

Forward:
RNN与LSTM系列(一)——LSTM反向传播公式推导
下划线的是ht−1ht−1​的函数 ,但Ct−1iCit−1​ 也是ht−1ht−1​的函数,不知道为什么不算 。

已知:δft、δit、δCt~、δCt−1δft、δit、δCt~、δCt−1

求:δht−1δht−1

解:
RNN与LSTM系列(一)——LSTM反向传播公式推导
式1是Forward里第一个式子对oti和ctioit和cit求导;式2是Forward里第二个式子对fti,iti,Ct~fit,iit,Ct~求导。

然后看Forward里的后几个式子,对ht−1ht−1求导:
RNN与LSTM系列(一)——LSTM反向传播公式推导
所以(*)式用矩阵计算则为:
RNN与LSTM系列(一)——LSTM反向传播公式推导

3.6 总梯度

 

∂E∂W=∑t=0T∂Et∂W∂E∂W=∑t=0T∂Et∂W

 

4.变种

GRU

It combines the forget and input gates into a single “update gate.” It also merges the cell state and hidden state, and makes some other changes. The resulting model is simpler than standard LSTM models, and has been growing increasingly popular.

RNN与LSTM系列(一)——LSTM反向传播公式推导

add peephole

RNN与LSTM系列(一)——LSTM反向传播公式推导

use coupled forget

Another variation is to use coupled forget and input gates. Instead of separately deciding what to forget and what we should add new information to, we make those decisions together. We only forget when we’re going to input something in its place. We only input new values to the state when we forget something older.

RNN与LSTM系列(一)——LSTM反向传播公式推导

其他

These are only a few of the most notable LSTM variants. There are lots of others, like Depth Gated RNNs by Yao, et al. (2015). There’s also some completely different approach to tackling long-term dependencies, like Clockwork RNNs by Koutnik, et al. (2014).

Which of these variants is best? Do the differences matter? Greff, et al. (2015) do a nice comparison of popular variants, finding that they’re all about the same. Jozefowicz, et al. (2015)tested more than ten thousand RNN architectures, finding some that worked better than LSTMs on certain tasks.

参考:http://colah.github.io/posts/2015-08-Understanding-LSTMs/