Cosine Embedding Loss: Different Enough Is Enough

Loss FunctionsEasy+10 XP+10

Cosine Embedding Loss: Different Enough Is Enough

When you train a model to produce embeddings, you don't care about the exact numbers — you care that similar things point the same way and different things don't. Cosine embedding loss says exactly that, and says it differently depending on whether the pair is meant to match.

Start with cosine similarity, which ignores length entirely and measures only direction:

cos⁡(a,b)=a⋅b∥a∥ ∥b∥\cos(a, b) = \frac{a \cdot b}{\|a\| \, \|b\|}cos(a,b)=∥a∥∥b∥a⋅b​

Then:

  • label = 1 (the pair should match): loss = 1 - cos(a, b). Zero when they point the same way, growing as they drift apart.
  • label = -1 (the pair should not match): loss = max(0, cos(a, b) - margin). Once their similarity has dropped below margin, the loss is 0 and the pair is left alone.

Task: write cosine_embedding_loss(a, b, label, margin) returning the loss, rounded to 4 decimal places.

  • a and b are vectors of the same length, and neither is all zeros.
  • label is either 1 or -1.
  • The loss is never negative.

That max(0, ...) is the interesting half. Without it, the model would be rewarded for shoving unrelated pairs ever further apart forever, burning capacity on pairs that were already fine. The margin says "different enough is enough" and stops the pushing.

Examples

a = [1, 0]
b = [1, 0]
label = 1
margin = 0.0
returns 0.0
a = [1, 1]
b = [1, 0]
label = -1
margin = 0.5
returns 0.2071

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run