M2-084M2: Classical Machine LearningModel CalibrationMedium
⚡Flashcard Drill➔Mastery:

Temperature Scaling for Calibrating Deep Neural Networks

📐 Mathematical Definition
p^=softmax(z/T)\hat p=\mathrm{softmax}(z/T)
⚡ One-Sentence Core Answer
A single scalar parameter T>0T > 0 divides pre-softmax logits, softening or sharpening probabilities without changing the argmax prediction order or classification accuracy.

📌 Key Takeaways

  • •
    Mechanism: p^i=exp⁡(zi/T)∑jexp⁡(zj/T)\hat{p}_i = \frac{\exp(z_i / T)}{\sum_j \exp(z_j / T)} with learned scalar T>0T > 0
  • •
    Accuracy preservation: strictly preserves logit ranking, ensuring zero change in top-1 classification accuracy
  • •
    Overconfidence alleviation: modern deep networks typically converge to T>1T > 1, which softens overconfident extremes

📐 Mathematical Derivations

Formulation: Let z=(z1,…,zK)z = (z_1, \dots, z_K) be the unnormalized logit vector produced by the penultimate layer. Temperature scaling modifies the softmax operator with temperature parameter T>0T > 0: p^k(x;T)=exp⁡(zk/T)∑j=1Kexp⁡(zj/T)\hat{p}_k(x; T) = \frac{\exp(z_k / T)}{\sum_{j=1}^K \exp(z_j / T)}.
- Entropy & Softness: When T>1T > 1, logits are compressed toward 0, pushing softmax outputs toward uniform distribution (increasing entropy, mitigating overconfidence). When T<1T < 1, logits expand, sharpening confident predictions.
- Preservation of Argmax: Because division by T>0T > 0 is a strictly monotonic transformation, arg⁡max⁡k(zk/T)=arg⁡max⁡kzk\arg\max_k (z_k / T) = \arg\max_k z_k. Top-1 accuracy and top-kk classification predictions remain completely unchanged.
- Optimization: All neural network weights are frozen; TT is optimized via Negative Log-Likelihood (NLL) on a held-out validation set using simple 1D gradient descent or L-BFGS: min⁡T−∑i=1Nvallog⁡p^yi(xi;T)\min_T - \sum_{i=1}^{N_{\text{val}}} \log \hat{p}_{y_i}(x_i; T).

🏭 Production Trade-offs

Engineering advantages: Requires optimizing only 1 scalar parameter, making it virtually immune to overfitting even on small validation datasets. Extremely lightweight to deploy in production inference servers (simple logit division). Limitation: Cannot correct non-monotonic miscalibration.
⚠️ Common Interview Pitfalls
  • ✕
    Optimizing temperature TT on the training set, which drives T→0T \to 0 due to cross-entropy overfitting
  • ✕
    Expecting temperature scaling to fix misclassifications, when it only alters probability calibration without changing top-1 error
🎯 Interviewer Follow-up Inquiries
  • ?
    Why is temperature scaling strictly incapable of altering a model's top-1 accuracy or ROC-AUC?
  • ?
    How does vector scaling or matrix scaling extend temperature scaling, and what overfitting risks do they introduce?
⚡
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-083: Expected Calibration Error (ECE): Mathematical Formulation and Practical Limitations📋Back to BankNext →M2-085: Scenarios Where Calibration is Critical: Core Use Cases and Concrete Examples
中文速览与面试要点

温度缩放(temperature scaling)如何校准深度网络?

在 logits 上除以温度 T,用验证集拟合 T;不改预测类别,只调整置信度。

核心要点

  • 温度缩放的机制:深度网络的 softmax 输出为 p̂=softmax(z),其中 z 是 logits。温度缩放引入参数 T>0:p̂'=softmax(z/T)。当 T>1 时,分布变得更 平坦 (降低最大概率、抬高其他概率)→ 缓解过度自信;T 关键性质 :① 不改变预测类别 ——因为 argmax(z/T)=argmax(z)(除以正数不改变排序),故准确率不变(或几乎不变);② 单参数 ——只需在验证集上最小化 NLL 拟合一个 T,计算极便宜、不易过拟合;③ 改善 NLL 与 ECE ——实验表明温度缩放能显著降低深度网络的 ECE(如从 10% 降到 2%)。 为什么深网过度自信 :与训练目标(交叉熵)在过参数化模型上的行为有关——模型会把 logits 的尺度推得很大以降低训练损失,而 NLL 对'自信但错误'的惩罚在训练集上被最小化后,泛化到测试集时表现为过度自信;此外现代训练技巧(BN、大 batch、强数据)都会加剧过度自信。
  • 实践要点:① T 的拟合 ——在 独立验证集 上最小化 NLL(等价于最大化校准后的似然);可用 LBFGS 或简单网格搜索(T 通常落在 1–3)。② 与知识蒸馏的区别 ——蒸馏中的温度 T 用于 软化教师的输出 (让学生学到类间相似性),是训练时用的;校准的温度用于 修正已训练模型的置信度 ,是后处理;两者数学形式相同但目的不同(蒸馏的 T 常取 3–20,校准的 T 通常 与其他校准方法的对比 ——Platt scaling 在校准 sigmoid 后可能改变排序(多参数),温度缩放严格保持排序(单参数);Isotonic 更灵活但过拟合风险大;实践中温度缩放是深度网络的默认首选。④ 局限 ——温度缩放只做 全局 缩放(所有样本同一 T),若模型的过度自信程度随样本变化(如难样本更过度自信),需更精细的方法(如输入相关的温度、直方图分箱、或 Mixup 等训练时正则化,后者同时改善泛化与校准)。⑤ 分布漂移下失效 ——线上分布变化后 T 需重新拟合。
  • 现代深度网络普遍过度自信(T>1)
  • 单参数、几乎不损精度

常见陷阱

  • 用训练集拟合温度(应使用独立验证集)
  • 认为温度缩放能提升准确率(它只改校准)