← 深度学习地图
CORE PATH · 01

训练机制与损失函数

先看神经网络怎样从输入走到新权重,再把 Sigmoid、Softmax、Cross Entropy 与 KL 放回同一个概率学习流程中。

神经网络怎么学习:一个不断重复的闭环

01Input:输入图片或特征
02前向传播(Forward Propagation):利用当前 Weight 计算 Prediction
03损失函数(Loss Function):衡量 Prediction 和真实值之间的误差
04反向传播(Backpropagation):利用 Chain Rule 计算每个 Weight 的 Gradient
05Gradient:告诉每个 Weight 应该增加还是减少,才能最快降低 Loss
06梯度下降(Gradient Descent):根据 Gradient 更新 Weight
得到新的 Weight,进入下一轮 Forward,持续训练
一句话:Forward 负责“算答案”,Loss 负责“打分”,Backpropagation 负责“追责”,Gradient Descent 负责“改参数”。

从 logits 到真实分布:四个关键词是一条流程

Logits神经网络输出的原始分数
Softmax把多个分数变成概率分布
One-hot真实标签也表示成概率分布
Cross Entropy根据真实类别的预测概率计算 Loss
Training不断减小 Loss
KL ↓预测分布越来越接近真实分布
Binary

Sigmoid

二分类:把一个分数压到 0–1,变成一个概率。

Multi-class

Softmax

多分类:把多个 logits 变成总和为 1 的概率分布。

Loss

Cross Entropy

给模型打分:真实类别的预测概率越低,Loss 越大。

Distance

KL Divergence

衡量两个概率分布差多少;训练的本质是让这个差异越来越小。

关键理解:它们不是四个孤立定义,而是完整概率学习流程里的四个角色。看到公式时,先问“它现在负责把分数变概率、计算 Loss,还是比较分布?”
CALCULATION

Softmax 不能直接按分数比例归一化

\[p_i=\frac{e^{z_i}}{\sum_j e^{z_j}}\]

例如 \(z=[1,2,3]\),必须先算 \(e^1,e^2,e^3\),再除以指数和;答案不是 \([1/6,2/6,3/6]\)。实际计算时可先减去 \(\max(z)\),结果不变且更稳定。For \(z=[1,2,3]\), first compute \(e^1,e^2,e^3\), then divide by their sum; the answer is not \([1/6,2/6,3/6]\). Subtracting \(\max(z)\) first gives the same result with better numerical stability.

IDENTICAL INPUTS

重复输入只能学经验概率

同一个输入出现 100 次,其中 80 次标签为 1、20 次为 0。模型看不到可区分信息时,交叉熵下的最优输出是 \(P(y=1\mid x)=80/100=0.8\)。The same input appears 100 times: 80 labels are 1 and 20 are 0. With no distinguishing information, the Cross Entropy optimum is \(P(y=1\mid x)=80/100=0.8\).

The optimal prediction equals the empirical class probability.

Hidden representation 与非线性为什么必要?

6D → 2D

Hidden Unit Dynamics

隐藏层把 6D 输入映射成 \((h_1,h_2)\)。每个输出神经元在这个表示空间中学习自己的 decision boundary;隐藏层的目标是让后续类别更容易线性分离。The hidden layer maps a 6D input to \((h_1,h_2)\). Each output neuron learns its own decision boundary in this representation space, making the classes easier to separate linearly.

The hidden layer learns a representation in which the outputs become linearly separable.

LINEAR STACK

多层 Linear 仍是一层 Linear

\[W_3(W_2(W_1x+b_1)+b_2)+b_3=Wx+b\]

矩阵乘法与偏置可以合并。因此堆叠 linear transfer functions 不会增加表达能力。

NONLINEARITY

ReLU / sigmoid / tanh 的作用

非线性激活打破 \(Wx+b\) 的合并,使网络能形成弯曲、分段或复杂的 decision boundary。没有非线性,再深也只是线性模型。Nonlinear activations prevent collapse into \(Wx+b\), allowing curved, piecewise, or complex decision boundaries. Without nonlinearity, depth still produces only a linear model.

为什么 Sigmoid 与 Cross Entropy 数学上很匹配?

SSE + Sigmoid

