Contrastive Loss is a loss function that learns a mapping from pairs of data points and "similar or dissimilar" labels, placing similar pairs close together and dissimilar pairs far apart. Hadsell, Chopra, and LeCun (2006) proposed it as part of DrLIM, a dimensionality reduction method that maps high-dimensional data to a low-dimensional space.
A pair is passed through the same network , and the output distance is . The label is for a similar pair and for a dissimilar pair. The loss is
where is the margin. As the figure at the top shows, similar pairs (dashed line) incur a loss until their distance reaches 0, while dissimilar pairs (solid line) incur a loss only when their distance is less than .
The paper points out two weaknesses in earlier dimensionality reduction methods. First, they assume that a meaningful distance can be computed in the input space. Second, they do not provide a function that maps new data not used in training. DrLIM, which uses Contrastive Loss, learns only from neighborhood relations, that is, which data points are similar to which, and the trained network maps new data directly. The neighborhood relations may come from information unrelated to input distances, such as prior knowledge, manual labels, or closeness in capture time.

