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

Implement Triplet Loss

Loss Functions
Medium

Compute triplet loss using squared Euclidean distance:

d(x,y)=∑j=1D(xj−yj)2d(x,y) = \sum_{j=1}^{D}(x_j-y_j)^2d(x,y)=j=1∑D​(xj​−yj​)2

For each anchor, positive, and negative triplet:

Li=max⁡(0,d(ai,pi)−d(ai,ni)+m)L_i = \max\left(0, d(a_i,p_i)-d(a_i,n_i)+m\right)Li​=max(0,d(ai​,pi​)−d(ai​,ni​)+m)

Here, DDD is embedding width and mmm is margin. Support one triplet with shape (D,)(D,)(D,) or a batch with shape (N,D)(N,D)(N,D). Return the mean loss as a Python float.

Loading visualization...

Examples

Input: anchor = [[1, 0]], positive = [[2, 0]], negative = [[5, 0]], margin = 1.0

Output: 0.0

Explanation: The positive squared distance is 1 and the negative squared distance is 16, so the margin is already satisfied.

Input: anchor = [[0, 0]], positive = [[3, 0]], negative = [[1, 0]], margin = 1.0

Output: 9.0

Input: anchor = [1, 0], positive = [2, 0], negative = [5, 0], margin = 1.0

Output: 0.0

Hint 1

Use np.atleast_2d so one vector and a batch share the same computation.

Hint 2

Sum squared coordinate differences along axis=1.

Hint 3

Apply np.maximum(0.0, positive_distance - negative_distance + margin).

Requirements

  • Use squared Euclidean distance
  • Support one triplet or a batch of triplets
  • Compute the margin loss for each triplet
  • Return the mean as a Python float

Constraints

  • anchor, positive, and negative have matching numeric shapes
  • Inputs have shape (D,) or (N,D)
  • margin is nonnegative
  • Use NumPy only
Try Similar Problems
Contrastive LossCosine Embedding LossInfo Nce LossEuclidean DistanceCosine Similarity

Sign in to take notes on this problem

Case 1
Case 2
Case 3

Accepts: array

Accepts: array

Accepts: array

Accepts: number

You must run your code first.
PrevNext

Implement Triplet Loss

Loss Functions
Medium

Compute triplet loss using squared Euclidean distance:

d(x,y)=∑j=1D(xj−yj)2d(x,y) = \sum_{j=1}^{D}(x_j-y_j)^2d(x,y)=j=1∑D​(xj​−yj​)2

For each anchor, positive, and negative triplet:

Li=max⁡(0,d(ai,pi)−d(ai,ni)+m)L_i = \max\left(0, d(a_i,p_i)-d(a_i,n_i)+m\right)Li​=max(0,d(ai​,pi​)−d(ai​,ni​)+m)

Here, DDD is embedding width and mmm is margin. Support one triplet with shape (D,)(D,)(D,) or a batch with shape (N,D)(N,D)(N,D). Return the mean loss as a Python float.

Loading visualization...

Examples

Input: anchor = [[1, 0]], positive = [[2, 0]], negative = [[5, 0]], margin = 1.0

Output: 0.0

Explanation: The positive squared distance is 1 and the negative squared distance is 16, so the margin is already satisfied.

Input: anchor = [[0, 0]], positive = [[3, 0]], negative = [[1, 0]], margin = 1.0

Output: 9.0

Input: anchor = [1, 0], positive = [2, 0], negative = [5, 0], margin = 1.0

Output: 0.0

Hint 1

Use np.atleast_2d so one vector and a batch share the same computation.

Hint 2

Sum squared coordinate differences along axis=1.

Hint 3

Apply np.maximum(0.0, positive_distance - negative_distance + margin).

Requirements

  • Use squared Euclidean distance
  • Support one triplet or a batch of triplets
  • Compute the margin loss for each triplet
  • Return the mean as a Python float

Constraints

  • anchor, positive, and negative have matching numeric shapes
  • Inputs have shape (D,) or (N,D)
  • margin is nonnegative
  • Use NumPy only
Try Similar Problems
Contrastive LossCosine Embedding LossInfo Nce LossEuclidean DistanceCosine Similarity

Sign in to take notes on this problem

Case 1
Case 2
Case 3

Accepts: array

Accepts: array

Accepts: array

Accepts: number

You must run your code first.