WOLFRAM

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

Examples

open allclose all

Basic Examples  (2)Summary of the most common use cases

Create a ContrastiveLossLayer with a given margin:

Out[1]=1

Create a ContrastiveLossLayer:

Out[1]=1

Apply it to some data:

Out[2]=2

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

Out[3]=3
Out[3]=3

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

Out[4]=4

Applications  (1)Sample problems that can be solved with this function

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:

Out[2]=2

Create the perceptron:

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

Out[4]=4

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:

Out[6]=6

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

Out[7]=7

Extract the embedding from the net:

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

Out[9]=9
Wolfram Research (2017), ContrastiveLossLayer, Wolfram Language function, https://reference.wolfram.com/language/ref/ContrastiveLossLayer.html.
Wolfram Research (2017), ContrastiveLossLayer, Wolfram Language function, https://reference.wolfram.com/language/ref/ContrastiveLossLayer.html.

Text

Wolfram Research (2017), ContrastiveLossLayer, Wolfram Language function, https://reference.wolfram.com/language/ref/ContrastiveLossLayer.html.

Wolfram Research (2017), ContrastiveLossLayer, Wolfram Language function, https://reference.wolfram.com/language/ref/ContrastiveLossLayer.html.

CMS

Wolfram Language. 2017. "ContrastiveLossLayer." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/ContrastiveLossLayer.html.

Wolfram Language. 2017. "ContrastiveLossLayer." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/ContrastiveLossLayer.html.

APA

Wolfram Language. (2017). ContrastiveLossLayer. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/ContrastiveLossLayer.html

Wolfram Language. (2017). ContrastiveLossLayer. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/ContrastiveLossLayer.html

BibTeX

@misc{reference.wolfram_2025_contrastivelosslayer, author="Wolfram Research", title="{ContrastiveLossLayer}", year="2017", howpublished="\url{https://reference.wolfram.com/language/ref/ContrastiveLossLayer.html}", note=[Accessed: 23-April-2025 ]}

@misc{reference.wolfram_2025_contrastivelosslayer, author="Wolfram Research", title="{ContrastiveLossLayer}", year="2017", howpublished="\url{https://reference.wolfram.com/language/ref/ContrastiveLossLayer.html}", note=[Accessed: 23-April-2025 ]}

BibLaTeX

@online{reference.wolfram_2025_contrastivelosslayer, organization={Wolfram Research}, title={ContrastiveLossLayer}, year={2017}, url={https://reference.wolfram.com/language/ref/ContrastiveLossLayer.html}, note=[Accessed: 23-April-2025 ]}

@online{reference.wolfram_2025_contrastivelosslayer, organization={Wolfram Research}, title={ContrastiveLossLayer}, year={2017}, url={https://reference.wolfram.com/language/ref/ContrastiveLossLayer.html}, note=[Accessed: 23-April-2025 ]}