TensorTonicTensorTonic
Problems
Study PlansProjectsNewInterviewPricingFeedback
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)=x1⋅x2∥x1∥⋅∥x2∥\cos(x_1, x_2) = \frac{x_1 \cdot x_2}{\|x_1\| \cdot \|x_2\|}cos(x1​,x2​)=∥x1​∥⋅∥x2​∥x1​⋅x2​​

Then compute the loss based on the label:

L=1−cos⁡(x1,x2)if label=1L = 1 - \cos(x_1, x_2) \quad \text{if label} = 1L=1−cos(x1​,x2​)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} = -1L=max(0,cos(x1​,x2​)−margin)if label=−1

Return the cosine embedding loss as a float.

Loading visualization...

Examples

Input: x1 = [1, 0, 0], x2 = [1, 0, 0], label = 1, margin = 0

Output: 0.0

Explanation: Identical vectors have cosine similarity 1, so the similar-pair loss is 0.

Input: x1 = [1, 0, 0], x2 = [0, 1, 0], label = 1, margin = 0

Output: 1.0

Hint 1

Use zip with sum to compute the dot product and each squared norm.

Hint 2

Choose the loss branch from label after computing cosine similarity.

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
Contrastive LossCosine SimilarityTriplet LossInfo Nce LossCross Entropy Loss

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

Accepts: array

Accepts: number

Accepts: number

You must run your code first.
PrevNext

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)=x1⋅x2∥x1∥⋅∥x2∥\cos(x_1, x_2) = \frac{x_1 \cdot x_2}{\|x_1\| \cdot \|x_2\|}cos(x1​,x2​)=∥x1​∥⋅∥x2​∥x1​⋅x2​​

Then compute the loss based on the label:

L=1−cos⁡(x1,x2)if label=1L = 1 - \cos(x_1, x_2) \quad \text{if label} = 1L=1−cos(x1​,x2​)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} = -1L=max(0,cos(x1​,x2​)−margin)if label=−1

Return the cosine embedding loss as a float.

Loading visualization...

Examples

Input: x1 = [1, 0, 0], x2 = [1, 0, 0], label = 1, margin = 0

Output: 0.0

Explanation: Identical vectors have cosine similarity 1, so the similar-pair loss is 0.

Input: x1 = [1, 0, 0], x2 = [0, 1, 0], label = 1, margin = 0

Output: 1.0

Hint 1

Use zip with sum to compute the dot product and each squared norm.

Hint 2

Choose the loss branch from label after computing cosine similarity.

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
Contrastive LossCosine SimilarityTriplet LossInfo Nce LossCross Entropy Loss

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

Accepts: array

Accepts: number

Accepts: number

You must run your code first.