219. 计算 dead ReLU 单元的比例
重要性 训练仪表盘通过追踪 dead units 来发现学习率是否过高。
如果一个 ReLU 单元对 batch 中的每个输入都输出 0,它将无法获得梯度,并且可能永远无法恢复。
编写 dead_fraction(pre),其中 pre 是一个 (batch, units) 的 pre-activations 数组。返回每一行中 pre-activation <= 0 的单元比例。
def dead_fraction(pre)Why a ReLU unit dies
Any input below 0 gives output 0 and slope 0, so no gradient flows back. Move x below 0 and watch the slope readout.
- input
- 0.00
- ReLU
- 0
- slope of ReLU
- 0.5
示例 1
输入: dead_fraction([[1, -1, -2], [0.5, -3, 4]])
输出: 1/3
解析: Since only unit 1 is never positive.
示例 2
输入: pre = [[-1, 0], [-2, -3], [0, -1]]
输出: 1.0
约束条件
- Python 3 with NumPy. Code runs in your browser.
- Your code must pass all 3 tests to be accepted.
提示 1
np.all(pre <= 0, axis=0) 会为每个 unit 返回一个 boolean。
提示 2
Units 是列。
提示 3
如果一个 unit 的所有值都 <= 0,则该 unit 是 dead 的。
常见错误
- 在错误的 axis 上进行 reduction。
- 使用 < 0,这会将精确的 0 计为 alive。
理解思路
如果一个 ReLU 单元对每个样本的输入都是零或负数,那么它在 batch 上就是 dead 的。它的输出始终为 0,因此无法传回梯度,其权重也会停止学习。测量 dead units 的占比是网络健康状况的一种快速检查。
pre = [[-1, 2, 0], [-0.5, -1, 0], [-2, 3, -1]], 三行三列。Unit 0 看到的数值是 -1, -0.5, -2: 从未为正,所以是 dead。Unit 1 看到的数值是 2, -1, 3: 至少有一次为正,所以是 alive。Unit 2 看到的数值是 0, 0, -1: 从未严格大于 0,所以是 dead。Dead fraction = 2 / 3 = 约 0.667。
对于每个 unit (列),检查 pre <= 0 是否对所有行都成立 (在 batch axis, axis 0 上进行 reduction),然后返回这些 boolean 在 units 上的 mean。在错误的 axis 上进行 reduction 测量的是 dead examples 而不是 dead units。0 被视为 dead,因为 ReLU 在那里的输出为 0。用于: 调试学习率过高的训练过程,以及比较 ReLU 与 leaky 或 GELU 变体。