LinearEmbeddingModel

Models & Types · AiDotNet.MetaLearning.Models

A linear embedding model mapping each row of a Matrix input to one embedding row: h_r = W x_r + b.

Inheritance: ModelBase<double, Matrix<double>, Tensor<double>> → LinearEmbeddingModel

Implements: ICloneable, IGeneratedParameterRegistrar<double>

For Beginners

Where LinearVectorModel turns each example into one number, this model turns each example into a short list of numbers (an embedding). Few-shot classifiers compare examples by their embeddings, so they need this richer output.

How It Works

The per-example counterpart of LinearVectorModel. Classifier and metric meta-learners - prototypical, matching and relation networks, ANIL’s body under its head - consume one embedding per example, which a model emitting one scalar per row cannot supply. The output is [rows, embeddingDim].

Its gradient is exact for any loss. dL/dh comes from differentiating the loss’s ComputeTapeLoss on the autodiff tape, so a meta-learner can pass a loss that closes over its own head or metric and train the embedding through it, rather than regressing the embedding onto the labels.

Example

using AiDotNet.MetaLearning.Models;
var trainX = new Matrix<double>(10, 3);           // 10 examples, 3 features
var trainY = new Tensor<double>(new[] { 10, 4 }); // their 4-dimensional embeddings
var result = new AiModelBuilder<double, Matrix<double>, Tensor<double>>()
    .ConfigureModel(new LinearEmbeddingModel(inputDim: 3, embeddingDim: 4))
    .Build(trainX, trainY);
Tensor<double> embeddings = result.Predict(trainX);  // shape [10, 4]

Constructors

Constructor Summary
LinearEmbeddingModel(int inputDim, int embeddingDim, double learningRate) Creates a linear embedding model.

Properties

Property Summary
ILossFunction<double> DefaultLossFunction { get; }
int EmbeddingDimension { get; } Gets the width of each example’s embedding.

Methods

Method Summary
Dictionary<string, double> GetFeatureImportance()
IEnumerable<int> GetActiveFeatureIndices()
IFullModel<double, Matrix<double>, Tensor<double>> WithParameters(Vector<double> parameters)
ModelMetadata<double> GetModelMetadata()
Tensor<double> Predict(Matrix<double> input)
Vector<double> ComputeGradients(Matrix<double> input, Tensor<double> target, ILossFunction<double> lossFunction)
bool IsFeatureUsed(int featureIndex)
void ApplyGradients(Vector<double> gradients, double learningRate)
void LoadModel(string filePath)
void LoadState(Stream stream)
void SaveModel(string filePath)
void SaveState(Stream stream)
void SetActiveFeatureIndices(IEnumerable<int> featureIndices)
void Train(Matrix<double> input, Tensor<double> expectedOutput)