learn2learn.algorithms

MAML

1
MAML(self, model, lr, first_order=False, allow_unused=None, allow_nograd=False)

[Source]

Description

High-level implementation of Model-Agnostic Meta-Learning.

This class wraps an arbitrary nn.Module and augments it with clone() and adapt() methods.

For the first-order version of MAML (i.e. FOMAML), set the first_order flag to True upon initialization.

Arguments

  • model (Module) - Module to be wrapped.
  • lr (float) - Fast adaptation learning rate.
  • first_order (bool, optional, default=False) - Whether to use the first-order approximation of MAML. (FOMAML)
  • allow_unused (bool, optional, default=None) - Whether to allow differentiation of unused parameters. Defaults to allow_nograd.
  • allow_nograd (bool, optional, default=False) - Whether to allow adaptation with parameters that have requires_grad = False.

References

  1. Finn et al. 2017. "Model-Agnostic Meta-Learning for Fast Adaptation of Deep Networks."

Example

1
2
3
4
5
6
linear = l2l.algorithms.MAML(nn.Linear(20, 10), lr=0.01)
clone = linear.clone()
error = loss(clone(X), y)
clone.adapt(error)
error = loss(clone(X), y)
error.backward()

adapt

1
MAML.adapt(self, loss, first_order=None, allow_unused=None, allow_nograd=None)

Description

Takes a gradient step on the loss and updates the cloned parameters in place.

Arguments

  • loss (Tensor) - Loss to minimize upon update.
  • first_order (bool, optional, default=None) - Whether to use first- or second-order updates. Defaults to self.first_order.
  • allow_unused (bool, optional, default=None) - Whether to allow differentiation of unused parameters. Defaults to self.allow_unused.
  • allow_nograd (bool, optional, default=None) - Whether to allow adaptation with parameters that have requires_grad = False. Defaults to self.allow_nograd.

clone

1
MAML.clone(self, first_order=None, allow_unused=None, allow_nograd=None)

Description

Returns a MAML-wrapped copy of the module whose parameters and buffers are torch.cloned from the original module.

This implies that back-propagating losses on the cloned module will populate the buffers of the original module. For more information, refer to learn2learn.clone_module().

Arguments

  • first_order (bool, optional, default=None) - Whether the clone uses first- or second-order updates. Defaults to self.first_order.
  • allow_unused (bool, optional, default=None) - Whether to allow differentiation of unused parameters. Defaults to self.allow_unused.
  • allow_nograd (bool, optional, default=False) - Whether to allow adaptation with parameters that have requires_grad = False. Defaults to self.allow_nograd.

maml_update

1
maml_update(model, lr, grads=None)

[Source]

Description

Performs a MAML update on model using grads and lr. The function re-routes the Python object, thus avoiding in-place operations.

NOTE: The model itself is updated in-place (no deepcopy), but the parameters' tensors are not.

Arguments

  • model (Module) - The model to update.
  • lr (float) - The learning rate used to update the model.
  • grads (list, optional, default=None) - A list of gradients for each parameter of the model. If None, will use the gradients in .grad attributes.

Example

1
2
3
4
maml = l2l.algorithms.MAML(Model(), lr=0.1)
model = maml.clone() # The next two lines essentially implement model.adapt(loss)
grads = autograd.grad(loss, model.parameters(), create_graph=True)
maml_update(model, lr=0.1, grads)

MetaSGD

1
MetaSGD(self, model, lr=1.0, first_order=False, lrs=None)

[Source]

Description

High-level implementation of Meta-SGD.

This class wraps an arbitrary nn.Module and augments it with clone() and adapt methods. It behaves similarly to MAML, but in addition a set of per-parameters learning rates are learned for fast-adaptation.

Arguments

  • model (Module) - Module to be wrapped.
  • lr (float) - Initialization value of the per-parameter fast adaptation learning rates.
  • first_order (bool, optional, default=False) - Whether to use the first-order version.
  • lrs (list of Parameters, optional, default=None) - If not None, overrides lr, and uses the list as learning rates for fast-adaptation.

References

  1. Li et al. 2017. “Meta-SGD: Learning to Learn Quickly for Few-Shot Learning.” arXiv.

Example

1
2
3
4
5
6
linear = l2l.algorithms.MetaSGD(nn.Linear(20, 10), lr=0.01)
clone = linear.clone()
error = loss(clone(X), y)
clone.adapt(error)
error = loss(clone(X), y)
error.backward()

clone

1
MetaSGD.clone(self)

Descritpion

Akin to MAML.clone() but for MetaSGD: it includes a set of learnable fast-adaptation learning rates.

adapt

1
MetaSGD.adapt(self, loss, first_order=None)

Descritpion

Akin to MAML.adapt() but for MetaSGD: it updates the model with the learnable per-parameter learning rates.

meta_sgd_update

1
meta_sgd_update(model, lrs=None, grads=None)

Description

Performs a MetaSGD update on model using grads and lrs. The function re-routes the Python object, thus avoiding in-place operations.

NOTE: The model itself is updated in-place (no deepcopy), but the parameters' tensors are not.

Arguments

  • model (Module) - The model to update.
  • lrs (list) - The meta-learned learning rates used to update the model.
  • grads (list, optional, default=None) - A list of gradients for each parameter of the model. If None, will use the gradients in .grad attributes.

Example

1
2
3
4
5
meta = l2l.algorithms.MetaSGD(Model(), lr=1.0)
lrs = [th.ones_like(p) for p in meta.model.parameters()]
model = meta.clone() # The next two lines essentially implement model.adapt(loss)
grads = autograd.grad(loss, model.parameters(), create_graph=True)
meta_sgd_update(model, lrs=lrs, grads)