5.1 Exercises

d2l第5.1节练习解答

  1. Show that adding layers to a linear deep network, i.e., a network without nonlinearity can never increase the expressive power of the network. Give an example where it actively reduces it.

线性网络(无激活):矩阵乘法必然坍缩

  • 多层线性网络等价于一个线性变换:
    \(\mathbf{y} = \mathbf{W}_L \cdots \mathbf{W}_1 \mathbf{x} = \mathbf{W}_{\text{eff}} \mathbf{x}\)
  • 有效权重矩阵的秩受瓶颈限制:
    \(\text{rank}(\mathbf{W}_{\text{eff}}) \le \min\limits_i \text{rank}(\mathbf{W}_i)\)
  • 如果中间层宽度小于输入/输出维度,信息被永久压缩,无法恢复(例如无法表达满秩映射)。

非线性网络(有激活):逐元素操作破坏线性相关性

  • 线性部分的输出经过非线性函数(如 ReLU):
    \(\mathbf{h}_{i+1} = \text{ReLU}(\mathbf{W}_{i+1} \mathbf{h}_i)\)
  • ReLU 会将所有负值归零,这一“剪切”操作破坏了矩阵行/列之间的线性相关性
  • 原本线性相关的行(成比例)经过不同的截断后,变得不再成比例,因此矩阵的秩在非线性层之后显著增加,逼近该层的维度上限。

本质对比

网络类型数学本质对秩的影响
线性深度网络矩阵乘积秩单调不增,受瓶颈限制
非线性深度网络复合非线性函数每层非线性可恢复/提升秩,打破瓶颈

  1. Compute the derivative of the pReLU activation function.
\[ \frac{d}{dx} \text{PReLU}(x) = \begin{cases} 1 & \text{if } x > 0 \\ \alpha & \text{if } x < 0 \\ 未定义(实践中取\alpha或1) & \text{if } x = 0 \end{cases} \]
  1. Compute the derivative of the Swish activation function \[ x\ sigmoid(\beta x) \].
\[ \frac{d}{dx} \text{Swish}(x) = \sigma(\beta x) + \beta x \cdot \sigma(\beta x) \cdot (1 - \sigma(\beta x)) \]
  1. Show that an MLP using only ReLU (or pReLU) constructs a continuous piecewise linear function.

设 \( f \) 是连续分段线性函数,其线性区域集合为 \(\{\mathcal{R}_i\}_{i=1}^m\),每个区域为凸多面体。

在每个区域 \(\mathcal{R}_i\) 上,\( f \) 可写成仿射形式:

\[ f(\mathbf{x}) = \mathbf{w}_i^\top \mathbf{x} + b_i \]

取任意一个区域 \(\mathcal{R}_i\),其上:

\[ f(\mathbf{x}) = \mathbf{w}_i^\top \mathbf{x} + b_i \]

定义超平面 \( H_i = \{\mathbf{x} \mid \mathbf{w}_i^\top \mathbf{x} + b_i = 0\} \),它将 \(\mathcal{R}_i\) 分割为两个子区域:

\[ \mathcal{R}_i^+ = \mathcal{R}_i \cap \{\mathbf{x} \mid \mathbf{w}_i^\top \mathbf{x} + b_i \ge 0\} \]

\[ \mathcal{R}_i^- = \mathcal{R}_i \cap \{\mathbf{x} \mid \mathbf{w}_i^\top \mathbf{x} + b_i \le 0\} \]

在这两个子区域上,\( g \) 的表达式分别为:

\[ g(\mathbf{x}) = \begin{cases} \mathbf{w}_i^\top \mathbf{x} + b_i & \mathbf{x} \in \mathcal{R}_i^+ \\[4pt] 0 & \mathbf{x} \in \mathcal{R}_i^- \end{cases} \]

两者都是仿射函数。

在边界 \( H_i \cap \mathcal{R}_i \) 上,\( f(\mathbf{x}) = 0 \),因此:

\[ \text{左侧取值} = 0, \quad \text{右侧取值} = 0 \]

两侧取值相等,故 \( g \) 在该边界处连续。



  1. Assume that we have a nonlinearity that applies to one minibatch at a time, such as the batch normalization (Ioffe and Szegedy, 2015). What kinds of problems do you expect this to cause?

破坏样本独立性(训练时的“信息泄露”):在训练时,Batch Normalization让一个样本的归一化结果依赖于同Batch中其他样本的均值和方差。这相当于在样本之间建立了一种非预期的依赖关系,从严格的理论角度看,这使得每个样本的梯度不再是独立的,可能给理论分析带来麻烦。


  1. Provide an example where the gradients vanish for the sigmoid activation function.

背景:Sigmoid 函数定义为 \(\sigma(x) = \frac{1}{1 + e^{-x}}\),其导数为 \(\sigma'(x) = \sigma(x) \cdot (1 - \sigma(x))\)。\(\sigma'(x)\) 的最大值为 \(0.25\)(在 \(x = 0\) 处取得),这意味着 Sigmoid 的梯度永远小于 1,在反向传播时容易造成梯度指数级衰减。

只要发生以下两种情况,梯度就会迅速消失:

  • 权重初始化过大

  • 网络层数加深

假设我们有一个3层的全连接网络,每一层后接Sigmoid。设输入为 $x=1$,所有权重初始化为 $w=4$,偏置 $b=0$。

  • 第一层输入:\(z_1 = w \cdot x = 4 \times 1 = 4\)
    • 激活输出:\(h_1 = \sigma(z_1) = \sigma(4) \approx 0.9820\)
    • 该层梯度(局部):\(\sigma'(z_1) = \sigma(4) \cdot (1 - \sigma(4)) \approx 0.9820 \times 0.0180 \approx 0.0177\)
  • 第二层输入:\(z_2 = w \cdot h_1 = 4 \times 0.9820 \approx 3.928\)
    • 激活输出:\(h_2 = \sigma(z_2) \approx 0.9806\)
    • 该层梯度:\(\sigma'(z_2) \approx 0.9806 \times 0.0194 \approx 0.0190\)
  • 第三层输入:\(z_3 = w \cdot h_2 = 4 \times 0.9806 \approx 3.922\)
    • 激活输出:\(z_2 = w \cdot h_1 = 4 \times 0.9820 \approx 3.928\)
    • 该层梯度:\(\sigma'(z_3) \approx 0.9804 \times 0.0196 \approx 0.0192\)

反向传播的梯度

根据链式法则,损失对第一层权重的更新梯度,等于沿途所有局部梯度的乘积:

[ \text{总梯度}

\sigma’(z_3) \cdot w_3 \cdot \sigma’(z_2) \cdot w_2 \cdot \sigma’(z_1) \cdot w_1 \cdot \text{上游梯度} ]

代入数值:

\[ \text{总梯度} \approx 0.0192 \times 4 \times 0.0190 \times 4 \times 0.0177 \times 4 \approx 0.00035 \]

这个结果,比第一层本身的局部梯度(0.0177)又缩小了将近 100 倍。当网络有 10 层、20 层时,这个梯度会指数级衰减,无限接近于 0,导致靠近输入层的权重几乎无法得到有效更新,网络停止学习。