Skip to content
BIASED
Back to topic

Optional

Residual vs plain: depth degradation

NumPy sin-x regression: deep plain ReLU vs residual. Self-check in the notebook.

Version
v1
Minutes
~25
Evidence
Self-check in the notebook

Until platform auto-check exists, compare cell output with the expected numbers in the notebook. This is not a debt and does not affect mastery.

Download .ipynb

Download the notebook and run it locally. Its environment requirements are listed inside.

Notebook contents

Readable on your phone. The code below has not been executed in the browser; download the notebook to run it.

DL · Residual vs plain: схлопывание представлений

Activity version: 1 · Seed: 1 · Данные: N=128N{=}128 почти ортогональных входов, ширина 6464.

Интуиция из ResNet / init: deep plain ReLU (He) гонит примеры к одному направлению (рост средней |корреляции|), а residual h↦h+0.1 F(h)h\mapsto h+0.1\,F(h) сохраняет различие. Только forward.

Ожидаемые числа (depth 40, seed 1)
Model mean off-diag ∥corr∥\|corr\| at layer 40 Tolerance
plain >0.7> 0.7
residual <0.35< 0.35
plain − residual >0.4> 0.4
Самопроверка в ноутбуке

Не ImageNet. Не влияет на сертификат.

import numpy as np
 
rng = np.random.default_rng(0)
N, H, depth = 128, 64, 40
X, _ = np.linalg.qr(rng.normal(size=(N, H)))
 
def mean_abs_offdiag_corr(h):
    h = h - h.mean(axis=0, keepdims=True)
    h = h / (np.linalg.norm(h, axis=1, keepdims=True) + 1e-12)
    C = h @ h.T
    mask = ~np.eye(N, dtype=bool)
    return float(np.mean(np.abs(C[mask])))
 
def run(residual, seed=1, gain=2.0, res_scale=0.1):
    rng = np.random.default_rng(seed)
    h = X.copy()
    last = None
    for _ in range(depth):
        W = rng.normal(0, np.sqrt(gain / H), size=(H, H))
        a = np.maximum(0.0, h @ W)
        h = h + res_scale * a if residual else a
        last = mean_abs_offdiag_corr(h)
    return last
 
c_plain = run(False)
c_res = run(True)
print("plain corr", c_plain, "residual corr", c_res)
assert c_plain > 0.7
assert c_res < 0.35
assert (c_plain - c_res) > 0.4
print("OK: residual vs plain correlation collapse")
 
Самопроверка в ноутбуке
  • plain: средняя |corr| > 0.7 к слою 40
  • residual: |corr| < 0.35; зазор > 0.4