IMetaLearner<T, TInput, TOutput>

Interfaces · AiDotNet.Interfaces

Unified interface for meta-learning algorithms that train models to quickly adapt to new tasks.

Known implementations: MetaLearnerBase<T, TInput, TOutput>

For Beginners

Meta-learning is like teaching someone how to learn, not just what to learn.

Traditional vs Meta-Learning:

  • Traditional: Train on thousands of cat/dog images → classify cats vs dogs well
  • Meta-Learning: Train on many classification tasks → learn ANY new category from 5 examples

Real-world applications:

  • Few-shot image classification (recognize new objects from 1-5 images)
  • Rapid robot adaptation (new environments with minimal data)
  • Personalized recommendations (adapt to new users quickly)
  • Drug discovery (predict properties of new molecules)

How It Works

This is the unified interface for all meta-learning algorithms in the framework. It combines both training infrastructure and algorithm capabilities, enabling seamless integration with AiModelBuilder while supporting all 17 meta-learning algorithms (MAML, Reptile, ProtoNets, LEO, MetaOptNet, etc.).

**Architecture - Two-Loop Optimization:**Inner Loop (Task Adaptation):

  • Given: New task with support set (K examples per class)
  • Process: Few gradient steps (1-10) to adapt model
  • Output: Task-specific adapted parameters
  • Goal: Quickly learn this specific task

Outer Loop (Meta-Optimization):

  • Given: Batch of tasks from task distribution
  • Process: For each task, adapt (inner loop) and evaluate on query set
  • Output: Updated meta-parameters
  • Goal: Learn parameters that enable fast adaptation across all tasks

This two-loop structure is what enables “learning to learn.”

Production Considerations:

  • Use MetaTrainStep() for training loop with proper batch sizes (2-32 tasks)
  • Monitor Evaluate() metrics every N iterations to detect overfitting
  • Use AdaptAndEvaluate() for deployment to quickly adapt to new tasks
  • Save/Load models after meta-training for deployment
  • Thread Safety: Not thread-safe, use separate instances for concurrent training

Example

var trainingFeatures = new Matrix<double>(new double[,] { { 1.0, 2.0 }, { 3.0, 4.0 }, { 5.0, 6.0 }, { 7.0, 8.0 } });
var trainingLabels = new Vector<double>(new double[] { 0.0, 1.0, 0.0, 1.0 });
            
// 1. Setup: an episodic loader that samples 5-way 5-shot tasks
var dataLoader = new UniformEpisodicDataLoader<double, Tensor<double>, Tensor<double>>(
    datasetX: trainingFeatures,
    datasetY: trainingLabels,
    nWay: 5,          // 5 classes per task
    kShot: 5,         // 5 support examples per class
    queryShots: 15    // 15 query examples per class
);
            
// 2. Configure: the meta-model is what the outer loop learns
var architecture = new NeuralNetworkArchitecture<double>(inputFeatures: 4, outputSize: 5);
var metaModel = new NeuralNetwork<double>(architecture);
            
var options = new MAMLOptions<double, Tensor<double>, Tensor<double>>(metaModel)
{
    InnerLearningRate = 0.01,
    OuterLearningRate = 0.001,
    AdaptationSteps = 5,
    MetaBatchSize = 4,
    NumMetaIterations = 1000,
    LossFunction = new CrossEntropyLoss<double>(),
    DataLoader = dataLoader
};
            
// 3. Meta-training
var trainX = Tensor<double>.CreateRandom(4, 4);
var trainY = Tensor<double>.CreateRandom(4, 5);
            
var result = new AiModelBuilder<double, Tensor<double>, Tensor<double>>()
    .ConfigureModel(new MAMLAlgorithm<double, Tensor<double>, Tensor<double>>(options))
    .Build(trainX, trainY);
            
// 4. Deployment: adapt to a task the model has never seen
var newTask = dataLoader.GetNextTask();
var adaptResult = result.AdaptAndEvaluate(newTask);

Properties

Property Summary
IFullModel<T, TInput, TOutput> BaseModel { get; } Gets the base model being meta-trained.
IMetaLearnerOptions<T> Options { get; } Gets the meta-learner options (configuration).
MetaLearningAlgorithmType AlgorithmType { get; } Gets the type of meta-learning algorithm.
double InnerLearningRate { get; } Gets the learning rate used for task adaptation (inner loop).
double OuterLearningRate { get; } Gets the learning rate used for meta-learning (outer loop).
int AdaptationSteps { get; } Gets the number of adaptation steps to perform during task adaptation (inner loop).
int CurrentIteration { get; } Gets the current meta-training iteration count.

Methods

Method Summary
IFullModel<T, TInput, TOutput> GetMetaModel() Gets the current meta-model.
IModel<TInput, TOutput, ModelMetadata<T>> Adapt(IMetaLearningTask<T, TInput, TOutput> task) Adapts the model to a new task using its support set.
MetaAdaptationResult<T> AdaptAndEvaluate(MetaLearningTask<T, TInput, TOutput> task) Adapts the model to a specific task and evaluates adaptation quality.
MetaEvaluationResult<T> Evaluate(int numTasks) Evaluates meta-learning performance on multiple held-out tasks.
MetaTrainingResult<T> Train() Trains the meta-learner using the configuration specified during construction.
MetaTrainingStepResult<T> MetaTrainStep(int batchSize) Performs one meta-training step (outer loop update) using the episodic data loader.
T Evaluate(TaskBatch<T, TInput, TOutput> taskBatch) Evaluates meta-learning performance on multiple held-out tasks.
T MetaTrain(TaskBatch<T, TInput, TOutput> taskBatch) Performs one meta-training step on a batch of tasks.
void Load(string filePath) Loads a previously meta-trained model from disk.
void Reset() Resets the meta-learner to initial untrained state.
void Save(string filePath) Saves the meta-trained model to disk for later deployment.
void SetMetaModel(IFullModel<T, TInput, TOutput> model) Sets the base model for this meta-learning algorithm.