Implement Contrastive Loss (Siamese)
Implement Contrastive Loss (Siamese)
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=∣∣ai−bi∣∣2 ℓi=yidi2+(1−yi)max(0,m−di)2Return the mean loss.
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
Log in to take notes on this problem
Accepts: array
Accepts: array
Accepts: array
Accepts: number
Accepts: string
Implement Contrastive Loss (Siamese)
Implement Contrastive Loss (Siamese)
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=∣∣ai−bi∣∣2 ℓi=yidi2+(1−yi)max(0,m−di)2Return the mean loss.
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
Log in to take notes on this problem
Accepts: array
Accepts: array
Accepts: array
Accepts: number
Accepts: string