CS231n Homework 2
CS231n Homework 2
Batch Normalization
Inline Question 1:
Describe the results of this experiment. How does the weight initialization scale affect models with/without batch normalization differently, and why?
Answer:
实验结果表明:weight initialization scale 对没有 Batch Normalization(BN)的模型训练影响很大,而对带 BN的模型影响明显更小、训练更稳定。
对无 BN模型来说,初始化尺度会直接决定各层 activation 的幅度,从而影响 gradient 的大小与传播。如果 scale 太小,信号在层间逐渐变弱,出现 vanishing activation / vanishing gradient,训练变慢甚至学不动;如果 scale 太大,信号与梯度会被层层放大,导致 exploding activation / exploding gradient,训练不稳定、loss 波动甚至发散,因此不同尺度下性能差异明显。
对有 BN模型来说,BN 会在每个 mini-batch 内对中间层 activation 做标准化(控制 mean 和 variance),再配合可学习的 gamma / beta 进行重缩放。这样一来,即使初始权重尺度不同,进入后续层的 activation 分布也会被拉回到稳定范围,梯度更平滑,优化更容易收敛,因此模型对 initialization scale 不那么敏感。
Inline Question 2:
Describe the results of this experiment. What does this imply about the relationship between batch normalization and batch size? Why is this relationship observed?
Answer:
从图里可以看到,在使用 Batch Normalization (BN) 的情况下,不同 batch size(标成 with_norm5/10/50)对最终 validation accuracy 的影响并不大:各条曲线在前几轮快速上升后,基本都收敛在 0.31–0.35 左右,差距很小。相比之下,training accuracy 的差异更明显:较大的 batch(如 with_norm50)训练集上升更快、更高,但验证集提升有限,说明更大的 batch 更容易把训练集拟合得更好,却不一定带来更强的泛化。
这意味着 BN 和 batch size 存在耦合关系:BN 依赖 mini-batch 的均值/方差来做标准化,batch 越大,这些统计量越稳定、噪声越小,训练更平滑;batch 太小则统计量噪声更大,带来更强的随机扰动,可能让训练不稳定,但也可能起到一定 regularization 效果,所以验证集表现未必更差。之所以会出现这种关系,是因为 BN 在训练时用的是 batch 内统计量(而不是全局真实分布),本质上引入了与 batch size 相关的“统计噪声”,从而同时影响优化稳定性与泛化表现。
Inline Question 3:
Which of these data preprocessing steps is analogous to batch normalization, and which is analogous to layer normalization?
- Scaling each image in the dataset, so that the RGB channels for each row of pixels within an image sums up to 1.
- Scaling each image in the dataset, so that the RGB channels for all pixels within an image sums up to 1.
- Subtracting the mean image of the dataset from each image in the dataset.
- Setting all RGB values to either 0 or 1 depending on a given threshold.
Answer: 2对应layernorm;3对应BatchNorm
Inline Question 4:
When is layer normalization likely to not work well, and why?
- Using it in a very deep network
- Having a very small dimension of features
- Having a high regularization term
Answer:2. Having a very small dimension of features。
原因:Layer Normalization (LN) 是在单个样本内部对 feature 维度计算 mean/variance 并做归一化。如果 feature dimension 很小,用来估计 mean/variance 的样本数不足,统计量会很不稳定(噪声大),归一化结果容易失真,导致训练不稳定或效果变差。相反,网络很深(1)并不是 LN 特别“天然不适用”的条件;而 regularization term 很高(3)主要影响的是过强约束导致欠拟合,不是 LN 本身的失效原因。
Dropout
Inline Question 1:
What happens if we do not divide the values being passed through inverse dropout by p in the dropout layer? Why does that happen?
Answer:
如果在 inverse dropout 里不把保留下来的 activation 除以 p,那么训练时经过 dropout 后的输出期望值会变小,导致网络在训练阶段看到的信号整体被“压低”。
原因是:dropout 会以概率 p 保留单元(以 1-p 置零)。如果不做 /p 的缩放,则输出的期望为 \(\mathbb{E}[y] = p \cdot x\)
也就是比原来的 x 小了 p 倍。这样一来,后续层的 activation 和 gradient 的尺度都会系统性变小,训练会更慢,甚至出现类似 vanishing gradient 的问题。更关键的是,测试时通常会关闭 dropout(全部单元都保留),此时输出变回接近 x,会比训练时大一截,导致 train/test mismatch,模型在推理时的分布发生偏移,性能变差。
而 inverse dropout 通过对保留下来的值做 x/p,使得 \(\mathbb{E}[y] = p \cdot (x/p)=x\) 从而保证训练和测试阶段的输出尺度一致。
Inline Question 2:
Compare the validation and training accuracies with and without dropout – what do your results suggest about dropout as a regularizer?
Answer:
从图里可以看到 without dropout(keep_ratio=1.00) 的 training accuracy 很快接近 1.0,但 validation accuracy 在中后期反而逐渐下降到更低水平,说明模型在训练集上学得“太好”,出现明显 overfitting。也就是训练集表现不断变强,但泛化并没有同步提升,甚至变差。
加入 dropout(keep_ratio=0.25) 后,训练变得更难,training accuracy 上升更慢、最终也更低(大约到 0.9 左右),但 validation accuracy 整体更高、更稳定(峰值更高且后期不容易下滑)。这说明 dropout 作为 regularizer 能通过随机丢弃部分 neurons/activations 来减少对特定特征的依赖,降低 co-adaptation,相当于在训练时引入噪声与“隐式 ensemble”效果,从而提升模型的泛化能力。