当输出接近 0 或 1,Sigmoid 进入饱和区,导数很小。多层反向传播继续相乘后,学习信号可能接近 0。

Cross Entropy + Sigmoid

交叉熵会抵消 Sigmoid 导数,输出层对 logit 的梯度简化为 z − t。即使预测很错,仍能提供较强学习信号。

∂L / ∂a = z − tz:预测概率 · t:真实标签 · a:Sigmoid 前的 logit
SOFTMAX + CE

负对数损失的梯度

\[L=-\log p_y\quad\Longrightarrow\quad\frac{\partial L}{\partial z}=p-y\]

正确类别分量为 \(p_y-1\),其他类别分量为 \(p_j\)。这里的 \(y\) 是 one-hot target。For the correct class, the component is \(p_y-1\); every other component is \(p_j\). Here \(y\) is the one-hot target.

SIGN CHECK

若题目问的是 log probability

\[\frac{\partial\log p_y}{\partial z}=y-p\]

它与 Cross Entropy 梯度方向相反,因为 Cross Entropy 前面多了负号。先看清题目是 \(\log p_y\) 还是 \(-\log p_y\)。Its gradient points in the opposite direction because Cross Entropy has a leading minus sign. Check whether the question uses \(\log p_y\) or \(-\log p_y\).

FINAL HIGH FREQUENCY · 建议直接背

English

SSE combined with sigmoid may produce very small gradients when the output neuron is saturated. Cross Entropy cancels the sigmoid derivative, resulting in the simple gradient z−t, which provides stronger learning signals and faster convergence.

中文

SSE 与 Sigmoid 结合时,在输出接近 0 或 1 的饱和区容易出现梯度很小的问题;而 Cross Entropy 会抵消 Sigmoid 导数,最终梯度简化为 z−t,因此训练更稳定、收敛更快。

为什么深层网络训练困难?

Vanishing Gradient

很多小于 1 的导数跨层相乘,梯度越来越小,前面层几乎学不到。

Exploding Gradient

较大的导数反复相乘,梯度迅速放大,更新不稳定。

Initialization

合适的参数尺度让激活与梯度在层间保持合理范围。

BatchNorm + Residual

BatchNorm 稳定激活分布;Residual 提供梯度捷径。

为什么 Sigmoid 不适合深网络?

Sigmoid 在饱和区导数接近 0,多层反向传播时梯度不断相乘,容易导致 vanishing gradient。

为什么 ReLU 更适合深网络?

ReLU 在正区间导数为 1,梯度更容易传播;但负区间长期无激活时可能出现 dead ReLU。

BatchNorm 的作用

训练时先用 mini-batch 的 mean 与 variance 标准化:\(\hat{x}=(x-\mu_B)/\sqrt{\sigma_B^2+\epsilon}\),再用可学习的 \(\gamma,\beta\) 缩放和平移。它让训练更稳定、通常更快收敛;核心定义是 normalization,不要只回答“正则化”。During training, BatchNorm uses the mini-batch mean and variance: \(\hat{x}=(x-\mu_B)/\sqrt{\sigma_B^2+\epsilon}\), then applies learnable \(\gamma,\beta\) scale and shift. It stabilizes training and usually speeds convergence; its defining operation is normalization.

ResNet 为什么有效?

引入 skip connection,网络学习残差 F(x),输出为 x + F(x)。梯度可沿捷径传播,缓解梯度消失,使更深网络可训练。

整章知识树

Deep Learning ├── 神经网络怎么学习? │ ├── Forward │ ├── Loss │ ├── Gradient │ ├── Backpropagation │ └── Gradient Descent ├── Loss Function │ ├── SSE │ ├── Cross Entropy │ ├── Softmax │ ├── KL Divergence │ └── Maximum Likelihood ├── 为什么训练困难? │ ├── Vanishing / Exploding Gradient │ ├── Initialization │ ├── BatchNorm │ └── Residual Connection ├── CNN ├── RNN ├── Transformer └── Reinforcement Learning
最终记忆:训练就是让预测分布越来越接近真实分布。Forward 产生预测,Loss 衡量差异,Backpropagation 算梯度,Gradient Descent 更新权重,再进入下一轮。