RNN会有梯度消失这样的问题,对于远距离无法进行梯度更新,因此更新主要集中在临近区域。
GRU和LSTM都是为了处理RNN梯度消失问题而设计的,可以学习到长距离依赖。
LSTM
LSTM比GRU更加复杂一些,有遗忘门,输入门,输出门。LSTM的关键就是细胞状态,如下图所示。
- forget gate: f t = s i g m o i d ( W f x t + U f h t − 1 + b f ) f_t = sigmoid(W_fx_t + U_fh_{t-1} +b_f) ft=sigmoid(Wfxt+Ufht−1+bf)
- input gate: i t = s i g m o i d ( W i x t + U i h t − 1 + b i ) i_t = sigmoid(W_ix_t + U_ih_{t-1}+b_i) it=sigmoid(Wixt+Uiht−1+bi)
- output gate: o t = s i g m o i d ( W o x t + U o h t − 1 + b o ) o_t = sigmoid(W_ox_t + U_oh_{t-1}+b_o) ot=sigmoid(Woxt+Uoht−1+bo)
- c ^ t = t a n h ( W c x t + U c h t − 1 + b c ) \hat c_t = tanh(W_cx_t + U_ch_{t-1}+b_c) c^t=tanh(Wcxt+Ucht−1+bc)
- c t = f t ⋅ c t − 1 + i t ⋅ c ^ t c_t = f_t \cdot c_{t-1} + i_t \cdot \hat c_t ct=ft⋅ct−1+it⋅c^t 此处的短路连接可以缓解梯度消失
- h t = o t ⋅ t a n h ( c t ) h_t = o_t \cdot tanh(c_t) ht=ot⋅tanh(ct)
GRU
GRU将忘记门和输入门合成了一个单一的更新门。
GRU引入了两个门,分别是update gate和reset gate:
- update gate: z t = s i g m o i d ( W z x t + U z h t − 1 + b z ) z_t = sigmoid(W_zx_t + U_zh_{t-1}+b_z) zt=sigmoid(Wzxt+Uzht−1+bz)
- reset gate: r t = s i g m o i d ( W r x t + U r h t − 1 + b r ) r_t = sigmoid(W_rx_t + U_rh_{t-1}+b_r) rt=sigmoid(Wrxt+Urht−1+br)
s
t
=
t
a
n
h
(
W
x
t
+
r
t
⋅
U
h
t
−
1
+
b
)
s_t= tanh(Wx_t + r_t \cdot Uh_{t-1}+b)
st=tanh(Wxt+rt⋅Uht−1+b), 候选值。
h
t
=
(
1
−
z
)
⋅
s
t
+
z
⋅
h
t
−
1
h_t = (1-z) \cdot s_t + z \cdot h_{t-1}
ht=(1−z)⋅st+z⋅ht−1,此处的短路连接可以缓解梯度消失,当z=1时,
h
t
=
h
t
−
1
h_t = h_{t-1}
ht=ht−1,信息得以保持。
重置门决定了如何将新的输入信息与前面的记忆相结合,更新门定义了前面的记忆保存到当前时间步的量。当重置门为1, 更新门为0的时候,即可获得RNN模型。
[1] https://www.jianshu.com/p/9dc9f41f0b29
[2] https://zhuanlan.zhihu.com/p/28297161
[3] https://blog.youkuaiyun.com/zhangxb35/article/details/70060295
[4] https://zhuanlan.zhihu.com/p/25518711