represents a net that takes a pair of arrays, embeds them into a vector space using net, and outputs the distance under the embedding.


includes options for distance function to use and other parameters.

Details and Options


open allclose all

Basic Examples  (2)

Create a NetPairEmbeddingOperator that embeds inputs using a NetChain:

Create a NetPairEmbeddingOperator that embeds a pair of length-2 vectors using a randomly initialized NetChain:

Apply the net to a pair of input vectors:

Thread the layer across a batch of input vectors:

Scope  (1)

Create a NetGraph with one input and output:

Create a NetPairEmbeddingOperator that embeds vectors using a NetGraph:

The embedding network in NetPairEmbeddingOperator[net] can be extracted using NetExtract:

Options  (1)

DistanceFunction  (1)

Create a NetPairEmbeddingOperator that uses the CosineDistance function to calculate the distance between the two embedded arrays:

Apply the layer to a pair of input vectors:

Applications  (2)

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:

Use NetPairEmbeddingOperator to define a loss network that measures 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:

Learn an embedding of the digits in the MNIST dataset. First, import the data and take only those examples with labels between 0 and 4:

Create a training set by sampling pairs of images and associating them with True if their labels are different and False if their labels are the same:

Define a convolutional network to use as an embedding network:

Construct the pair-embedding net:

Train the network:

Apply the network to a list of pairs of digits to compute their distances under the embedding. Digits with the same label have small distances:

Extract the embedding network:

Compute the embedding of a digit:

Sample 500 digits and group them by their labels:

Compute their embeddings and plot them. Digits with the same label are clustered under the learned embedding:

Properties & Relations  (1)

NetMapOperator can be used to implement NetPairEmbeddingOperator. Create an initialized LinearLayer:

Create NetPairEmbeddingOperator using the linear layer as its embedding net, and evaluate it on an input:

Define a NetGraph with a NetMapOperator that is equivalent to the previous NetPairEmbeddingOperator, and evaluate it on the same input:

Possible Issues  (1)

Nets with multiple inputs or outputs cannot be used as the embedding network in NetPairEmbeddingOperator[net]:

Wolfram Research (2017), NetPairEmbeddingOperator, Wolfram Language function, (updated 2020).


Wolfram Research (2017), NetPairEmbeddingOperator, Wolfram Language function, (updated 2020).


@misc{reference.wolfram_2021_netpairembeddingoperator, author="Wolfram Research", title="{NetPairEmbeddingOperator}", year="2020", howpublished="\url{}", note=[Accessed: 22-June-2021 ]}


@online{reference.wolfram_2021_netpairembeddingoperator, organization={Wolfram Research}, title={NetPairEmbeddingOperator}, year={2020}, url={}, note=[Accessed: 22-June-2021 ]}


Wolfram Language. 2017. "NetPairEmbeddingOperator." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2020.


Wolfram Language. (2017). NetPairEmbeddingOperator. Wolfram Language & System Documentation Center. Retrieved from