ContrastiveLossLayer

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.
  • 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 allclose all

Basic Examples  (2)

Create a ContrastiveLossLayer with a given margin:

In[1]:=
Click for copyable input
Out[1]=

Create a ContrastiveLossLayer:

In[1]:=
Click for copyable input
Out[1]=

Apply it to some data:

In[2]:=
Click for copyable input
Out[2]=

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

In[3]:=
Click for copyable input
Out[3]=
Out[3]=

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

In[4]:=
Click for copyable input
Out[4]=

Applications  (1)

See Also

NetPairEmbeddingOperator  MeanAbsoluteLossLayer  CrossEntropyLossLayer  NetGraph  NetTrain

Introduced in 2017
(11.1)