损失函数 · 先把“错多少”算出来Loss Function · First Compute “How Wrong”
prediction + target → one quantity to minimize
损失函数把预测与目标的差异压成一个训练要尽量减小的数。没有这个可比较的数,参数更新就不知道哪次改变更好。A loss function compresses the gap between prediction and target into one number training
tries to reduce. Without it, an update cannot tell whether a change helped.
01 · 像量尺一样01 · Think like a ruler
两次投篮都没进,怎样让机器判断哪一次更接近篮筐?Two shots both miss. How can a machine tell which one landed closer?
|ŷ−y|
02拖动预测点,看误差绳和损失高度Drag the Prediction; Watch Distance and Loss
唯一变量:预测值One variable: prediction
目标Target
7.0
y
误差距离Error distance
3.0
|ŷ − y|
损失Loss
3.0
absolute
03损失函数的 4 个零件Four Parts of a Loss Function
input → compare → penalize → reduce
PREDICTION
预测 ŷPrediction ŷ
模型当前给出的答案。The model's current answer.
TARGET
目标 yTarget y
这道题希望得到的参照答案。The reference answer for this example.
RULE
惩罚规则Penalty rule
绝对值与平方会以不同形状放大错误。Absolute and squared rules amplify error differently.
SCALAR
一个数One number
训练比较更新前后哪个损失更小。Training compares which update has lower loss.
04最小机制:差异经过规则才成为损失Minimal Mechanism: A Rule Turns Difference into Loss
L(ŷ,y)
L1 = |ŷ − y| L2 = (ŷ − y)2
同样相差 3,绝对损失为 3,平方损失为
9。规则决定“大错”被惩罚多重。For the same gap of 3, absolute loss is 3 and squared loss is 9. The rule decides how
strongly large errors are penalized.
移动预测Move prediction改变 ŷ − yChange ŷ − y
应用规则Apply rule映射成非负惩罚Map to a nonnegative penalty
比较大小Compare values更小表示更接近目标Lower means closer to target
05边界实验:让一个大错被平方放大Boundary Test: Square a Large Error
主动制造失败Create a failure
把预测拖到 0,再切换平方损失Move prediction to 0, then use squared loss
观察误差距离仍是 7,但平方损失升到
49。损失不是误差本身,而是“如何惩罚误差”的规则。The error distance remains 7, while squared loss jumps to 49. Loss is not the error
itself; it is the rule for penalizing error.
尚未执行。Not run yet.
✗ 损失就是预测值。Loss is the prediction.
→ ✓ 损失由预测、目标和惩罚规则共同决定。Prediction, target, and penalty rule determine loss.
✗ 所有损失都按同样方式惩罚错误。Every loss penalizes error the same way.
→ ✓ 不同曲线会改变训练偏好。Different curves change training priorities.
✗ 损失低就证明真实任务一定好。Low loss proves the real task works.
→ ✓ 它只证明在当前目标与数据上更合规则。It only shows better fit to this objective and data.
06 · 一句话带走06 · One line to keep
损失函数把预测与目标的差异变成一个可比较、可被训练压低的数。A loss function turns the prediction-target gap into a comparable number training can
reduce.