Skip to content

Examples

Sinusoid regression

The repository ships a runnable script at examples/sinusoid_regression.py reproducing the spirit of section 5.1 of the MAML paper. It meta-trains a 2-hidden-layer ReLU network (width 40) on sine-wave regression, then shows that a few gradient steps on 10 support points fit a previously unseen wave.

python examples/sinusoid_regression.py

Expected output (abridged):

iter    0  meta-loss 3.19
...
iter 1800  meta-loss 0.71
held-out query MSE  pre-adapt: 3.13  post-adapt: 0.87

The post-adaptation query MSE is far below the pre-adaptation value: the meta-learned initialization adapts to a new task from only a handful of points.

Meta-SGD and ANIL

from iteryne import MetaSGD, ANIL

# Learn a per-parameter inner learning rate alongside the initialization.
meta_sgd = MetaSGD(model, inner_lr=0.01, inner_steps=1)

# Adapt only the final layer in the inner loop.
anil = ANIL(model, head=model[-1], inner_lr=0.01, inner_steps=5)

Both are drop-in replacements for MAML and work with MetaTrainer unchanged.

Classification

iteryne is loss-agnostic. For N-way K-shot classification, supply a classification model and nn.CrossEntropyLoss, and provide a TaskSampler that yields class-balanced support/query splits:

import torch.nn as nn
from iteryne import MAML, MetaTrainer

maml = MAML(conv_net, inner_lr=0.01, inner_steps=5, first_order=True)
trainer = MetaTrainer(
    maml,
    torch.optim.Adam(maml.parameters(), lr=1e-3),
    nn.CrossEntropyLoss(),
    my_nway_kshot_sampler,
)
trainer.fit(num_iterations=10_000, meta_batch_size=4)