Dive into Deep Learning · §3.1
softmax 回归:用于类别概率的线性模型
独热编码 · 仿射 logit · softmax · 平移不变性 · 交叉熵 · \hat{\mathbf{y}} - \mathbf{y} 梯度 · 温度
动机与失效模式
用连续标量回归将服饰照片分为 q = 10 个无序类别会立刻失效:
目标表示
考虑含四个像素特征 (x_1, x_2, x_3, x_4)、q = 3 类(猫 1、鸡 2、狗 3)的 2 \times 2 灰度图像:
线性层
softmax 回归为每个类别 j \in \{1, \ldots, q\} 分配独立的权重向量 \mathbf{w}_j \in \mathbb{R}^d 与偏置 b_j \in \mathbb{R}:
概率归一化
softmax 算子对每个 logit 取指数再除以其配分和:
\hat{y}_j = \mathrm{softmax}(\mathbf{o})_j = \frac{\exp(o_j)}{\sum_{k=1}^q \exp(o_k)}
softmax 的结构性质
指数将加法平移转化为可约去的公因子:
\mathrm{softmax}(\mathbf{o} + c\mathbf{1})_j = \frac{\exp(c)\exp(o_j)}{\exp(c)\sum_{k=1}^q \exp(o_k)} = \mathrm{softmax}(\mathbf{o})_j
float32 上溢(o > 88.7)。与二元分类的联系
当 q = 2 时,平移不变性意味着两个概率仅取决于标量差 o = o_1 - o_2:
\hat{y}_1 = \frac{1}{1 + \exp(-(o_1 - o_2))} = \sigma(o_1 - o_2)
批量矩阵形式
代码中通过单次 GEMM 加按行 softmax 处理含 n 个样本、d 个特征的小批量:
\mathbf{O} = \mathbf{X}\mathbf{W} + \mathbf{b}, \qquad \hat{\mathbf{Y}} = \mathrm{softmax}(\mathbf{O})
axis=1, keepdims=True)。线性决策边界
softmax 保持 logit 排序,因此类别 j 在所有 k \neq j 满足 o_j \ge o_k 处胜出:
损失的概率推导
在 \hat{y}_j = P(y = j \mid \mathbf{x}) 下观测到真实类别 y(独热向量 \mathbf{y} \in \{0, 1\}^q)的负对数似然即为交叉熵损失:
l(\mathbf{y}, \hat{\mathbf{y}}) = -\sum_{j=1}^q y_j \log \hat{y}_j = -\log \hat{y}_y
优化陷阱:严重误判时的梯度消失
在 softmax 输出上最小化平方误差 \frac{1}{2}\|\hat{\mathbf{y}} - \mathbf{y}\|_2^2 会在自信误判处停滞:
精确梯度推导
通过 log-sum-exp 函数直接用 logit \mathbf{o} 表示交叉熵:
l(\mathbf{y}, \hat{\mathbf{y}}) = \log \sum_{k=1}^q \exp(o_k) - o_y
对 o_j 求导即得精确残差向量:
\nabla_{\mathbf{o}}\, l = \hat{\mathbf{y}} - \mathbf{y}
对参数的链式法则
通过 \mathbf{O} = \mathbf{X}\mathbf{W} + \mathbf{b} 反向传播得到小批量参数梯度:
\nabla_{\mathbf{W}} L = \frac{1}{n}\mathbf{X}^\top(\hat{\mathbf{Y}} - \mathbf{Y}), \qquad \nabla_{\mathbf{b}} L = \frac{1}{n}(\hat{\mathbf{Y}} - \mathbf{Y})^\top\mathbf{1}_n
为何称为交叉熵
在 Shannon 信息论中,概率为 p 的事件携带惊异 -\log p:
准确率与可微训练
定义样本的分类间隔为真实类 logit 减最高竞争 logit,m = o_y - \max_{j \neq y} o_j:
置信度与决策边界
交叉熵在 m > 0 时仍持续下降,即使全部决策已正确,最小化它仍会把预测概率推向 0 和 1:
概念自测
根据 softmax 与交叉熵的性质解答以下三题:
答案:(1) 每个 logit 减去 100 得 \mathrm{softmax}(2, 0, 0) \approx (0.79, 0.11, 0.11),比值为 e^{102-100} = e^2 \approx 7.39。(2) (0.6, 0.4) 给出 H(\mathbf{p}) = -0.6\log 0.6 - 0.4\log 0.4 \approx 0.67\text{ nats},优于 (0.9, 0.1) 的 -0.6\log 0.9 - 0.4\log 0.1 \approx 0.98\text{ nats}。(3) 总损失为 60{,}000 \times 0.10 = 6{,}000\text{ nats},每次误判(m \le 0)至少消耗 \log 2 \approx 0.693\text{ nats},因此至多 6{,}000 / 0.693 \approx 8{,}656 次错误(14.4\%)。
本节小结
| 概念 | 数学形式 | 核心性质 / 实际作用 |
|---|---|---|
| 独热目标 | \mathbf{y} \in \{0, 1\}^q,\; y_y = 1,\; \sum_j y_j = 1 | 任意两类间距均为 \sqrt{2};合法类别分布 |
| 批量仿射 logit | \mathbf{O} = \mathbf{X}\mathbf{W} + \mathbf{b} \in \mathbb{R}^{n \times q} | d \cdot q + q 个参数(Fashion-MNIST 上为 7{,}850);边界为平直超平面 |
| softmax 算子 | \hat{y}_j = \exp(o_j) / \sum_{k=1}^q \exp(o_k) | 保序(\operatorname{argmax} \hat{y}_j = \operatorname{argmax} o_j);平移不变(\mathrm{softmax}(\mathbf{o}+c\mathbf{1}) = \mathrm{softmax}(\mathbf{o})) |
| 对数几率与 q=2 极限 | o_i - o_j = \log(\hat{y}_i / \hat{y}_j),\; \hat{y}_1 = \sigma(o_1 - o_2) | logit 之差即对数几率;二分类 softmax 等价于二元逻辑回归 |
| 交叉熵损失 | l(\mathbf{y}, \hat{\mathbf{y}}) = -\log \hat{y}_y = \log\sum_k \exp(o_k) - o_y | 负对数似然;关于 (\mathbf{W}, \mathbf{b}) 凸;0\text{--}1 错误上界(误判 \ge \log 2) |
| logit 与权重梯度 | \nabla_{\mathbf{o}} l = \hat{\mathbf{y}} - \mathbf{y},\; \nabla_{\mathbf{W}} L = \frac{1}{n}\mathbf{X}^\top(\hat{\mathbf{Y}}-\mathbf{Y}) | 预测减目标;自信误判时梯度不消失(区别于平方误差) |
| 温度缩放 | \hat{\mathbf{y}}(T) = \mathrm{softmax}(\mathbf{o} / T),\; T > 0 | 调节置信度(T \to 0 为 argmax,T \to \infty 为均匀分布),不改变类别预测 |