ContrastiveLossLayer
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.
ContrastiveLossLayer[margin]
specifies a distance above which the loss is zero for True targets.
Details and Options

- ContrastiveLossLayer is typically used in conjunction with NetPairEmbeddingOperator in order to learn an embedding from an input into a vector space, such that similar inputs cluster together in the vector space and dissimilar inputs are separated.
- ContrastiveLossLayer exposes the following ports for use in NetGraph etc.:
-
"Input" a real number representing a distance "Target" True if the distance should be maximized, False if it should be minimized "Loss" a real number - ContrastiveLossLayer[margin] computes the following loss:
- ContrastiveLossLayer[…][<"Input"in,"Target"target >] explicitly computes the loss from applying the layer.
- ContrastiveLossLayer[…][<"Input"{in1,in2,…},"Target"{target1,target2,…} >] explicitly computes losses for each of the ini and targeti.
- When given a NumericArray as input, the output will be a NumericArray.
- ContrastiveLossLayer is typically used inside NetGraph to construct a training network for a learned embedding.
- A ContrastiveLossLayer[…] can be provided as the third argument to NetTrain when training a specific network.
- When appropriate, ContrastiveLossLayer is automatically used by NetTrain if an explicit loss specification is not provided.

Examples
open all close allBasic Examples (2)
Create a ContrastiveLossLayer with a given margin:
Create a ContrastiveLossLayer:
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)
Introduced in 2017
(11.1)