TensorTonicTensorTonic
Problems
Study PlansProjectsNewInterviewPricingFeedback
Problems
Loading...
1 / 1

Implement Contrastive Loss (Siamese)

Loss Functions
Medium

Compute contrastive loss for pairs of embeddings. A label of 1 marks a similar pair, while 0 marks a dissimilar pair. First compute each Euclidean distance:

di=∥ai−bi∥2d_i = \lVert a_i-b_i \rVert_2di​=∥ai​−bi​∥2​

Then compute each pair loss:

ℓi=yidi2+(1−yi)max⁡(0,m−di)2\ell_i = y_id_i^2 + (1-y_i)\max(0,m-d_i)^2ℓi​=yi​di2​+(1−yi​)max(0,m−di​)2

Here, aia_iai​ and bib_ibi​ are the two embeddings, did_idi​ is their distance, yiy_iyi​ is the pair label, mmm is the margin, and ℓi\ell_iℓi​ is the pair loss. A one-dimensional embedding represents one pair; a two-dimensional input contains one pair per row. Return the mean or sum as a Python float according to reduction.

Loading visualization...

Examples

Input: a = [1.0, 0.0], b = [1.0, 0.0], y = [1], margin = 1.0, reduction = "mean"

Output: 0.0

Explanation: The similar embeddings have zero distance, so their squared-distance loss is zero.

Input: a = [0.0, 0.0], b = [0.5, 0.0], y = [0], margin = 1.0, reduction = "mean"

Output: 0.25

Input: a = [[0.0, 0.0], [1.0, 1.0]], b = [[0.0, 0.0], [2.0, 2.0]], y = [1, 0], margin = 1.0, reduction = "mean"

Output: 0.0

Hint 1

Convert a one-dimensional difference to shape (1, D) before reducing over axis 1.

Hint 2

Use np.linalg.norm(a - b, axis=1) for the pairwise distances.

Hint 3

Build the positive and negative terms separately before applying the reduction.

Requirements

  • Treat one-dimensional a and b as a single embedding pair
  • Compute one Euclidean distance per pair
  • Support mean and sum reduction
  • Return a Python float

Constraints

  • a and b have the same shape, either (D,)(D,)(D,) or (N,D)(N,D)(N,D)
  • y contains one label per pair, and every label is 0 or 1
  • margin is positive and reduction is mean or sum
  • Use NumPy only
Try Similar Problems
Triplet LossCosine Embedding LossInfo Nce LossCross Entropy LossCosine Similarity

Sign in to take notes on this problem

Case 1
Case 2
Case 3

Accepts: array

Accepts: array

Accepts: array

Accepts: number

Accepts: string

You must run your code first.
PrevNext

Implement Contrastive Loss (Siamese)

Loss Functions
Medium

Compute contrastive loss for pairs of embeddings. A label of 1 marks a similar pair, while 0 marks a dissimilar pair. First compute each Euclidean distance:

di=∥ai−bi∥2d_i = \lVert a_i-b_i \rVert_2di​=∥ai​−bi​∥2​

Then compute each pair loss:

ℓi=yidi2+(1−yi)max⁡(0,m−di)2\ell_i = y_id_i^2 + (1-y_i)\max(0,m-d_i)^2ℓi​=yi​di2​+(1−yi​)max(0,m−di​)2

Here, aia_iai​ and bib_ibi​ are the two embeddings, did_idi​ is their distance, yiy_iyi​ is the pair label, mmm is the margin, and ℓi\ell_iℓi​ is the pair loss. A one-dimensional embedding represents one pair; a two-dimensional input contains one pair per row. Return the mean or sum as a Python float according to reduction.

Loading visualization...

Examples

Input: a = [1.0, 0.0], b = [1.0, 0.0], y = [1], margin = 1.0, reduction = "mean"

Output: 0.0

Explanation: The similar embeddings have zero distance, so their squared-distance loss is zero.

Input: a = [0.0, 0.0], b = [0.5, 0.0], y = [0], margin = 1.0, reduction = "mean"

Output: 0.25

Input: a = [[0.0, 0.0], [1.0, 1.0]], b = [[0.0, 0.0], [2.0, 2.0]], y = [1, 0], margin = 1.0, reduction = "mean"

Output: 0.0

Hint 1

Convert a one-dimensional difference to shape (1, D) before reducing over axis 1.

Hint 2

Use np.linalg.norm(a - b, axis=1) for the pairwise distances.

Hint 3

Build the positive and negative terms separately before applying the reduction.

Requirements

  • Treat one-dimensional a and b as a single embedding pair
  • Compute one Euclidean distance per pair
  • Support mean and sum reduction
  • Return a Python float

Constraints

  • a and b have the same shape, either (D,)(D,)(D,) or (N,D)(N,D)(N,D)
  • y contains one label per pair, and every label is 0 or 1
  • margin is positive and reduction is mean or sum
  • Use NumPy only
Try Similar Problems
Triplet LossCosine Embedding LossInfo Nce LossCross Entropy LossCosine Similarity

Sign in to take notes on this problem

Case 1
Case 2
Case 3

Accepts: array

Accepts: array

Accepts: array

Accepts: number

Accepts: string

You must run your code first.