深度学习笔记——数值稳定性、模型初始化与激活函数

引言

本节将对数值稳定性、模型初始化以及激活函数进行系统介绍。

数值稳定性——梯度爆炸

关于梯度消失的部分,这里不再赘述。详见笔记:机器学习笔记之正则化(六)批标准化(BatchNormalization)

这里提到数值稳定性中的数值是指神经网络反向传播过程中梯度的变化量。在神经网络比较的情况下,梯度数值非常容易不稳定

考虑已知一个 d d d层的神经网络,其中第 t t t隐藏 ( t < d ) (t < d) (t<d)的输出 h t h_t ht t − 1 t-1 t1隐藏层输出 h t − 1 h_{t-1} ht1之间的关系表示为:
h t = f t ( h t − 1 ) h_t = f_t(h_{t-1}) ht=ft(ht1)
关于该神经网络的前馈计算过程表示如下:
需要注意的是,这里的 y y y并不是预测结果(因为包含损失函数 L \mathcal L L),它仅仅描述一个前馈计算过程。
y = L ∘ f d ∘ f d − 1 ∘ ⋯ ∘ f 1 ( x ) y = \mathcal L \circ f_d \circ f_{d-1} \circ \cdots\circ f_1(x) y=Lfdfd1f1(x)
基于上述运算,损失函数 L \mathcal L L关于第 t t t层神经网络 h t h_t ht权重参数 W t \mathcal W_t Wt梯度可表示为:
∂ L ∂ W t = ∂ L ∂ h d ⋅ ∂ h d ∂ h d − 1 ⋯ ∂ h t + 1 ∂ h t ⏟ d − t 次矩阵乘法 ⋅ ∂ h t ∂ W t \frac{\partial \mathcal L}{\partial \mathcal W_t} = \frac{\partial \mathcal L}{\partial h_d} \cdot \underbrace{\frac{\partial h_d}{\partial h_{d-1}} \cdots\frac{\partial h_{t+1}}{\partial h_t}}_{d - t次矩阵乘法} \cdot \frac{\partial h_t}{\partial \mathcal W_t} WtL=hdLdt次矩阵乘法 hd1hdhtht+1Wtht
假设第 t t t隐藏层函数 f t ( h t − 1 ) = σ ( W t ⋅ h t − 1 ) f_t(h_{t-1}) = \sigma(\mathcal W_t \cdot h_{t-1}) ft(ht1)=σ(Wtht1),其中 σ \sigma σ表示激活函数这里为简化起见,忽略了偏置项 b b b;那么关于 t t t隐藏层输出 h t h_t ht h t − 1 h_{t-1} ht1梯度表示为:
∂ h t ∂ h t − 1 = Diag [ σ ′ ( W t ⋅ h t − 1 ) ] ⏟ ∂ h t / ∂ Z t ⋅ W t T ⏟ ∂ Z t / ∂ h t Z t = W t ⋅ h t − 1 \frac{\partial h_t}{\partial h_{t-1}} = \underbrace{\text{Diag} \left[\sigma' \left(\mathcal W_t \cdot h_{t-1}\right)\right]}_{\partial h_t /\partial \mathcal Z_t} \cdot \underbrace{\mathcal W_t^T}_{\partial \mathcal Z_t/\partial h_t} \quad \mathcal Z_t = \mathcal W_t \cdot h_{t-1} ht1ht=ht/Zt Diag[σ(Wtht1)]Zt/ht WtTZt=Wtht1
其中 Diag \text{Diag} Diag表示对角矩阵——由于 h t h_t ht Z t \mathcal Z_t Zt是两个形状相同的向量(激活函数不影响向量形状),向量关于向量的导数是一个多维矩阵(张量 ( Tensor ) (\text{Tensor}) (Tensor))。这个对角阵的元素的每一行元素均对应向量 W t \mathcal W_t Wt内的某一具体分量
推荐一篇‘向量求导’的文章,见下方链接。侵删。

至此,关于上述 d − t d-t dt矩阵乘法可表示为如下形式:
∂ h d ∂ h t = ∏ i = t d − 1 ∂ h i + 1 ∂ h i = ∏ i = t d − 1 Diag [ σ ′ ( W i h i − 1 ) ] ⋅ W i T \begin{aligned} \frac{\partial h_d}{\partial h_t} & = \prod_{i=t}^{d-1} \frac{\partial h_{i+1}}{\partial h_i} \\ & = \prod_{i=t}^{d-1} \text{Diag}\left[\sigma'(\mathcal W_i h_{i-1})\right] \cdot \mathcal W_i^T \end{aligned} hthd=i=td1hihi+1=i=td1Diag[σ(Wihi1)]WiT

假设构建如下场景:

  • 使用 ReLU \text{ReLU} ReLU函数作为激活函数,其函数以及导数结果表示为如下形式:
    σ ( x ) = max ⁡ ( 0 , x ) σ ′ ( x ) = { 1 if  x > 0 0 Otherwise \sigma(x) = \max(0,x) \quad \sigma'(x) = \begin{cases} 1 \quad \text{if } x > 0 \\ 0 \quad \text{Otherwise} \end{cases} σ(x)=max(0,x)σ(x)={ 1if x>00Otherwise
  • 针对某任务目标(例如极大似然估计),使用梯度上升法作为参数的迭代算法:
    W ( t + 1 ) ⇐ W ( t ) + η ⋅ ∂ L ( t ) ∂ W \mathcal W^{(t+1)} \Leftarrow \mathcal W^{(t)} + \eta \cdot \frac{\partial \mathcal L^{(t)}}{\partial \mathcal W} W(t+1)W(t)+ηWL(t)
  • 由于样本特征/权重初始化的问题,导致权重结果各分量 > 1 >1 >1

至此。观察梯度 ∂ h d ∂ h t \begin{aligned} \frac{\partial h_d}{\partial h_t}\end{aligned} hthd
根据 ReLU \text{ReLU} ReLU函数的导数表达,那么矩阵 Diag [ σ ′ ( W i ⋅ h i − 1 ) ] \text{Diag} [\sigma'(\mathcal W_i \cdot h_{i-1})] Diag[σ(Wihi1)]必然是一个由 0 , 1 0,1 0,1元素构成的多维矩阵。并且这些 0 0 0分量对应的权重分量 W i ( k ) T ∈ W i T \mathcal W_{i(k)}^T \in \mathcal W_i^T Wi(k)TWiT的梯度结果为 0 0 0
∂ h d ∂ h t = ∏ i = t d − 1 Diag [ σ ′ ( W i ⋅ h i − 1 ) ] ⏟ 0 , 1 元素构成 ⋅ ∏ i = t d − 1 W i T \frac{\partial h_d}{\partial h_t} = \prod_{i=t}^{d-1} \underbrace{\text{Diag}[\sigma'(\mathcal W_i \cdot h_{i-1})]}_{0,1元素构成} \cdot \prod_{i=t}^{d-1} \mathcal W_i^T hthd=i=td10,1元素构成 Diag[σ(Wihi1)]i=td1WiT

而最终的梯度结果表示为那些没有被置 0 0 0权重分量的乘积结果。由于这些权重分量均大于 1 1 1,并且 ReLU \text{ReLU} ReLU在大于 0 0 0部分的导数为 1 1 1。也就是说,这些大于 0 0 0的权重分量在 ReLU \text{ReLU} ReLU激活函数条件下,其梯度不会造成削减。最终导致 ∏ i = t d − 1 W i T \prod_{i=t}^{d-1} \mathcal W_i^T i=td1WiT连乘过程中越来越大。

从而导致 ∂ L ∂ W \begin{aligned}\frac{\partial \mathcal L}{\partial \mathcal W}\end{aligned} WL越来越大,最终导致 W ( t + 1 ) \mathcal W^{(t+1)} W(t+1)累加过程中越来越大,导致梯度爆炸现象。

小插曲:关于对角阵 Diag [ σ ′ ( W t ⋅ h t − 1 ) ] \text{Diag}[\sigma'(\mathcal W_t \cdot h_{t-1})] Diag[σ(Wtht1)]的解释

实际上,这部分在其他描述反向传播时也都描述过,只不过这次更详细一些,针对‘向量对向量求偏导’进行具体描述。

为了书写方便,我们将第 t t t隐藏层的输入和输出由原来的 h t − 1 , h t h_{t-1},h_{t} ht1,ht改为 h ( t − 1 ) , h ( t ) h^{(t-1)},h^{(t)} h(t1),h(t)
已知某神经网络 t t t隐藏层的前馈计算图表示如下:
某层神经网络前馈计算图
由图中可知,向量 h ( t − 1 ) = ( h 1 ( t − 1 ) , h 2 ( t − 1 ) , ⋯   , h m ( t − 1 ) ) m × 1 T h^{(t-1)} = \left(h_1^{(t-1)},h_2^{(t-1)},\cdots,h_m^{(t-1)}\right)^T_{m \times 1} h(t1)=(h1(t1),h2(t1),,hm(t1))m×1T是该层的输入 Z ( t ) = ( Z 1 ( t ) , Z 2 ( t ) , ⋯   , Z n ( t ) ) n × 1 T \mathcal Z^{(t)} = \left(\mathcal Z_{1}^{(t)},\mathcal Z_2^{(t)},\cdots,\mathcal Z_{n}^{(t)}\right)_{n \times 1}^T Z(t)=(Z1(t),Z2(t),,Zn(t))n×1T表示线性计算的输出 h ( t ) = ( h 1 ( t ) , h 2 ( t ) , ⋯   , h n ( t ) ) n × 1 T h^{(t)} = \left(h_1^{(t)},h_2^{(t)},\cdots,h_n^{(t)}\right)_{n \times 1}^T h(t)=(h1(t),h2(t),,hn(t))n×1T表示该隐藏层的输出。其前馈计算过程表示如下:
h ( t ) = ReLU ( [ W ( t ) ] T h ( t − 1 ) ⏟ Z ( t ) ) h^{(t)} = \text{ReLU} \left(\underbrace{[\mathcal W^{(t)}]^T h^{(t-1)}}_{\mathcal Z^{(t)}}\right) h(t)=ReLU Z(t) [W(t)]Th(t1)
其中权重矩阵 W ( t ) \mathcal W^{(t)} W(t)是一个 m × n m \times n m×n格式的矩阵:
W ( t ) = [ W 1 ⇔ 1 ( t ) , W 1 ⇔ 2 ( t ) , ⋯   , W 1 ⇔ n ( t ) W

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

静静的喝酒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值