Class MatchingNetworksOptions<T, TInput, TOutput>
- Namespace
- AiDotNet.MetaLearning.Options
- Assembly
- AiDotNet.dll
Configuration options for Matching Networks algorithm.
public class MatchingNetworksOptions<T, TInput, TOutput> : IMetaLearnerOptions<T>
Type Parameters
TThe numeric data type used for calculations (e.g., float, double).
TInputThe input data type (e.g., Matrix<T>, Tensor<T>).
TOutputThe output data type (e.g., Vector<T>, Tensor<T>).
- Inheritance
-
MatchingNetworksOptions<T, TInput, TOutput>
- Implements
- Inherited Members
Remarks
Matching Networks use attention mechanisms over the support set to classify query examples. It computes a weighted sum of support labels where weights are determined by an attention function that measures similarity between examples.
For Beginners: Matching Networks learn to pay attention to similar examples:
- Encode all examples (support and query) with a shared encoder
- For each query, compute attention weights with all support examples
- Use similarity (cosine, dot product, etc.) for weights
- Predict weighted sum of support labels (soft nearest neighbor)
Unlike ProtoNets which uses class prototypes, Matching Networks consider every support example individually.
Constructors
MatchingNetworksOptions(IFullModel<T, TInput, TOutput>)
Initializes a new instance of the MatchingNetworksOptions class with the required meta-model.
public MatchingNetworksOptions(IFullModel<T, TInput, TOutput> metaModel)
Parameters
metaModelIFullModel<T, TInput, TOutput>The encoder to be trained (required).
Examples
// Create Matching Networks with minimal configuration
var options = new MatchingNetworksOptions<double, Tensor<double>, Tensor<double>>(myEncoder);
var matchingNets = new MatchingNetworksAlgorithm<double, Tensor<double>, Tensor<double>>(options);
// Create with custom attention function
var options = new MatchingNetworksOptions<double, Tensor<double>, Tensor<double>>(myEncoder)
{
AttentionFunction = MatchingNetworksAttentionFunction.DotProduct,
UseBidirectionalEncoding = true
};
Exceptions
- ArgumentNullException
Thrown when metaModel is null.
Properties
AdaptationSteps
Gets or sets the number of adaptation steps.
public int AdaptationSteps { get; set; }
Property Value
- int
Default is 1 (Matching Networks uses attention-based adaptation).
AttentionFunction
Gets or sets the attention function for computing similarity.
public MatchingNetworksAttentionFunction AttentionFunction { get; set; }
Property Value
- MatchingNetworksAttentionFunction
Default is Cosine.
Remarks
For Beginners: - Cosine: Measures angle between vectors (most common) - DotProduct: Simple inner product - Euclidean: Uses distance (inverted so closer = higher) - Learned: Uses a neural network to compute similarity
CheckpointFrequency
Gets or sets how often to save checkpoints.
public int CheckpointFrequency { get; set; }
Property Value
DataLoader
Gets or sets the episodic data loader for sampling tasks. Default: null (tasks must be provided manually to MetaTrain).
public IEpisodicDataLoader<T, TInput, TOutput>? DataLoader { get; set; }
Property Value
- IEpisodicDataLoader<T, TInput, TOutput>
EnableCheckpointing
Gets or sets whether to save model checkpoints.
public bool EnableCheckpointing { get; set; }
Property Value
EvaluationFrequency
Gets or sets how often to evaluate the meta-learner.
public int EvaluationFrequency { get; set; }
Property Value
EvaluationTasks
Gets or sets the number of tasks to use for evaluation.
public int EvaluationTasks { get; set; }
Property Value
GradientClipThreshold
Gets or sets the maximum gradient norm for gradient clipping.
public double? GradientClipThreshold { get; set; }
Property Value
- double?
Default is 10.0.
InnerLearningRate
Gets or sets the learning rate for the inner loop (not used in Matching Networks).
public double InnerLearningRate { get; set; }
Property Value
- double
Default is 0.01.
L2Regularization
Gets or sets the L2 regularization strength.
public double L2Regularization { get; set; }
Property Value
- double
Default is 0.0 (no regularization).
LossFunction
Gets or sets the loss function for training. Default: null (uses cross-entropy loss internally).
public ILossFunction<T>? LossFunction { get; set; }
Property Value
MetaBatchSize
Gets or sets the number of tasks to sample per meta-training iteration.
public int MetaBatchSize { get; set; }
Property Value
- int
Default is 4.
MetaModel
Gets or sets the meta-model (encoder) to be trained. This is the only required property.
public IFullModel<T, TInput, TOutput> MetaModel { get; set; }
Property Value
- IFullModel<T, TInput, TOutput>
Remarks
The meta-model should be a feature encoder that maps inputs to an embedding space where similar examples are close together.
For Beginners: This is typically a CNN for images or an MLP for tabular data. The encoder learns to produce embeddings where attention weights are meaningful.
MetaOptimizer
Gets or sets the optimizer for encoder updates. Default: null (uses built-in Adam optimizer with OuterLearningRate).
public IGradientBasedOptimizer<T, TInput, TOutput>? MetaOptimizer { get; set; }
Property Value
- IGradientBasedOptimizer<T, TInput, TOutput>
NumClasses
Gets or sets the number of output classes.
public int NumClasses { get; set; }
Property Value
- int
Default is 5.
NumMetaIterations
Gets or sets the total number of meta-training iterations.
public int NumMetaIterations { get; set; }
Property Value
- int
Default is 1000.
OuterLearningRate
Gets or sets the learning rate for the outer loop (encoder training).
public double OuterLearningRate { get; set; }
Property Value
- double
Default is 0.001.
RandomSeed
Gets or sets the random seed for reproducibility.
public int? RandomSeed { get; set; }
Property Value
- int?
Temperature
Gets or sets the temperature for softmax attention.
public double Temperature { get; set; }
Property Value
- double
Default is 1.0.
Remarks
Lower temperature makes attention weights more peaked (focused on one example). Higher temperature makes attention weights more uniform.
UseBidirectionalEncoding
Gets or sets whether to use bidirectional encoding.
public bool UseBidirectionalEncoding { get; set; }
Property Value
- bool
Default is false.
Remarks
When enabled, uses bidirectional LSTM to encode sequences, allowing information to flow in both directions.
UseFirstOrder
Gets or sets whether to use first-order approximation.
public bool UseFirstOrder { get; set; }
Property Value
- bool
Default is true since Matching Networks doesn't use gradient-based inner loop.
UseFullContextEmbedding
Gets or sets whether to use full context embedding.
public bool UseFullContextEmbedding { get; set; }
Property Value
- bool
Default is false.
Remarks
When enabled, each example's embedding considers all other examples in the episode through attention mechanisms.
Methods
Clone()
Creates a deep copy of the Matching Networks options.
public IMetaLearnerOptions<T> Clone()
Returns
- IMetaLearnerOptions<T>
A new MatchingNetworksOptions instance with the same configuration.
IsValid()
Validates that all Matching Networks configuration options are properly set.
public bool IsValid()
Returns
- bool
True if the configuration is valid; otherwise, false.