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.
First compute the cosine similarity:
cos(x1,x2)=∥x1∥⋅∥x2∥x1⋅x2Then compute the loss based on the label:
L=1−cos(x1,x2)if label=1 L=max(0,cos(x1,x2)−margin)if label=−1Return the cosine embedding loss as a float.
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
Use zip with sum to compute the dot product and each squared norm.
Choose the loss branch from label after computing cosine similarity.
Accepts: array
Accepts: array
Accepts: number
Accepts: number
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.
First compute the cosine similarity:
cos(x1,x2)=∥x1∥⋅∥x2∥x1⋅x2Then compute the loss based on the label:
L=1−cos(x1,x2)if label=1 L=max(0,cos(x1,x2)−margin)if label=−1Return the cosine embedding loss as a float.
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
Use zip with sum to compute the dot product and each squared norm.
Choose the loss branch from label after computing cosine similarity.
Accepts: array
Accepts: array
Accepts: number
Accepts: number