API reference
High-level API
iteryne.MAML
Bases: Module
Model-agnostic meta-learning wrapper for any nn.Module.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
module
|
Module
|
The model to meta-train. Its parameters are the meta-parameters
|
required |
inner_lr
|
float
|
Inner-loop step size |
0.01
|
inner_steps
|
int
|
Default number of inner gradient steps used by :meth: |
1
|
first_order
|
bool
|
If |
False
|
adapt_names
|
Iterable[str] | None
|
Optional subset of parameter names to adapt in the inner loop. Defaults
to all parameters. See :mod: |
None
|
allow_unused
|
bool
|
Forwarded to the inner gradient computation. |
True
|
Source code in src/iteryne/maml.py
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | |
clone()
Create a fresh :class:Learner starting from the current theta.
The learner references the wrapped module's live parameter leaves, so a
backward() through an adapted learner accumulates meta-gradients into
self.parameters().
Source code in src/iteryne/maml.py
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 | |
forward(*args, **kwargs)
Forward pass through the unadapted module (the meta-parameters).
Source code in src/iteryne/maml.py
188 189 190 | |
iteryne.Learner
A single adaptable copy of a model's parameters.
A Learner is created by :meth:MAML.clone. It starts from the wrapped
module's live parameters and updates an internal parameter mapping with each
:meth:adapt call. Calling the learner runs a stateless forward pass with
the current (possibly adapted) parameters.
Source code in src/iteryne/maml.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | |
__call__(*args, **kwargs)
Forward pass using the learner's current parameters.
Source code in src/iteryne/maml.py
72 73 74 | |
adapt(support_loss)
Take one differentiable inner-loop step from support_loss.
Updates :attr:params in place (the learner) and returns self so
calls can be chained.
Source code in src/iteryne/maml.py
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | |
adapt_on(loss_fn, x, y, *, steps=None)
Run steps inner steps on a single (x, y) support batch.
Convenience wrapper that recomputes the support loss at each step. If
steps is None, uses the learner's configured inner_steps.
Source code in src/iteryne/maml.py
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | |
iteryne.MetaSGD
Bases: MAML
MAML variant with learnable, per-parameter inner learning rates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
module
|
Module
|
Model to meta-train. |
required |
inner_lr
|
float
|
Initial value for every per-parameter learning rate. |
0.01
|
inner_steps
|
int
|
Number of inner gradient steps. |
1
|
first_order
|
bool
|
Defaults to |
False
|
adapt_names
|
Iterable[str] | None
|
Optional subset of parameter names to adapt (and to learn rates for). |
None
|
allow_unused
|
bool
|
Forwarded to the inner gradient computation. |
True
|
Source code in src/iteryne/metasgd.py
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | |
iteryne.ANIL
Bases: MAML
MAML that adapts only the parameters of a head submodule.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
module
|
Module
|
The full model to meta-train. |
required |
head
|
Module
|
A submodule of |
required |
**kwargs
|
object
|
Forwarded to :class: |
{}
|
Source code in src/iteryne/anil.py
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | |
iteryne.head_names(module, head)
Return the names (as seen from module) of head's parameters.
Parameters are matched by identity, so head must be a submodule of
module (or otherwise share the same parameter tensors).
Source code in src/iteryne/anil.py
21 22 23 24 25 26 27 28 29 30 31 | |
Training
iteryne.MetaTrainer
Drive MAML meta-training over batches of tasks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
maml
|
MAML
|
The MAML wrapper (or any subclass, e.g. |
required |
meta_optimizer
|
Optimizer
|
Optimizer over |
required |
loss_fn
|
LossFn
|
|
required |
task_sampler
|
TaskSampler
|
Source of training tasks. |
required |
Source code in src/iteryne/trainer.py
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | |
meta_step(tasks)
Run one meta-update over tasks and return the mean query loss.
Source code in src/iteryne/trainer.py
62 63 64 65 66 67 68 69 70 71 72 73 74 | |
fit(num_iterations, meta_batch_size, *, eval_every=None, eval_tasks=None, callback=None)
Meta-train for num_iterations outer steps.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_iterations
|
int
|
Number of meta-updates. |
required |
meta_batch_size
|
int
|
Number of tasks sampled per meta-update. |
required |
eval_every
|
int | None
|
If set, run :meth: |
None
|
eval_tasks
|
Sequence[Task] | None
|
Held-out tasks for evaluation (required if |
None
|
callback
|
Callable[[int, float], None] | None
|
Optional |
None
|
Returns:
| Type | Description |
|---|---|
History
|
Per-iteration meta-loss and any recorded evaluation losses. |
Source code in src/iteryne/trainer.py
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | |
evaluate(tasks)
Adapt to each task and return the mean post-adaptation query loss.
Adaptation still requires gradients (the inner loop), but the reported query loss is detached and no meta-update is performed.
Source code in src/iteryne/trainer.py
123 124 125 126 127 128 129 130 131 132 133 134 | |
iteryne.History
dataclass
Records produced by :meth:MetaTrainer.fit.
Source code in src/iteryne/trainer.py
26 27 28 29 30 31 | |
Tasks and data
iteryne.Task
dataclass
A single few-shot task: a support split and a query split.
Attributes:
| Name | Type | Description |
|---|---|---|
support_x, support_y |
Inputs and targets used for inner-loop adaptation ( |
|
query_x, query_y |
Held-out inputs and targets used to evaluate the adapted model and form the meta-objective. |
Source code in src/iteryne/tasks.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | |
iteryne.TaskSampler
Bases: Protocol
Protocol for objects that sample a batch of :class:Task from p(T).
Source code in src/iteryne/tasks.py
41 42 43 44 45 46 47 | |
sample(meta_batch_size)
Return meta_batch_size freshly sampled tasks.
Source code in src/iteryne/tasks.py
45 46 47 | |
iteryne.SinusoidTaskSampler
Sample few-shot sinusoid regression tasks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
amp_range
|
tuple[float, float]
|
Range |
(0.1, 5.0)
|
phase_range
|
tuple[float, float]
|
Range |
(0.0, pi)
|
x_range
|
tuple[float, float]
|
Range |
(-5.0, 5.0)
|
k_support
|
int
|
Number of support points per task ( |
10
|
k_query
|
int
|
Number of query points per task. |
10
|
generator
|
Generator | None
|
Optional :class: |
None
|
seed
|
int | None
|
Convenience seed used to build a generator when |
None
|
device
|
device | str | None
|
Tensor placement/precision for sampled data. |
None
|
dtype
|
device | str | None
|
Tensor placement/precision for sampled data. |
None
|
Source code in src/iteryne/datasets/sinusoid.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | |
sample(meta_batch_size)
Return meta_batch_size independently sampled sinusoid tasks.
Source code in src/iteryne/datasets/sinusoid.py
98 99 100 | |
Functional core
iteryne.functional_forward(model, params, *args, buffers=None, **kwargs)
Run model statelessly using the given params (and buffers).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Module
|
Any |
required |
params
|
Mapping[str, Tensor]
|
Mapping of parameter name to tensor, as returned by :func: |
required |
*args
|
Any
|
Forwarded to the module's |
()
|
**kwargs
|
Any
|
Forwarded to the module's |
()
|
buffers
|
Mapping[str, Tensor] | None
|
Optional mapping of buffer name to tensor. If |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
Whatever |
Source code in src/iteryne/functional.py
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | |
iteryne.inner_step(params, loss, lr, *, first_order=False, adapt_names=None, allow_unused=True)
Take one differentiable gradient-descent step on params.
Computes grads = d loss / d params and returns the updated mapping
p' = p - lr * grad for every adapted parameter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
Mapping[str, Tensor]
|
Current parameter mapping (the point at which |
required |
loss
|
Tensor
|
Scalar support loss, differentiable w.r.t. |
required |
lr
|
LearningRate
|
Inner-loop step size |
required |
first_order
|
bool
|
If |
False
|
adapt_names
|
Iterable[str] | None
|
Optional subset of parameter names to update in the inner loop. Names not
in this set are passed through unchanged (used by ANIL to adapt only the
head). If |
None
|
allow_unused
|
bool
|
Passed to :func: |
True
|
Returns:
| Type | Description |
|---|---|
dict[str, Tensor]
|
The adapted parameter mapping. |
Source code in src/iteryne/functional.py
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | |
iteryne.adapt(model, params, build_loss, *, lr=0.01, inner_steps=1, first_order=False, adapt_names=None, allow_unused=True)
Run the full inner loop and return the adapted parameters theta'.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Module
|
The module whose architecture defines the forward pass. |
required |
params
|
Mapping[str, Tensor]
|
Starting parameter mapping |
required |
build_loss
|
Callable[[dict[str, Tensor]], Tensor]
|
Callable mapping a parameter dict to the scalar support loss. Keeping the
loss as a closure (rather than |
required |
lr
|
LearningRate
|
Inner step size |
0.01
|
inner_steps
|
int
|
Number of gradient steps in the inner loop ( |
1
|
first_order
|
bool
|
See :func: |
False
|
adapt_names
|
bool
|
See :func: |
False
|
allow_unused
|
bool
|
See :func: |
False
|
Returns:
| Type | Description |
|---|---|
dict[str, Tensor]
|
The adapted parameter mapping after |
Source code in src/iteryne/functional.py
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 | |
iteryne.named_params(model)
Return a fresh {name: parameter} dict for model.
The tensors are the model's live :class:~torch.nn.Parameter leaves, so
gradients flowing back into them accumulate into model.parameters().grad.
Source code in src/iteryne/functional.py
47 48 49 50 51 52 53 | |
iteryne.named_buffers(model)
Return a fresh {name: buffer} dict for model (e.g. BatchNorm stats).
Source code in src/iteryne/functional.py
56 57 58 | |