算法强化 —— GRU和LSTM

GRU(Gated Recurrent Unit)

一个带门的RNN
一般RNN模型中,每个元素受其周围附近的影响较大,难以建立跨度较大的依赖性。由于跨度较大,普通的RNN模型就容易出现梯度消失,捕捉不到他们之间的依赖,造成语法错误。
在这里插入图片描述
其中, a < t > a^{<t>} 的表达式为
a < t > = t a n h ( W a [ a < t 1 > , x < t > ] + b a ) a^{<t>} = tanh(W_a[a^{<t-1>},x^{<t>}]+b_a)
这个结构的缺点是阻断了 a < t > a^{<t>} a < t 1 > a^{<t-1>} 的直接性联系,相当于少了一个记忆单元,记住 a < t 1 > a^{<t-1>} ,这是造成梯度消失的主要原因。
因此,为了解决梯度消失问题,对上述单元进行修改,添加了记忆单元,这种结构称之为Gated Recurrent Unit(GRU)
在这里插入图片描述
c ~ < t > = tanh ( W c [ c < t 1 > , x < t > ] + b c ) Γ u = σ ( W u [ c < t 1 > , x < t > ] + b u ) c < t > = Γ u c ~ < t > + ( 1 Γ u ) c < t 1 > \begin{array}{c} \tilde{c}^{<t>}=\tanh \left(W_{c}\left[c^{<t-1>}, x^{<t>}\right]+b_{c}\right) \\ \Gamma_{u}=\sigma\left(W_{u}\left[c^{<t-1>}, x^{<t>}\right]+b_{u}\right) \\ c^{<t>}=\Gamma_{u} * \tilde{c}^{<t>}+\left(1-\Gamma_{u}\right) * c^{<t-1>} \end{array}
其中 Γ u \Gamma_u 是一种Gate,记忆单元。 Γ u \Gamma_u 的取值范围在[0,1]。
Γ u = 1 \Gamma_u = 1 时,代表更新, c < t > = c ~ < t > c^{<t>} = \tilde{c}^{<t>} ;
Γ u = 0 \Gamma_u = 0 时,代表记忆, c < t > = c < t 1 > c^{<t>} = c^{<t-1>} ,保留之前的模块输出。
所以,根据$\Gamma_u 的值,能够让模型自己决定是选择更新还是记忆。 \Gamma_u $能够保证RNN模型中跨度很大的依赖关系不受影响,消除梯度小时问题。

Long Short Term Memory(LSTM)

LSTM称之为长短期记忆模型,能够有效处理RNN模型中“长期依赖”的问题。
在这里插入图片描述
LSTM有3个Gate,分别是Forget Gate(遗忘门),Update Gate(输入门),Output Gate(输出门)。 x < t > x^{<t>} 是输入, c < t 1 > c^{<t-1>} a < t 1 > a^{<t-1>} 来自上一模块的输出。
c ~ < t > = tanh ( W c [ a < t 1 > , x < t > ] + b c ) Γ u = σ ( W u [ a < t 1 > , x < t > ] + b u ) Γ f = σ ( W f [ a < t 1 > , x < t > ] + b f ) Γ o = σ ( W o [ a < t 1 > , x < t > ] + b o ) c < t > = Γ u c ~ < t > + Γ f c < t 1 > a < t > = Γ o c < t > \begin{array}{c} \tilde{c}^{<t>}=\tanh \left(W_{c}\left[a^{<t-1>}, x^{<t>}\right]+b_{c}\right) \\ \Gamma_{u}=\sigma\left(W_{u}\left[a^{<t-1>}, x^{<t>}\right]+b_{u}\right) \\ \Gamma_{f}=\sigma\left(W_{f}\left[a^{<t-1>}, x^{<t>}\right]+b_{f}\right) \\ \Gamma_{o}=\sigma\left(W_{o}\left[a^{<t-1>}, x^{<t>}\right]+b_{o}\right) \\ c^{<t>}=\Gamma_{u} * \tilde{c}^{<t>}+\Gamma_{f} * c^{<t-1>} \\ a^{<t>}=\Gamma_{o} * c^{<t>} \end{array}
其中, Γ u , Γ f , Γ o \Gamma_{u},\Gamma_{f},\Gamma_{o} 分别表示Forget Gate、Update Gate、Output Gate因子,取值范围均在[0,1]之间。这几个范数会在RNN模型训练过程中自适应调整记忆与更新的权重因子,已取得最佳模型训练效果。

如果考虑 c < t 1 > c^{<t-1>} ,对 Γ u , Γ f , Γ o \Gamma_{u},\Gamma_{f},\Gamma_{o} 的影响,可对LSTM表达式进行修改
c ~ < t > = tanh ( W c [ a < t 1 > , x < t > ] + b c ) Γ u = σ ( W u [ a < t 1 > , x < t > , c < t 1 > ] + b u ) Γ f = σ ( W f [ a < t 1 > , x < t > , c < t 1 > ] + b f ) Γ o = σ ( W o [ a < t 1 > , x < t > , c < t 1 > ] + b o ) c < t > = Γ u c ~ < t > + Γ f c < t 1 > a < t > = Γ o c < t > \begin{array}{c} \tilde{c}^{<t>}=\tanh \left(W_{c}\left[a^{<t-1>}, x^{<t>}\right]+b_{c}\right) \\ \Gamma_{u}=\sigma\left(W_{u}\left[a^{<t-1>}, x^{<t>},c^{<t-1>}\right]+b_{u}\right) \\ \Gamma_{f}=\sigma\left(W_{f}\left[a^{<t-1>}, x^{<t>},c^{<t-1>}\right]+b_{f}\right) \\ \Gamma_{o}=\sigma\left(W_{o}\left[a^{<t-1>}, x^{<t>},c^{<t-1>}\right]+b_{o}\right) \\ c^{<t>}=\Gamma_{u} * \tilde{c}^{<t>}+\Gamma_{f} * c^{<t-1>} \\ a^{<t>}=\Gamma_{o} * c^{<t>} \end{array}

LSTM是RNN的一个优秀的变种模型,继承了大部分RNN模型的特征,同时解决了神经网络反向传播过程中由于逐步缩减而产生的梯度消失问题。
LSTM非常适合用于处理与时间序列高度相关的问题,例如机器翻译、对话生成、编解码等。

发布了110 篇原创文章 · 获赞 3 · 访问量 4088

猜你喜欢

转载自blog.csdn.net/qq_33357094/article/details/105038939