E6EasyPart E · Loss Functions Handbook
Triplet Margin Loss with Euclidean Distance
Metric-learning loss pulling Anchor-Positive closer than Anchor-Negative by at least a distance margin.
⏱️ Time:
O(B * D)💾 Space:
O(B)🎯 Interactive Self-Practice Loop (Attempt Before Peeking)
Practice in 4 steps: recall mechanism, complete core lines, check edge cases, and log mastery locally.
Step 1 · Mechanism Recall & Stability Self-Check
Step 2 · Complete Key Implementation Lines
import numpy as np
def triplet_margin_loss(
anchor: np.ndarray, # (B, D)
positive: np.ndarray, # (B, D)
negative: np.ndarray, # (B, D)
margin: float = 1.0,
p: int = 2,
reduction: str = "mean"
) -> float:
# 1. 计算欧氏距离范数
d_pos =
d_neg = np.linalg.norm(anchor - negative, ord=p, axis=-1)
# 2. 折叶边际损失: max(0, d_pos - d_neg + margin)
losses = np.maximum(0.0, d_pos - d_neg + margin)
if reduction == "mean":
return
return losses
Step 3 · Verify Boundary Cases & Assertions
Step 4 · Follow-Up Drill & Review Outcome
Q1: What are semi-hard negatives, and why do they stabilize Triplet Loss training?
💡
Core Mental Anchor / Mnemonic
Hinge loss max(0, d(a, p) - d(a, n) + margin) enforces separation until margin holds.
📐 Mathematical Derivation & Core Formula
### Metric Manifold Shaping with Margin
Triplet Loss pulls an Anchor closer to a same-class Positive than to a different-class Negative by at least margin :
Once the margin is satisfied, hinge truncation sets loss and gradient to zero.
Triplet Loss pulls an Anchor closer to a same-class Positive than to a different-class Negative by at least margin :
Once the margin is satisfied, hinge truncation sets loss and gradient to zero.
🔄 Tensor Dimensions & Shape Flow
anchor, pos, neg: (B, D) -> d_pos, d_neg: (B,) -> max(0, d_pos - d_neg + margin) -> mean scalar
🛡️ Numerical Stability & Pitfalls Checklist
- Add tiny epsilon inside sqrt or clamp distances safely at zero to keep Euclidean norm gradients finite.
- Pair with semi-hard or hard negative mining so gradients do not vanish early.
💻 Reference Code Implementation
import numpy as np
def triplet_margin_loss(
anchor: np.ndarray, # (B, D)
positive: np.ndarray, # (B, D)
negative: np.ndarray, # (B, D)
margin: float = 1.0,
p: int = 2,
reduction: str = "mean"
) -> float:
# 1. 计算欧氏距离范数
d_pos = np.linalg.norm(anchor - positive, ord=p, axis=-1)
d_neg = np.linalg.norm(anchor - negative, ord=p, axis=-1)
# 2. 折叶边际损失: max(0, d_pos - d_neg + margin)
losses = np.maximum(0.0, d_pos - d_neg + margin)
if reduction == "mean":
return float(np.mean(losses))
return losses
🧪 Runnable Assertions & Validation
Copy and run directly in Python / Jupyter:
import numpy as np
a = np.array([[0.0, 0.0]])
p = np.array([[0.0, 1.0]]) # 距离 1
n = np.array([[0.0, 3.0]]) # 距离 3
loss = triplet_margin_loss(a, p, n, margin=1.0)
# d_pos - d_neg + margin = 1 - 3 + 1 = -1 < 0 -> loss = 0
assert loss == 0.0
# 若 n 太近 (距离 1.5)
n_close = np.array([[0.0, 1.5]])
loss_close = triplet_margin_loss(a, p, n_close, margin=1.0)
# 1 - 1.5 + 1 = 0.5
assert np.isclose(loss_close, 0.5)
print("✓ Triplet Margin 损失自测通过")🎯 Core Follow-up Inquiries & Reference Answers
Q1:What are semi-hard negatives, and why do they stabilize Triplet Loss training?
Semi-hard negatives satisfy : already farther than the positive, yet inside the margin. Mining purely hardest negatives early often collapses embeddings to a single point, whereas semi-hard negatives give steady, stable gradients.
⚡
Test muscle memory on this topic via 3-Layer Flashcards
Active Recall: Mentally formulate Prompt ➔ Math Derivation ➔ Benchmark Implementation in 3 minutes.