Back to topic
Optional
Magnitude pruning: density and error
Zero out 90% of weights by |w|; density, Frobenius error, renormalization. Self-check in the notebook.
- Version
- v1
- Minutes
- ~15
- 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 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 · Сжатие разреживанием / Magnitude sparsity
Activity version: 1 · Seed: 0 · Данные: случайная матрица весов .
Тема Сжатие моделей: keep top-10% by . Плотность мала, но доля энергии заметно выше плотности.
Ожидаемые числа (seed 0)
| Quantity | Value | Tolerance |
|---|---|---|
| density | 0.10 |
exact |
| relative F-error | ≈ 0.75 |
abs ≤ 0.05 |
| energy retained | ≈ 0.44 |
within [0.35, 0.55] (> density) |
Самопроверка в ноутбуке
Не влияет на mastery.
import numpy as np
rng = np.random.default_rng(0)
W = rng.normal(size=(50, 40)) # 2000 weights; 10% = 200
k = W.size // 10
idx = np.argsort(np.abs(W), axis=None)
mask = np.zeros(W.size, dtype=bool)
mask[idx[-k:]] = True
mask = mask.reshape(W.shape)
W_hat = W * mask
density = float(mask.mean())
rel = float(np.linalg.norm(W - W_hat) / np.linalg.norm(W))
energy = float((W_hat ** 2).sum() / (W ** 2).sum())
print("density", density, "rel", rel, "energy", energy)
assert density == 0.10
assert abs(rel - 0.75) < 0.05
assert 0.35 <= energy <= 0.55
assert energy > density
print("OK: compression sparsity")
Самопроверка в ноутбуке
- density = 0.10
- относительная ошибка ≈ 0.75
- retained energy ≈ 0.44 > density