NetPairEmbeddingOperator
✖
NetPairEmbeddingOperator
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

- In NetPairEmbeddingOperator[net], net should take exactly one input and produce exactly one output.
- NetExtract can be used to extract net from a NetPairEmbeddingOperator[net] object.
- The following optional parameter can be included:
-
DistanceFunction EuclideanDistance distance function to use LearningRateMultipliers Automatic learning rate multipliers for trainable arrays in the net - The following values of DistanceFunction are supported:
-
EuclideanDistance Euclidean distance between inputs CosineDistance cosine distance between inputs - The input and output ports of the net represented by NetPairEmbeddingOperator are:
-
"Input" a pair of arrays "Output" a real number - When it cannot be inferred from other layers in a larger net, the option "Input"->{n1,n2,…} can be used to fix the input dimensions of each of the pair of input arrays of NetPairEmbeddingOperator.
- When a NetPairEmbeddingOperator is trained, NetTrain will automatically attach a ContrastiveLossLayer if a loss is not specified.
- NetExtract allows access to the forward and reverse nets via "Net".
- Options[NetPairEmbeddingOperator] gives the list of default options to construct the operator. Options[NetPairEmbeddingOperator[…]] gives the list of default options to evaluate the operator on some data.
- Information[NetPairEmbeddingOperator[…]] gives a report about the operator.
- Information[NetPairEmbeddingOperator[…],prop] gives the value of the property prop of NetPairEmbeddingOperator[…]. Possible properties are the same as for NetGraph.
Examples
open allclose allBasic Examples (2)Summary of the most common use cases
Create a NetPairEmbeddingOperator that embeds inputs using a NetChain:

https://wolfram.com/xid/0itb22k1l8pppg8sb69q2-ctvkut

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

https://wolfram.com/xid/0itb22k1l8pppg8sb69q2-bpjf7z

Apply the net to a pair of input vectors:

https://wolfram.com/xid/0itb22k1l8pppg8sb69q2-u1pshm

Thread the layer across a batch of input vectors:

https://wolfram.com/xid/0itb22k1l8pppg8sb69q2-y9irfu

Scope (1)Survey of the scope of standard use cases
Create a NetGraph with one input and output:

https://wolfram.com/xid/0itb22k1l8pppg8sb69q2-x4rdxk

Create a NetPairEmbeddingOperator that embeds vectors using a NetGraph:

https://wolfram.com/xid/0itb22k1l8pppg8sb69q2-3v7p3o

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

https://wolfram.com/xid/0itb22k1l8pppg8sb69q2-0nodq8

Options (1)Common values & functionality for each option
DistanceFunction (1)
Create a NetPairEmbeddingOperator that uses the CosineDistance function to calculate the distance between the two embedded arrays:

https://wolfram.com/xid/0itb22k1l8pppg8sb69q2-za23jm

Apply the layer to a pair of input vectors:

https://wolfram.com/xid/0itb22k1l8pppg8sb69q2-8i2650

Applications (2)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:

https://wolfram.com/xid/0itb22k1l8pppg8sb69q2-0w0qim

https://wolfram.com/xid/0itb22k1l8pppg8sb69q2-xx3zxx


https://wolfram.com/xid/0itb22k1l8pppg8sb69q2-kxylgv
Use NetPairEmbeddingOperator to define a loss network that measures the performance of the embedding:

https://wolfram.com/xid/0itb22k1l8pppg8sb69q2-gpq0g4

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:

https://wolfram.com/xid/0itb22k1l8pppg8sb69q2-n5bk4

https://wolfram.com/xid/0itb22k1l8pppg8sb69q2-nhie0i

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

https://wolfram.com/xid/0itb22k1l8pppg8sb69q2-ek98bp

Extract the embedding from the net:

https://wolfram.com/xid/0itb22k1l8pppg8sb69q2-3znx2u
Plot the 1D embedding learned by the net as a color map:

https://wolfram.com/xid/0itb22k1l8pppg8sb69q2-j6lxzb

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:

https://wolfram.com/xid/0itb22k1l8pppg8sb69q2-zdoft9
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:

