Class CNAPOptions<T, TInput, TOutput>
- Namespace
- AiDotNet.MetaLearning.Options
- Assembly
- AiDotNet.dll
Configuration options for the Conditional Neural Adaptive Processes (CNAP) algorithm.
public class CNAPOptions<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
-
CNAPOptions<T, TInput, TOutput>
- Implements
- Inherited Members
Remarks
CNAP extends Neural Processes by conditioning on task-specific context points and learning to produce fast adaptation weights for each task. This enables effective few-shot learning through learned task representations.
For Beginners: CNAP learns to understand tasks from examples: 1. It encodes examples (context points) into a task representation 2. It uses this representation to generate task-specific fast weights 3. The fast weights modify the base model to work well on that specific task
This is like having a teacher who can quickly understand what kind of problem you're working on and adjust their teaching style accordingly.
Key Components: - Encoder: Processes context points into task representations - Adaptation Network: Generates task-specific fast weights from representations - Base Model: The neural network that gets modified by fast weights
Constructors
CNAPOptions(IFullModel<T, TInput, TOutput>)
Initializes a new instance of the CNAPOptions class with the required meta-model.
public CNAPOptions(IFullModel<T, TInput, TOutput> metaModel)
Parameters
metaModelIFullModel<T, TInput, TOutput>The meta-model to be trained (required).
Exceptions
- ArgumentNullException
Thrown when metaModel is null.
Properties
AdaptationNetworkLayers
Gets or sets the number of layers in the adaptation network.
public int AdaptationNetworkLayers { get; set; }
Property Value
- int
Default: 2.
AdaptationSteps
Gets or sets the number of gradient steps to take during inner loop adaptation.
public int AdaptationSteps { get; set; }
Property Value
- int
Default: 1 (CNAP typically uses single-step adaptation with fast weights).
CheckpointFrequency
Gets or sets how often to save checkpoints.
public int CheckpointFrequency { get; set; }
Property Value
- int
Default: 1000.
ContextSize
Gets or sets the number of context points to use for encoding (support set size).
public int ContextSize { get; set; }
Property Value
- int
Default: 5 (for 5-shot learning).
DataLoader
Gets or sets the episodic data loader for sampling tasks.
public IEpisodicDataLoader<T, TInput, TOutput>? DataLoader { get; set; }
Property Value
- IEpisodicDataLoader<T, TInput, TOutput>
Default: null (tasks must be provided manually to MetaTrain).
DropoutRate
Gets or sets the dropout rate for regularization.
public double DropoutRate { get; set; }
Property Value
- double
Default: 0.1.
EnableCheckpointing
Gets or sets whether to save checkpoints during training.
public bool EnableCheckpointing { get; set; }
Property Value
- bool
Default: false.
EncoderLayers
Gets or sets the number of layers in the encoder network.
public int EncoderLayers { get; set; }
Property Value
- int
Default: 3.
EvaluationFrequency
Gets or sets how often to evaluate during meta-training.
public int EvaluationFrequency { get; set; }
Property Value
- int
Default: 500.
EvaluationTasks
Gets or sets the number of tasks to use for evaluation.
public int EvaluationTasks { get; set; }
Property Value
- int
Default: 100.
FastWeightMode
Gets or sets how fast weights are applied to the model.
public FastWeightApplicationMode FastWeightMode { get; set; }
Property Value
- FastWeightApplicationMode
Default: Additive.
Remarks
- Additive: Fast weights are added to base weights (θ + α) - Multiplicative: Fast weights scale base weights (θ × α) - FiLM: Feature-wise Linear Modulation (γθ + β)
FastWeightRegularization
Gets or sets the regularization weight for fast weights.
public double FastWeightRegularization { get; set; }
Property Value
- double
Default: 0.001.
FastWeightScale
Gets or sets the scale factor for fast weights.
public double FastWeightScale { get; set; }
Property Value
- double
Default: 1.0.
GradientClipThreshold
Gets or sets the maximum gradient norm for gradient clipping.
public double? GradientClipThreshold { get; set; }
Property Value
- double?
Default: 10.0.
HiddenDimension
Gets or sets the hidden dimension for the encoder and adaptation networks.
public int HiddenDimension { get; set; }
Property Value
- int
Default: 512.
InnerLearningRate
Gets or sets the learning rate for the inner loop (task adaptation).
public double InnerLearningRate { get; set; }
Property Value
- double
Default: 0.01.
InnerOptimizer
Gets or sets the optimizer for inner-loop adaptation.
public IGradientBasedOptimizer<T, TInput, TOutput>? InnerOptimizer { get; set; }
Property Value
- IGradientBasedOptimizer<T, TInput, TOutput>
Default: null (uses built-in SGD optimizer).
LossFunction
Gets or sets the loss function for training.
public ILossFunction<T>? LossFunction { get; set; }
Property Value
- ILossFunction<T>
Default: null (uses model's default loss function if available).
MetaBatchSize
Gets or sets the number of tasks to sample per meta-training iteration.
public int MetaBatchSize { get; set; }
Property Value
- int
Default: 4.
MetaModel
Gets or sets the meta-model 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 is the base network that will be modified by fast weights during task adaptation. It should be a neural network with parameters that can be efficiently modulated.
MetaOptimizer
Gets or sets the optimizer for meta-parameter updates (outer loop).
public IGradientBasedOptimizer<T, TInput, TOutput>? MetaOptimizer { get; set; }
Property Value
- IGradientBasedOptimizer<T, TInput, TOutput>
Default: null (uses built-in Adam optimizer).
NormalizeFastWeights
Gets or sets whether to normalize fast weights.
public bool NormalizeFastWeights { get; set; }
Property Value
- bool
Default: true.
Remarks
Normalizing fast weights helps stabilize training and prevents the adaptation from making overly large changes to the base model.
NumAttentionHeads
Gets or sets the number of attention heads when using attention.
public int NumAttentionHeads { get; set; }
Property Value
- int
Default: 4.
NumMetaIterations
Gets or sets the total number of meta-training iterations to perform.
public int NumMetaIterations { get; set; }
Property Value
- int
Default: 10000.
OuterLearningRate
Gets or sets the learning rate for the outer loop (meta-update).
public double OuterLearningRate { get; set; }
Property Value
- double
Default: 0.001.
PredictUncertainty
Gets or sets whether to predict uncertainty along with predictions.
public bool PredictUncertainty { get; set; }
Property Value
- bool
Default: false.
RandomSeed
Gets or sets the random seed for reproducibility.
public int? RandomSeed { get; set; }
Property Value
- int?
Default: null (non-deterministic).
RepresentationDimension
Gets or sets the dimension of the task representation vector.
public int RepresentationDimension { get; set; }
Property Value
- int
Default: 256.
Remarks
The representation dimension controls the capacity of the task encoding. Higher values can capture more complex task relationships but require more computation and may be prone to overfitting.
UncertaintyWeight
Gets or sets the weight for uncertainty loss when predicting uncertainty.
public double UncertaintyWeight { get; set; }
Property Value
- double
Default: 0.1.
UseAttention
Gets or sets whether to use attention for aggregating context points.
public bool UseAttention { get; set; }
Property Value
- bool
Default: true.
Remarks
When enabled, the encoder uses self-attention to aggregate information from multiple context points. This allows the model to learn which context points are most relevant for each task.
For Beginners: Attention helps the model focus on the most important examples when understanding a new task, rather than treating all examples equally.
UseFirstOrder
Gets or sets whether to use first-order approximation.
public bool UseFirstOrder { get; set; }
Property Value
- bool
Default: true (CNAP typically uses first-order for efficiency).
UseLayerNorm
Gets or sets whether to use layer normalization in networks.
public bool UseLayerNorm { get; set; }
Property Value
- bool
Default: true.
Methods
Clone()
Creates a deep copy of the CNAP options.
public IMetaLearnerOptions<T> Clone()
Returns
- IMetaLearnerOptions<T>
A new CNAPOptions instance with the same configuration values.
IsValid()
Validates that all CNAP configuration options are properly set.
public bool IsValid()
Returns
- bool
True if the configuration is valid for CNAP training; otherwise, false.