损失函数与梯度下降Loss Functions & Gradient Descent
怎样把预测误差变成可反复改进参数的操作?How can prediction error become a repeatable parameter update?
前置知识:Prerequisites: B05:批量计算;B08:链式法则;B09:梯度。B05: batch computation; B08: chain rule; B09: gradients.
复习前置:Review prerequisites: B05 · 矩阵与批量计算Matrices & Batch Computation · B08 · 一元链式法则The Single-Variable Chain Rule · B09 · 偏导数与梯度Partial Derivatives & Gradients
学完后你能:After this lesson you can: 计算均方误差,完成一步更新,并识别学习率过大。Calculate mean squared error, take one update, and identify an excessive learning rate.
动手观察Explore
对两点拟合直线,单步更新参数;改变学习率并重置,比较损失与参数轨迹。Fit a line to two points one step at a time. Change the learning rate and reset to compare loss and the parameter trajectory.
从观察到解释From observation to explanation
定义误差Define error
对于数据 (0,1)、(1,3),若 w=0、b=0,预测均为 0,均方误差是 ((0−1)²+(0−3)²)/2=5。损失是数值比较,不是概率解释。For (0,1) and (1,3), w=0 and b=0 predict zero for both, giving mean squared error ((0−1)²+(0−3)²)/2=5. The loss is a numerical comparison; its probability interpretation comes later.
做一次更新Make one update
在此起点,损失对 w 的偏导是 −3,对 b 的偏导是 −4。学习率 0.1 时,沿负梯度更新得到 w=0.3、b=0.4,损失从 5 降低到 2.825。At this starting point the loss derivatives are −3 for w and −4 for b. With learning rate 0.1, the negative-gradient update gives w=0.3, b=0.4, reducing loss from 5 to 2.825.
观察步长Observe the step size
梯度给出当前位置的局部方向,学习率决定走多远。合适的步长可以逐步降低损失;过大的步长可能越过低点并造成振荡或上升。The gradient gives a local direction and the learning rate sets the step length. A suitable step can lower loss over time; an oversized step may overshoot, oscillate or increase loss.
公式与符号Formula and symbols
xᵢ、yᵢ 是第 i 个样本的输入与实际输出;w、b 是直线参数;n 是样本数;L 是均方误差;θ=(w,b),t 是步数,η>0 是学习率。xᵢ and yᵢ are the input and observed output of sample i; w and b are line parameters; n is sample count; L is mean squared error; θ=(w,b), t is the step, and η>0 is the learning rate.
留意这个区别Watch for this
有限步长不保证每一步都下降;一次下降也不能证明已经到达全局最优。MSE 与高斯似然的联系留到 B18。A finite step does not guarantee improvement, and one improvement does not prove global optimality. The connection between MSE and Gaussian likelihood comes in B18.
继续探索Explore further
继续学习算法Continue to an algorithm
课后练习
检验你的理解