M2-089M2: Classical Machine LearningEvaluation Metrics & Hyperparameter TuningHard
⚡Flashcard Drill➔Mastery:

Handling Randomness and Seed Variance in Hyperparameter Tuning

📐 Mathematical Definition
σ^seed2=1S∑s(perfs−perfˉ)2\hat\sigma^2_{seed}=\frac1S\sum_s(\mathrm{perf}_s-\bar{\mathrm{perf}})^2
⚡ One-Sentence Core Answer
Evaluate configurations across multiple random seeds, report mean and confidence intervals, and apply paired statistical tests to distinguish genuine improvements from seed noise.

📌 Key Takeaways

  • •
    Sources of randomness: weight initialization, mini-batch shuffling, data splits, non-deterministic GPU operations
  • •
    Seed variance risk: '0.5% gain' may simply reflect an outlier favorable random seed
  • •
    Paired comparison: evaluate candidate models using identical random seeds and data folds to cancel shared noise

📐 Mathematical Derivations

Statistical Formulation: Let performance metric of model AA on seed ss be XA,s=μA+ϵsX_{A, s} = \mu_A + \epsilon_s, where ϵs∼N(0,σ2)\epsilon_s \sim \mathcal{N}(0, \sigma^2). If seed variance σ\sigma is 1.0%1.0\% and the true improvement Δ=μA−μB=0.5%\Delta = \mu_A - \mu_B = 0.5\%, a single-seed trial has a high probability P(XA,s<XB,s)=Φ(−Δ2σ)=Φ(−0.35)≈36.3%P(X_{A, s} < X_{B, s}) = \Phi\left(-\frac{\Delta}{\sqrt{2}\sigma}\right) = \Phi(-0.35) \approx 36.3\% of falsely declaring model AA inferior.
Resolution via Averaging: Evaluating across SS independent seeds reduces standard error of the mean to σ/S\sigma / \sqrt{S}. To detect effect size Δ\Delta with statistical power 1−β=0.801 - \beta = 0.80 at α=0.05\alpha = 0.05, required seeds: S≥2(zα/2+zβΔ/σ)2S \ge 2 \left( \frac{z_{\alpha/2} + z_\beta}{\Delta / \sigma} \right)^2.
Paired $t$-test: Compute seed-wise differences Ds=XA,s−XB,sD_s = X_{A, s} - X_{B, s} using identical seeds, testing H0:μD=0H_0: \mu_D = 0 via t=DˉsD/St = \frac{\bar{D}}{s_D / \sqrt{S}}.

🏭 Production Trade-offs

Standardized best practices: ① Never report the single best seed (which represents extreme selection bias / lucky draw). Always report Mean±Std\text{Mean} \pm \text{Std} over at least 3–5 seeds. ② Budget allocation: If evaluation noise is large, reduce the number of tested hyperparameter configurations and allocate compute to multi-seed verification. ③ Reproducibility: Set deterministic flags (e.g., torch.use_deterministic_algorithms(True)) during regression tests, though note small training speed penalties.
⚠️ Common Interview Pitfalls
  • ✕
    Picking the single best random seed from 20 runs and reporting it as the model's actual performance benchmark
  • ✕
    Declaring an architectural improvement based on a 0.2% metric bump when single-seed standard deviation is 0.6%
🎯 Interviewer Follow-up Inquiries
  • ?
    Why is a paired tt-test statistically more powerful than an two-sample independent tt-test when comparing models across seeds?
  • ?
    How does selection bias affect reported benchmarks when picking the maximum validation score across 100 random seeds?
⚡
Test muscle memory on this topic via 3-Layer Flashcards
Active Recall: Mentally formulate Prompt ➔ Math Derivation ➔ Benchmark Implementation in 3 minutes.
🎯 Drill This Card Now➔
📚

Associated Knowledge Base Guides & Mindmaps

Explore the comprehensive technical article, exam cards, and global architecture tree.

← PreviousM2-088: Hyperparameter Importance Hierarchy and Efficient Tuning Strategies📋Back to BankNext →M2-090: Why Offline and Online Evaluations Diverge: Causes and Mitigation Strategies
中文速览与面试要点

如何处理调参中的随机性(种子方差)?

多次随机种子重复评估,报告均值与标准差;用配对检验比较模型。

核心要点

  • 随机性来源与影响:① 初始化 ——不同的随机初始化会收敛到不同的局部解,性能差异在小模型上可达数个百分点;② 数据划分/采样顺序 ——K 折的划分、SGD 的样本顺序、数据增强的随机性;③ dropout/正则 ——训练时的随机性;④ 硬件非确定性 ——浮点累加顺序(尤其在 GPU 并行归约时)。 为什么重要 :若用单次运行的结果比较两个模型,观测到的差异可能完全来自随机噪声——'模型 A 比 B 好 0.5%'可能只是 A 恰好抽到好种子。这在 小数据、小模型、短训练 时尤其严重(方差大)。 标准做法 :对每个配置用 多个随机种子 (S=3–10,视方差与成本而定)重复训练,报告 均值 ± 标准差 (或标准误、置信区间),而非单点最优值。
  • 实践要点:① 种子数的选择 ——方差大时需更多种子;可用'序贯测试'(先跑 3 个种子,若差异显著则停止,否则加种子)。② 配对检验 ——比较两个模型时,应使用 相同的种子/数据划分 (配对设计),这样能消除共同的随机性、提高检验功效;用配对 t 检验或 Wilcoxon 符号秩检验,而非独立样本检验。③ 不要报'最优种子' ——报告所有种子的均值与方差(报最优是选择性偏差,会高估性能);若必须报单点,报中位数而非最大值。④ 选择偏差 ——在大量配置中挑'验证集最好的'会高估真实性能(见'选择性偏差'题);应用独立测试集或嵌套 CV 确认。⑤ 减小随机性的手段 ——确定性算法(如全批量梯度下降)、固定所有随机种子、关闭非确定性算子( torch.use_deterministic_algorithms );但需注意确定性可能降低性能或速度。⑥ 报告规范 ——论文/报告中应说明种子数、是否配对、方差来源分解;仅在差异超过方差时才声称改进。
  • 小数据/小模型方差大,需多seed
  • 报告置信区间而非单点最优

常见陷阱

  • 用单次运行结果比较模型(噪声主导)
  • 报告最优种子的性能(选择性偏差)