Contrastive Loss for Siamese Networks

medium
Unit 12 · Mixed Precision & Metric Learning0/3 to milestone

A Siamese network embeds two inputs and learns that similar pairs are close and dissimilar pairs are at least a margin mm apart (Hadsell et al.):

L=1N∑i12[yi di2+(1−yi)max⁡(0, m−di)2]L = \frac{1}{N}\sum_i \tfrac12\left[y_i\, d_i^2 + (1 - y_i)\max(0,\ m - d_i)^2\right]

where di=∥ai−bi∥d_i = \|a_i - b_i\| and yi=1y_i = 1 for similar pairs. Implement contrastive_loss(A, B, y, margin=1.0) and contrastive_grad(A, B, y, margin=1.0) returning the gradients (dA, dB) of the mean loss.

#metric-learning#losses#embeddings2 visible + 1 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Contrastive Loss for Siamese Networks · TechGradify