彩票假说:留掩码,重置权重
The lottery ticket hypothesis
运行小型剪枝重置实验,区分初始化对照,并计入寻找掩码的成本。
lottery-ticket从问题出发
大网络训练后,有时可以剪成一个有用的小网络。彩票假说问得更具体:稀疏子网络重置回原始初始化后,能否独立训练,并在相近训练预算内匹配原网络?
Frankle与Carbin论文物理第2页把中奖彩票同时定义为连接掩码与初始参数值。保留训练后权重再微调,是另一种实验。即使一个小演示成功,也不能证明所有架构、稀疏度或优化器都满足假说;对照本身就是问题的一部分。
定义与推导
单轮发现流程先训练稠密模型,按训练后的幅值决定剪哪些项,再重置保留项:
掩码从θT选出,重训却从θ₀开始。混淆两份数组,会悄悄把重置训练变成微调。原论文还研究反复训练、剪剩余权重、再重置的迭代剪枝;本实现只做一轮。
固定掩码可在每次梯度更新后强制执行:
初始时将屏蔽项置零,每步再投影回零,否则优化器可能让声称删掉的连接重新长出来。带动量优化器还需规定状态处理;本例使用无持续优化器缓冲的普通梯度下降。
网络含四个标量tanh隐藏单元:四个输入权重、四个输出权重、四个隐藏偏置和一个输出偏置。只剪八个权重项,保留训练后绝对值最大的四个,同幅值时按索引定序,偏置不屏蔽:
所以“剪掉50%权重”不等于“剪掉全部参数项的50%”。部分未屏蔽偏置可能属于断开的单元,对预测没有作用。布尔掩码改变函数,也不会自动带来快速稀疏实现;本文数组运算仍为稠密运算。
动手计算
完整的Python 3.12 / NumPy 1.26.4程序在16个训练输入上拟合x²。稠密训练找到掩码后,比较原始初始化的掩码、同掩码的新初始化,以及相同大小的随机权重掩码,种子和预算都明确列出。
import numpy as np
def initial(seed=7):
return np.random.default_rng(seed).normal(0, 0.5, 13)
def forward(theta, x):
hidden = np.tanh(x[:, None] * theta[4:8] + theta[8:12])
return hidden @ theta[:4] / 2 + theta[12], hidden
def train(theta0, mask, steps=4000):
theta0, mask = np.asarray(theta0, dtype=float), np.asarray(mask)
if theta0.shape != (13,) or mask.shape != (13,) or mask.dtype != bool or not np.isfinite(theta0).all() or type(steps) is not int or not 0 <= steps <= 6000:
raise ValueError('Use 13 finite parameters, a Boolean mask and 0 to 6000 steps.')
x = np.linspace(-1, 1, 16); y = x ** 2
theta = theta0.copy() * mask
for _ in range(steps):
prediction, hidden = forward(theta, x)
residual = (prediction - y) / len(x)
hidden_grad = residual[:, None] * theta[:4] * (1 - hidden ** 2) / 2
grad = np.r_[hidden.T @ residual / 2, (hidden_grad * x[:, None]).sum(axis=0),
hidden_grad.sum(axis=0), residual.sum()]
theta = (theta - 0.1 * grad) * mask
return theta
def magnitude_mask(trained, keep=4):
trained = np.asarray(trained, dtype=float)
if trained.shape != (13,) or not np.isfinite(trained).all() or type(keep) is not int or not 0 <= keep <= 8:
raise ValueError('Keep an integer number of the eight weight entries.')
order = sorted(range(8), key=lambda i: (-abs(trained[i]), i))
mask = np.ones(13, dtype=bool); mask[:8] = False; mask[order[:keep]] = True
return mask
def diagnostic_mse(theta):
# This synthetic grid was inspected while developing the teaching example; it is not an untouched test set.
x = np.linspace(-0.97, 0.97, 31)
return float(np.mean((forward(theta, x)[0] - x ** 2) ** 2))
def experiment():
theta0 = initial(); dense = train(theta0, np.ones(13, dtype=bool))
mask = magnitude_mask(dense)
rng = np.random.default_rng(11); random_mask = np.ones(13, dtype=bool)
random_mask[:8] = False; random_mask[rng.choice(8, 4, replace=False)] = True
variants = {'dense': dense, 'reset ticket': train(theta0, mask),
'fresh initialization': train(initial(13), mask), 'random mask': train(theta0, random_mask)}
return theta0, dense, mask, variants
if __name__ == '__main__':
theta0, dense, mask, variants = experiment()
print('retained weight indices', np.flatnonzero(mask[:8]).tolist(), 'unmasked parameters including biases', int(mask.sum()))
for name, theta in variants.items():
print(name, 'diagnostic grid MSE', round(diagnostic_mse(theta), 9))
print('reset differs from fine-tuning start', round(float(np.linalg.norm(theta0 * mask - dense * mask)), 9))
print('updates: discovery 4000; each compared training run 4000; discovery is additional search cost')
实际输出:
retained weight indices [0, 3, 4, 7] unmasked parameters including biases 9
dense diagnostic grid MSE 0.003236354
reset ticket diagnostic grid MSE 0.001171416
fresh initialization diagnostic grid MSE 0.002254996
random mask diagnostic grid MSE 0.091124316
reset differs from fine-tuning start 3.195355338
updates: discovery 4000; each compared training run 4000; discovery is additional search cost
重置掩码在这张诊断网格上的误差最低,但新初始化也优于稠密结果,随机掩码则明显较弱。这些只是各一次运行的观察,不足以说明原始初始化总是必要。
编写例子、确定足够训练时长时查看过这张31点合成网格,因此明确称为诊断网格,不冒充未见测试集。最初600步欠拟合,最终例子让各对照都训练4000步,不据此作独立泛化结论。研究实验应预先规定选择标准,保留未参与选择的评估数据,并重复初始化。
d个可剪幅值排序成本O(d log d),选择算法可以降低该开销。发现掩码所需的稠密训练是额外工作;只拿稀疏重训阶段与稠密基线比较,会漏算搜索成本。
什么时候会失效
幅值依赖参数缩放,不一定衡量连接的功能重要性。非结构化稀疏、整通道剪枝和删层,产生不同架构与硬件机会;非零项减少也不是延迟测量。
Liu等人的剪枝研究强调强有力的从头训练基线,尤其针对其考察的结构化方法。它与彩票实验在设置和评价细节上有所不同,不能简化成“初始化总重要”对“从不重要”。比较结论前,应对齐预算、稀疏定义、重置方式、学习率和最终评估标准。本文没有替真实图像分类器发现一个未经检验的掩码。
练习与答案
- 哪份数组决定掩码,哪份初始化重训?
答案1: 训练后θT决定幅值掩码,原始θ₀初始化重置训练。
- 本例多少参数项未屏蔽?
答案2: 九项:四个权重、五个偏置,不代表九项都影响输出。
- 新初始化对照与随机掩码对照有什么不同?
答案3: 前者保留连接改数值,后者改连接,并对保留项使用已声明的原始初始化。
- 为何诊断网格结果不能叫中奖彩票证明?
答案4: 网格影响过例子开发,每种条件只跑一次,也未完成满足原假说标准的独立评估。
来源与延伸
Frankle与Carbin物理第2页给出假说及训练—剪枝—重置区别;Liu等人的摘要提供有范围限制的从头训练基线质疑。四单元网络与掩码测试均为原创。可对照损失曲面理解参数缩放,以及神经架构搜索理解显式选择结构。
先判断,再看解释
原始训练—剪枝—重置流程重置什么?
学习目标
- 实现掩码重置训练,避免误做微调。
- 区分掩码发现成本、初始化对照与评估证据。
先修与延伸
查看知识关联图 →参考文献
- The Lottery Ticket Hypothesis: Finding Sparse, Trainable Neural Networks — arXiv v5
Jonathan Frankle; Michael Carbin · 2019 - Rethinking the Value of Network Pruning — arXiv v2
Zhuang Liu; Mingjie Sun; Tinghui Zhou; Gao Huang; Trevor Darrell · 2019
版本1 · 编写状态: 已完成记录中的技术复核
查看技术检查记录(AI)
Codex · 2026-09-06 · Codex single-AI-author technical check: primary-source scope, explicit assumptions, original calculations, independent mathematical references and bilingual tests; not independent human or second-agent review.
- 单AI作者技术检查:限定范围的一手来源阅读、明确假设、原创计算、可运行示例及对应双语公式、程序和习题。不冒充人工或第二代理独立审校。
- 每篇四项CPython 3.12.4针对性测试通过,按主题使用独立导数、积分、正规方程或精确/字面轨迹参照。八个程序均使用NumPy 1.26.4;检查包括增强几何、精确二次证明、有限差分、谱解、条件风险分解及掩码神经网络训练。均为CPU教学计算,不冒充框架或GPU基准。
- 完整正文、公式、代码和习题迁入;测试只读取正式MDX,删除临时目录回退及草稿跳过,状态断言更新为checked。没有修改算法或独立参照,最终记录绑定两语SHA。
- 出版导航将已发表主题指向完整文章并移除对应待写措辞,未发表目标保留目录入口;原理、公式、程序与习题未改,重新绑定最终正文SHA。
核对的来源位置:
- The Lottery Ticket Hypothesis: Finding Sparse, Trainable Neural Networks — arXiv v5 · Physical page 2: hypothesis, mask plus original initialization, one-shot train–prune–reset steps and distinction from iterative pruning.
- Rethinking the Value of Network Pruning — arXiv v2 · Physical page 1 abstract: from-scratch controls, structured-pruning experiment scope and comparison with lottery-ticket initialization.
相关主题