Problems
Loading...
1 / 1

Cosine Embedding Loss

Loss Functions
Easy

Cosine embedding loss measures whether two vectors are similar or dissimilar based on a label. It is commonly used in metric learning and siamese networks to learn embeddings where similar items are close and dissimilar items are far apart in cosine space.

Given two vectors, a label (+1 for similar, -1 for dissimilar), and a margin, compute the cosine embedding loss.

Formula

First compute the cosine similarity:

cos(x1,x2)=x1x2x1x2\cos(x_1, x_2) = \frac{x_1 \cdot x_2}{\|x_1\| \cdot \|x_2\|}

Then compute the loss based on the label:

L=1cos(x1,x2)if label=1L = 1 - \cos(x_1, x_2) \quad \text{if label} = 1 L=max(0,cos(x1,x2)margin)if label=1L = \max(0, \cos(x_1, x_2) - \text{margin}) \quad \text{if label} = -1
Loading visualization...

Examples

Input:

x1 = [1, 0, 0], x2 = [1, 0, 0], label = 1, margin = 0.0

Output:

0.0

Identical vectors have cosine similarity = 1. For similar pairs (label = 1), loss = 1 - 1 = 0.

Input:

x1 = [1, 0, 0], x2 = [0, 1, 0], label = 1, margin = 0.0

Output:

1.0

Orthogonal vectors have cosine similarity = 0. For similar pairs, loss = 1 - 0 = 1. The model is penalized for not making similar items close.

Hint 1

Compute the dot product with sum(ab for a,b in zip(x1,x2)). Compute norms with math.sqrt(sum(aa for a in x)). Divide dot product by the product of norms to get cosine similarity.

Hint 2

For label = -1 with margin, use max(0, cos_sim - margin). If cos_sim is already below the margin, the loss is 0 and no further learning signal is given.

Requirements

  • Compute cosine similarity as dot product divided by the product of norms
  • For label = 1 (similar): return 1 - cosine_similarity
  • For label = -1 (dissimilar): return max(0, cosine_similarity - margin)
  • Return a single float

Constraints

  • Both vectors have the same length and at least one element
  • Vectors are non-zero (norms > 0)
  • label is either 1 or -1
  • margin >= 0
  • Return a single float
  • Time limit: 300 ms
Try Similar Problems