represents a loss layer that computes a loss based on a distance metric and a target that specifies whether the distance should be minimized or maximized.


specifies a distance above which the loss is zero for True targets.

Details and Options


open allclose all

Basic Examples  (2)

Create a ContrastiveLossLayer with a given margin:

Create a ContrastiveLossLayer:

Apply it to some data:

If the target is True, the loss is nonzero only when the input distance is less than the default margin of 0.5:

If the target is False, the loss is proportional to the input distance:

Applications  (1)

Train a multilayer perceptron to embed a synthetic dataset based only on its topology. First, create the training data on a spiral-like manifold that is dense in the plane:

Create the perceptron:

Create a loss network to measure the performance of the embedding:

Create a generator that will sample pairs of points and associate them with True if their parameterization on the manifold differs by more than Pi:

Train the network, using a generator to sample pairs of points, and classify them as the same if their original parameterization was close:

Extract the embedding from the net:

Plot the 1D embedding learned by the net as a color map:

Introduced in 2017