Class ProtoNetsOptions<T, TInput, TOutput>
- Namespace
- AiDotNet.MetaLearning.Options
- Assembly
- AiDotNet.dll
Configuration options for Prototypical Networks (ProtoNets) algorithm.
public class ProtoNetsOptions<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
-
ProtoNetsOptions<T, TInput, TOutput>
- Implements
- Inherited Members
Remarks
Prototypical Networks learn a metric space where classification is performed by computing distances to prototype representations of each class. Each prototype is the mean vector of the support set examples for that class in the learned embedding space.
For Beginners: ProtoNets is one of the simplest and most effective few-shot learning methods:
- Use a neural network to convert images/data into feature vectors
- For each class in a task, compute the "prototype" (average feature vector)
- To classify a new example, find the nearest prototype
- Train the network to make same-class examples cluster together
Unlike MAML, ProtoNets doesn't need gradient updates at test time - just compute prototypes and measure distances!
Constructors
ProtoNetsOptions(IFullModel<T, TInput, TOutput>)
Initializes a new instance of the ProtoNetsOptions class with the required meta-model.
public ProtoNetsOptions(IFullModel<T, TInput, TOutput> metaModel)
Parameters
metaModelIFullModel<T, TInput, TOutput>The feature encoder to be trained (required).
Examples
// Create ProtoNets with minimal configuration
var options = new ProtoNetsOptions<double, Tensor<double>, Tensor<double>>(myEncoder);
var protoNets = new ProtoNetsAlgorithm<double, Tensor<double>, Tensor<double>>(options);
// Create ProtoNets with custom configuration
var options = new ProtoNetsOptions<double, Tensor<double>, Tensor<double>>(myEncoder)
{
DistanceFunction = ProtoNetsDistanceFunction.Cosine,
NormalizeFeatures = true,
Temperature = 0.5
};
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 (ProtoNets uses non-parametric adaptation).
Remarks
ProtoNets doesn't use gradient-based adaptation steps like MAML. This value is kept for interface compatibility. The "adaptation" in ProtoNets is simply computing class prototypes, which is done in one step.
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>
DistanceFunction
Gets or sets the distance function for measuring similarity between embeddings.
public ProtoNetsDistanceFunction DistanceFunction { get; set; }
Property Value
- ProtoNetsDistanceFunction
The distance function. Default is Euclidean.
Remarks
The distance function determines how similarity is measured between query embeddings and class prototypes. Euclidean distance is the default and works well for most applications.
For Beginners: Start with Euclidean. Try Cosine if your features are normalized. Use Mahalanobis if you have domain knowledge about feature correlations.
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?
The gradient clip threshold, or null to disable. Default is 10.0.
InnerLearningRate
Gets or sets the learning rate for the inner loop (task adaptation).
public double InnerLearningRate { get; set; }
Property Value
- double
The inner learning rate. Default is 0.01.
Remarks
Note: ProtoNets doesn't perform gradient-based inner loop adaptation like MAML. This value is kept for interface compatibility but is not used in the core algorithm. Prototype computation is non-parametric.
LossFunction
Gets or sets the loss function for training. Default: null (uses cross-entropy loss internally).
public ILossFunction<T>? LossFunction { get; set; }
Property Value
Remarks
ProtoNets typically uses cross-entropy loss with softmax over negative distances. This encourages the correct class prototype to be closer than other prototypes.
MahalanobisScaling
Gets or sets the scaling factor for Mahalanobis distance.
public double MahalanobisScaling { get; set; }
Property Value
- double
The Mahalanobis scaling factor. Default is 1.0.
Remarks
This is a simplified scaling factor used when DistanceFunction is Mahalanobis. In a full implementation, this would be replaced by a learned covariance matrix.
MetaBatchSize
Gets or sets the number of tasks to sample per meta-training iteration.
public int MetaBatchSize { get; set; }
Property Value
- int
The meta-batch size. Default is 4.
Remarks
Each training step samples this many tasks (episodes), computes loss on each, and averages the gradients before updating the encoder.
MetaModel
Gets or sets the meta-model (feature 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 same-class examples are close together and different-class examples are far apart.
For Beginners: This is typically a CNN for images or an MLP for tabular data. The network learns to output feature vectors that cluster by class.
MetaOptimizer
Gets or sets the optimizer for feature 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>
NormalizeFeatures
Gets or sets whether to L2-normalize feature embeddings.
public bool NormalizeFeatures { get; set; }
Property Value
- bool
True to normalize features; false otherwise. Default is false.
Remarks
When enabled, feature vectors are normalized to unit length before computing prototypes and distances. This can improve stability and is recommended when using cosine distance.
For Beginners: Enable this if you're using Cosine distance, or if you notice that feature magnitudes vary widely.
NumMetaIterations
Gets or sets the total number of meta-training iterations.
public int NumMetaIterations { get; set; }
Property Value
- int
The number of meta-iterations. Default is 1000.
OuterLearningRate
Gets or sets the learning rate for the outer loop (encoder training).
public double OuterLearningRate { get; set; }
Property Value
- double
The outer learning rate. Default is 0.001.
Remarks
This controls how quickly the feature encoder is updated during training. A typical range is 0.0001 to 0.01.
For Beginners: This is how fast the embedding network learns. Start with 0.001 and adjust based on training stability.
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 scaling.
public double Temperature { get; set; }
Property Value
- double
The temperature value. Default is 1.0.
Remarks
Temperature controls the sharpness of the probability distribution: - Lower temperature (< 1.0): Sharper, more confident predictions - Higher temperature (> 1.0): Softer, more uniform predictions
For Beginners: Leave at 1.0 unless you want to calibrate confidence. Lower values make the model more "certain" about its predictions.
UseAdaptiveClassScaling
Gets or sets whether to use adaptive class-specific scaling factors.
public bool UseAdaptiveClassScaling { get; set; }
Property Value
- bool
True to use adaptive scaling; false otherwise. Default is false.
Remarks
When enabled, learns per-class scaling factors for distances. This allows the model to handle classes with different intra-class variances.
UseAttentionMechanism
Gets or sets whether to use an attention mechanism for prototype computation.
public bool UseAttentionMechanism { get; set; }
Property Value
- bool
True to use attention; false for simple averaging. Default is false.
Remarks
When enabled, uses learned attention weights to compute weighted prototypes instead of simple averaging. This can help focus on more informative examples.
For Beginners: Start with false (simple averaging). Enable if you have noisy or heterogeneous support sets.
UseFirstOrder
Gets or sets whether to use first-order approximation.
public bool UseFirstOrder { get; set; }
Property Value
- bool
Default is true since ProtoNets doesn't use gradient-based inner loop.
Remarks
For ProtoNets, this option is less relevant because adaptation is non-parametric (prototype computation, not gradient descent). This property is provided for interface compatibility with IMetaLearnerOptions.
Methods
Clone()
Creates a deep copy of the ProtoNets options.
public IMetaLearnerOptions<T> Clone()
Returns
- IMetaLearnerOptions<T>
A new ProtoNetsOptions instance with the same configuration.
IsValid()
Validates that all ProtoNets configuration options are properly set.
public bool IsValid()
Returns
- bool
True if the configuration is valid; otherwise, false.
Remarks
Checks: - MetaModel is set - Learning rate is positive - Temperature is positive - Batch sizes and iteration counts are positive