← Back to Deep Learning Mind Map
🧠 Deep LearningID: loss-functions-dl

Loss Functions

损失函数族
🎯Core Definition
The loss family dictates the geometry of the objective and convergence speed. Cross-entropy is standard for classification: L=−∑iyilog⁡piL=-\sum_{i}y_i\log p_i, reducing to BCE −[ylog⁡y^+(1−y)log⁡(1−y^)]-[y\log\hat y+(1-y)\log(1-\hat y)] for binary cases; regression uses MSE: L=1N∑∥y−y^∥2L=\frac{1}{N}\sum\Vert y-\hat y\Vert^2. The gradient contrast is the interview crux: softmax+CE yields pi−yip_i-y_i — linear, never saturating; softmax+MSE yields (pi−yi)pi(1−pi)(p_i-y_i)p_i(1-p_i), where p(1−p)→0p(1-p)\to0 at extreme predictions, killing gradients and slowing learning. For class imbalance, Focal Loss: FL(pt)=−αt(1−pt)γlog⁡ptFL(p_t)=-\alpha_t(1-p_t)^\gamma\log p_t; at γ=2\gamma=2 easy samples are down-weighted by (1−pt)2(1-p_t)^2. Contrastive learning uses InfoNCE: −log⁡esim(q,k+)/τ∑j=1Kesim(q,kj)/τ-\log\frac{e^{\text{sim}(q,k_+)/\tau}}{\sum_{j=1}^{K}e^{\text{sim}(q,k_j)/\tau}}, where temperature τ\tau controls negative-sample pressure; Triplet Loss: max⁡(0, d(a,p)−d(a,n)+m)\max(0,\,d(a,p)-d(a,n)+m) with margin mm.
💡Use Cases
interview favorites — “why CE for classification but MSE for regression?”, “gradient difference between CE and MSE under softmax?”, “how does Focal Loss fix imbalance?”, “how to tune InfoNCE temperature?”; RetinaNet, CLIP/SimCLR and multi-label BCE are common follow-ups.
⚡Key Problems Solved
convergence speed and robustness — CE's log⁡\log cancels the softmax denominator to give the linear gradient p−yp-y, while MSE's gradient vanishes under p(1−p)p(1-p) in saturated regions, so CE converges up to an order of magnitude faster on the same net; Focal down-weights abundant easy samples via (1−pt)γ(1-p_t)^\gamma, making RetinaNet the first one-stage detector to beat two-stage on COCO; InfoNCE's τ\tau sets the difficulty weight of negatives and directly determines CLIP/SimCLR embedding quality.
🎯Core Interview Exam Points
Fundamentals · Production Scenarios · Deep Dive
Core Def
Explain the core definition, underlying mechanism/math, and key principles of Loss Functions.
Use Cases
In production engineering, what are the primary use cases for Loss Functions and what core pain points does it solve?
🔥 High-Frequency Deep-Dive Questions
1
Derive the softmax+CE gradient pi−yip_i-y_i; contrast with softmax+MSE (pi−yi)pi(1−pi)(p_i-y_i)p_i(1-p_i) — why does CE converge faster?
2
Write Focal Loss FL(pt)=−αt(1−pt)γlog⁡ptFL(p_t)=-\alpha_t(1-p_t)^\gamma\log p_t; how do easy-sample weights change at γ=2\gamma=2 and why does it fix imbalance?
3
Write the InfoNCE formula and explain temperature τ\tau; why does a too-small τ\tau over-strengthen negative-sample gradients?
4
Relation between CE and BCE? Why BCE for multi-label classification instead of multi-class CE?
5
Why is CE not used for regression? Compare MSE / MAE / Huber gradients and outlier robustness?
Updated 2026-08-12
🎯
Verify Mastery: Practice Exam Questions for "Loss Functions"
Take pitfall multiple-choice questions with derivation options; mistakes are automatically logged.
🚀 Start Card Practice ➔
← Previous CardVanishing/Exploding GradientsNext Card →Batch Normalization

🔗 More Deep Learning Knowledge Cards

引用出处: TalentMe 450+ 工业级 AI 架构考点知识库 (Tech Vault) · 遵循 CC-BY-4.0 知识共享协议
URL: https://talentme.airsota.com/resources/tech/roadmap/mindmap/DL/card/loss-functions-dl
中文速览与面试要点

损失函数族 (Loss Functions)

定义: 损失函数族决定优化的目标几何与收敛速度。分类标准是交叉熵 CE: L=- _iy_i p_i,二分类退化为 BCE -[y y+(1-y) (1- y)];回归用 MSE: L= 1N y- y ^2。两者梯度对比是面试核心: Softmax+CE 的梯度是 p_i-y_i,线性、永不饱和;而 Softmax+MSE 的梯度是 (p_i-y_i)p_i(1-p_i),预测极端时 p(1-p) 0 令梯度消失、学习极慢。类别不平衡用 Focal Loss: FL(p_t)=- _t(1-p_t)^ p_t, =2 时易分样本的权重降到 (1-p_t)^2。对比学习用 InfoNCE: - e^ sim(q,k_+)/ _j=1^Ke^ sim(q,k_j)/ ,温度 控制负样本惩罚强度;Triplet Loss: (0,\,d(a,p)-d(a,n)+m),其中 m 为间隔。 使用场景: 面试高频“分类为什么用 CE 而回归用 MSE?”、“CE 与 MSE 在 Softmax 下的梯度差异?”、“Focal Loss 如何解决正负样本不平衡?”、“InfoNCE 的温度参数怎么调?”;目标检测 RetinaNet、对比学习 CLIP/SimCLR、多标签 BCE 都是现场延伸方向。 解决的问题: 收敛速度与鲁棒性——CE 的 与 Softmax 指数“互相抵消”得出 p-y 的线性梯度,而 MSE 在饱和区梯度被 p(1-p) 压没,同样网络下 CE 收敛可快一个数量级;Focal 用 (1-p_t)^ 压低易分样本(数量占优)的损失贡献,COCO 上 RetinaNet 是首个单阶段超越两阶段的检测器;InfoNCE 的 决定负样本“难易”权重,直接决定 CLIP/SimCLR 的表征质量。

核心要点

  • 白板推导 Softmax+CE 梯度 p_i-y_i,并对比 Softmax+MSE 的 (p_i-y_i)p_i(1-p_i): 为什么 CE 收敛更快?
  • 写出 Focal Loss FL(p_t)=- _t(1-p_t)^ p_t; =2 时易分样本(p_t 1)权重如何变化?为什么能解决类别不平衡?
  • 写出 InfoNCE 公式并解释温度 的作用;为什么 太小会让负样本梯度过强?
  • CE 与 BCE 的关系?多标签分类为什么用 BCE 而非多分类 CE?
  • 回归任务为什么不用 CE?MSE / MAE / Huber 的梯度与对离群点鲁棒性对比?