温度缩放 Logits

🖼️ Temperature-Scaled Logits Demo

Scale logits by a temperature before softmax and watch the calibrated distribution change, a practical demo of confidence calibration.

📐 计算公式 / 原理
温度缩放(本页代码口径):z′=logit÷T(T 为温度);对比 logit 同式缩放 logit₂÷T 一并输出。校验:无强校验,空值按 0 兜底;T≤0 时提示「温度 T 必须大于 0(T=0 时 logit÷T 无定义)」。判据:T>1 分布更平滑(置信度被压低)、T<1 更尖锐(置信度被放大)、T=1 保持原状。
对 logits 统一除以温度是最简单的概率校准手段,T 在验证集上用 NLL 搜索得到,不改变 argmax 因此不影响准确率,只改善置信度的可靠性(ECE)。它只能校正整体过度/不足自信,无法修正逐类别偏差。

📐 计算公式与说明

缩放判读(与上式互补):scaled=logit÷T 为线性缩放,同一样本所有类别 logit 除以同一 T,相对大小关系不变、softmax 概率排序不变;T 趋大越接近均匀分布、趋小越接近 one-hot。校验:与上方一致,T 须>0。判据:无固定分级,页面以缩放值与对比缩放值并列展示同 T 下的等比缩放关系。

仅用于演示 softmax 前缩放,不改变 argmax。

📚 深度解析:温度缩放 Logits

💡 常见使用场景

校准演示
logits=[3,1,0],T=1 → 概率≈[0.8438,0.1142,0.0420];T=2 → [0.6285,0.2312,0.1402],最高概率由 84.38% 降到 62.85%,更接近真实准确率即达到校准目的;T=0.5 → [0.9796,0.0179,0.0024] 过度自信。注意缩放只改数值大小,argmax 仍是第 1 类。

❓ 常见问题(FAQ)

温度缩放能提升准确率吗?
不直接提升分类准确率(argmax 不变),只让输出置信度更'诚实'地反映正确率,对需要可靠概率的应用(如风险决策)很有价值。
T 用什么值校准?
在验证集上用负对数似然最小化搜索最优 T(通常略大于 1),使预测置信度与经验准确率对齐;是后处理、不改模型权重。

Formula & Standard References

Item Formula / Note Source / Standard
Scaling formula temperature scaling divides the logits by the temperature before softmax Guo et al. 2017, ICML calibration
Ranking invariance with a positive temperature the ordering and argmax are preserved Guo et al. 2017, ICML calibration
Calibration use temperatures above one reduce overconfidence and below one sharpen predictions Hinton et al. 2015, knowledge distillation