Problems
Loading...
1 / 1

Implement Contrastive Loss (Siamese)

Loss Functions
Medium

Implement Contrastive Loss for Siamese pairs. Given embeddings ai, bi ∈ ℝd and labels yi ∈ {0,1} where y=1 means "similar/positive" and y=0 means "dissimilar/negative":

Mathematical Definition

di=aibi2d_i = ||a_i - b_i||_2 i=yidi2+(1yi)max(0,mdi)2\ell_i = y_i d_i^2 + (1 - y_i) \max(0, m - d_i)^2

Return the mean loss.

Loading visualization...

Examples

Input:

a = [1., 0.], b = [1., 0.], y = [1], margin = 1.0

Output:

d=0 → loss = 1*0² + 0*max(0,1-0)² = 0.0

Input:

a = [0., 0.], b = [0.5, 0.], y = [0], margin = 1.0

Output:

d=0.5 → loss = 0*0.25 + 1*(1-0.5)² = 0.25

Input:

a = [[0.,0.],[1.,1.]], b = [[0.,0.],[2.,2.]], y = [1, 0], margin = 1.0

Output:

d = [0.0, √2] → loss = [0.0, max(0,1-1.4142)²=0.0] → mean = 0.0

Hint 1

Compute distances using np.linalg.norm(a - b, axis=1) after ensuring proper dimensions.

Hint 2

Combine positive and negative terms: y * d**2 + (1-y) * np.maximum(0, margin-d)**2.

Requirements

  • Vectorized; avoid Python loops
  • Handle (D,) vs (N,D) by broadcasting
  • Validate y ∈ {0,1}
  • Support reduction="mean" and "sum"

Constraints

  • N ≤ 1e6, D ≤ 2048
  • Use NumPy only
  • Time limit: 300 ms
Try Similar Problems