https://wolfram.com/xid/0itb22k1l8pppg8sb69q2-nycr52

https://wolfram.com/xid/0itb22k1l8pppg8sb69q2-pgcdrq

https://wolfram.com/xid/0itb22k1l8pppg8sb69q2-730umu

Define a convolutional network to use as an embedding network:

https://wolfram.com/xid/0itb22k1l8pppg8sb69q2-k953ue

Construct the pair-embedding net:

https://wolfram.com/xid/0itb22k1l8pppg8sb69q2-ufuiqy


https://wolfram.com/xid/0itb22k1l8pppg8sb69q2-tmhvkk

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:

https://wolfram.com/xid/0itb22k1l8pppg8sb69q2-mk7wey

Extract the embedding network:

https://wolfram.com/xid/0itb22k1l8pppg8sb69q2-679ty0

Compute the embedding of a digit:

https://wolfram.com/xid/0itb22k1l8pppg8sb69q2-pysmgu

Sample 500 digits and group them by their labels:

https://wolfram.com/xid/0itb22k1l8pppg8sb69q2-zlofif
Compute their embeddings and plot them. Digits with the same label are clustered under the learned embedding:

https://wolfram.com/xid/0itb22k1l8pppg8sb69q2-ftweak

https://wolfram.com/xid/0itb22k1l8pppg8sb69q2-btvncb

Properties & Relations (1)Properties of the function, and connections to other functions
NetMapOperator can be used to implement NetPairEmbeddingOperator. Create an initialized LinearLayer:

https://wolfram.com/xid/0itb22k1l8pppg8sb69q2-4a82u2

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

https://wolfram.com/xid/0itb22k1l8pppg8sb69q2-hfdlob


https://wolfram.com/xid/0itb22k1l8pppg8sb69q2-iiunif

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

https://wolfram.com/xid/0itb22k1l8pppg8sb69q2-9lffv5


https://wolfram.com/xid/0itb22k1l8pppg8sb69q2-pg1cnj

Possible Issues (1)Common pitfalls and unexpected behavior
Nets with multiple inputs or outputs cannot be used as the embedding network in NetPairEmbeddingOperator[net]:

https://wolfram.com/xid/0itb22k1l8pppg8sb69q2-gh3l20


Wolfram Research (2017), NetPairEmbeddingOperator, Wolfram Language function, https://reference.wolfram.com/language/ref/NetPairEmbeddingOperator.html (updated 2020).
Text
Wolfram Research (2017), NetPairEmbeddingOperator, Wolfram Language function, https://reference.wolfram.com/language/ref/NetPairEmbeddingOperator.html (updated 2020).
Wolfram Research (2017), NetPairEmbeddingOperator, Wolfram Language function, https://reference.wolfram.com/language/ref/NetPairEmbeddingOperator.html (updated 2020).
CMS
Wolfram Language. 2017. "NetPairEmbeddingOperator." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2020. https://reference.wolfram.com/language/ref/NetPairEmbeddingOperator.html.
Wolfram Language. 2017. "NetPairEmbeddingOperator." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2020. https://reference.wolfram.com/language/ref/NetPairEmbeddingOperator.html.
APA
Wolfram Language. (2017). NetPairEmbeddingOperator. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/NetPairEmbeddingOperator.html
Wolfram Language. (2017). NetPairEmbeddingOperator. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/NetPairEmbeddingOperator.html
BibTeX
@misc{reference.wolfram_2025_netpairembeddingoperator, author="Wolfram Research", title="{NetPairEmbeddingOperator}", year="2020", howpublished="\url{https://reference.wolfram.com/language/ref/NetPairEmbeddingOperator.html}", note=[Accessed: 08-June-2025
]}
BibLaTeX
@online{reference.wolfram_2025_netpairembeddingoperator, organization={Wolfram Research}, title={NetPairEmbeddingOperator}, year={2020}, url={https://reference.wolfram.com/language/ref/NetPairEmbeddingOperator.html}, note=[Accessed: 08-June-2025
]}