SiameseNeuralNetwork<T>

Models & Types · AiDotNet.NeuralNetworks

Sentence-BERT (SBERT) style shared sentence-encoder tower: a transformer encoder that maps a tokenized input to a fixed-size embedding (default 768-d, BERT vocab 30522, max length 512) for semantic similarity and retrieval.

Inheritance: NeuralNetworkBase<T> → DeclaredModelLayoutBase<T> → VectorModelLayoutBase<T> → SiameseNeuralNetwork<T>

Implements: IEmbeddingModel<T>

For Beginners

A Siamese Network is like having two identical twins who think exactly the same way. You give a different photo to each twin, and they each describe what they see using a list of numbers. Because the twins think the same way, if the photos are similar, their descriptions will be almost identical. This is the most popular way to build face recognition or “find similar” search systems.

How It Works

This implements the SHARED ENCODER of a Siamese/dual-encoder setup (Reimers & Gurevych 2019, “Sentence-BERT: Sentence Embeddings using Siamese BERT-Networks”): the same encoder is applied independently to each sentence, and the resulting embeddings are compared by a distance/cosine metric. Predict returns the encoder embedding for one input; pair training (contrastive/triplet) compares two such embeddings.

NOTE: this is distinct from SiameseNetwork, which is the pair-in / similarity-score-out VERIFICATION network of Koch et al. 2015 (twin subnetwork + L1-distance sigmoid head). This class is the sentence-embedding ENCODER tower; that class is the end-to-end pair verifier.

Example

var architecture = new NeuralNetworkArchitecture<float>(inputFeatures: 8, outputSize: 4);
var input = Tensor<float>.CreateRandom(new[] { 1, 784 });
var trainX = Tensor<float>.CreateRandom(4, 8);
var trainY = Tensor<float>.CreateRandom(4, 2);
var result = new AiModelBuilder<float, Tensor<float>, Tensor<float>>()
    .ConfigureModel(new SiameseNeuralNetwork<float>(architecture))
    .Build(trainX, trainY);
var embedding = result.Predict(input);

Constructors

Constructor Summary
SiameseNeuralNetwork() Initializes a new instance of the SiameseNeuralNetwork with default configuration.
SiameseNeuralNetwork(NeuralNetworkArchitecture<T> architecture, ITokenizer tokenizer, IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>> optimizer, ILossFunction<T> lossFunction, SiameseNeuralNetworkOptions options) Initializes a new instance of the SiameseNeuralNetwork model.

Properties

Property Summary
int EmbeddingDimension { get; }
int MaxTokens { get; }

Methods

Method Summary
Matrix<T> EmbedBatch(IEnumerable<string> texts) Encodes a batch of strings into a matrix of embedding vectors.
ModelMetadata<T> GetModelMetadata() Retrieves metadata about the Siamese dual-encoder model.
Task<Matrix<T>> EmbedBatchAsync(IEnumerable<string> texts) Asynchronously encodes a batch of strings into a matrix of embedding vectors.
Task<Vector<T>> EmbedAsync(string text) Asynchronously encodes a single string into a normalized embedding vector.
Tensor<T> Forward(Tensor<T> input) Performs a forward pass through the shared encoder.
Vector<T> Embed(string text) Encodes a single string into a normalized embedding vector using the shared encoder brain.