Skip to main content

Iteryne

Model-Agnostic Meta-Learning (MAML) for any PyTorch nn.Module.

iteryne is a small, well-tested implementation of MAML (Finn, Abbeel & Levine, 2017) built on PyTorch's native torch.func. It meta-trains a model's initial parameters so that a few gradient steps on a new task's small support set generalize to that task's query set.

Features

  • Model-agnostic. Works on any nn.Module (MLPs, CNNs, BatchNorm, ...) with no rewriting, via torch.func.functional_call.
  • First- and second-order. Full MAML (gradient-through-gradient) and FOMAML (the first-order approximation) share one code path; flip first_order=True.
  • Variants included. Meta-SGD (learnable per-parameter inner learning rates) and ANIL (adapt only the head).
  • Two levels of API. A high-level MAML wrapper plus MetaTrainer, and an exposed functional core (adapt, inner_step, functional_forward).
  • Typed and tested. py.typed, mypy-strict, pytest suite including a gradcheck of the second-order meta-gradient.

Install

pip install iteryne
# or, from a clone:
pip install -e ".[dev]"

Requires Python >= 3.10 and PyTorch >= 2.1.

Quickstart

import torch
from torch import nn
from iteryne import MAML, MetaTrainer, SinusoidTaskSampler

model = nn.Sequential(nn.Linear(1, 40), nn.ReLU(), nn.Linear(40, 40), nn.ReLU(), nn.Linear(40, 1))
maml = MAML(model, inner_lr=0.01, inner_steps=1, first_order=False)
meta_opt = torch.optim.Adam(maml.parameters(), lr=1e-3)

trainer = MetaTrainer(maml, meta_opt, nn.MSELoss(), SinusoidTaskSampler(seed=0))
trainer.fit(num_iterations=2000, meta_batch_size=25)

# Fast-adapt to a new task with a few gradient steps:
task = SinusoidTaskSampler(seed=123).sample(1)[0]
learner = maml.clone()
learner.adapt_on(nn.MSELoss(), task.support_x, task.support_y)
preds = learner(task.query_x)

Manual training loop

MetaTrainer is optional. The core pattern:

maml = MAML(model, inner_lr=0.01, inner_steps=5, first_order=False)
meta_opt = torch.optim.Adam(maml.parameters(), lr=1e-3)

meta_opt.zero_grad()
for task in task_batch:
    learner = maml.clone()
    for _ in range(maml.inner_steps):
        learner.adapt(loss_fn(learner(task.support_x), task.support_y))
    loss_fn(learner(task.query_x), task.query_y).backward()  # accumulates meta-grad
meta_opt.step()

The single backward() call computes the full second-order meta-gradient when first_order=False, and the first-order (FOMAML) meta-gradient when True.

Variants

from iteryne import MetaSGD, ANIL

meta_sgd = MetaSGD(model, inner_lr=0.01)            # learns per-parameter inner LRs
anil = ANIL(model, head=model[-1], inner_lr=0.01)   # adapts only the head

How it works

Parameters are carried as a dict[str, Tensor] and applied with functional_call, so the module itself never has to be modified. The inner step

p' = p - alpha * grad(support_loss, p)

is computed with torch.autograd.grad(..., create_graph=not first_order). With create_graph=True the adapted parameters stay connected to the originals, so a later backward() differentiates through the inner step (full MAML). With create_graph=False the gradient term is detached, leaving the identity edge p' = p - alpha * g so the same backward() yields the first-order meta-gradient (FOMAML). One flag, one code path.

BatchNorm and other buffers are carried through the functional forward but are not adapted in the inner loop; for few-shot work consider track_running_stats=False.

Documentation

Build the docs site locally:

pip install -e ".[docs]"
mkdocs serve

Citation

@inproceedings{finn2017maml,
  title     = {Model-Agnostic Meta-Learning for Fast Adaptation of Deep Networks},
  author    = {Finn, Chelsea and Abbeel, Pieter and Levine, Sergey},
  booktitle = {International Conference on Machine Learning (ICML)},
  year      = {2017}
}

Metadata

Release files for iteryne 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for iteryne 0.1.0
File Size Uploaded Provenance Actions
iteryne-0.1.0.tar.gz 21.8 kB With provenance Details for iteryne-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for iteryne 0.1.0
File Interpreter ABI Platform Actions
iteryne-0.1.0-py3-none-any.whl Python 3 none any Details for iteryne-0.1.0-py3-none-any.whl

Total release size: 38.4 kB

Release files / iteryne-0.1.0.tar.gz

Download URL iteryne-0.1.0.tar.gz
Size 21.8 kB
Tags Source
SHA-256 checksum
How to use checksums
fedb27e6d31f923221fac94d7ee9bf03fdb833d2783df4a8d2be1fa285a17443
BLAKE2b-256 checksum
How to use checksums
04e33d2328b197b2161de9524efca6c7d7c03000b17afaeb4729f2eb454fd93f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 29, 2026.

Transparency log

Release files / iteryne-0.1.0-py3-none-any.whl

Download URL iteryne-0.1.0-py3-none-any.whl
Size 16.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
71c1f7a932434a9544777e32cc0f8595fc5b972965b96bcc8eb18548dd49e86e
BLAKE2b-256 checksum
How to use checksums
358fd4719d873c41590d59cf63fc69391cff57d6a6bd97af2ac6509e1523445e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 29, 2026.

Transparency log

Security for iteryne 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for iteryne-0.1.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 29, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page