Package: learn2learn
learn2learn is a software library for meta-learning research.learn2learn builds on top of PyTorch to accelerate two aspects of the meta-learning research cycle:fast prototyping, essential in letting researchers quickly try new ideas, andcorrect reproducibility, ensuring that these ideas are evaluated fairly.learn2learn provides low-level utilities and unified interface to create new algorithms and domains, together with high-quality implementations of existing algorithms and standardized benchmarks.
It retains compatibility withtorchvision,torchaudio,torchtext,cherry, and any other PyTorch-based library you might be using.To learn more, see our whitepaper:arXiv:2008.12284Overviewlearn2learn.data:Tasksetand transforms to create few-shot tasks from any PyTorch dataset.learn2learn.vision: Models, datasets, and benchmarks for computer vision and few-shot learning.learn2learn.gym: Environment and utilities for meta-reinforcement learning.learn2learn.algorithms: High-level wrappers for existing meta-learning algorithms.learn2learn.optim: Utilities and algorithms for differentiable optimization and meta-descent.ResourcesWebsite:http://learn2learn.net/Documentation:http://learn2learn.net/docs/learn2learnTutorials:http://learn2learn.net/tutorials/getting_started/Examples:https://github.com/learnables/learn2learn/tree/master/examplesGitHub:https://github.com/learnables/learn2learn/Slack:http://slack.learn2learn.net/Installationpipinstalllearn2learnSnippets & ExamplesThe following snippets provide a sneak peek at the functionalities of learn2learn.High-level WrappersFew-Shot Learning with MAMLFor more algorithms (ProtoNets, ANIL, Meta-SGD, Reptile, Meta-Curvature, KFO) refer to theexamplesfolder.
Most of them can be implemented with with theGBMLwrapper. (documentation).maml=l2l.algorithms.MAML(model,lr=0.1)opt=torch.optim.SGD(maml.parameters(),lr=0.001)foriterationinrange(10):opt.zero_grad()task_model=maml.clone()# torch.clone() for nn.Modulesadaptation_loss=compute_loss(task_model)task_model.adapt(adaptation_loss)# computes gradient, update task_model in-placeevaluation_loss=compute_loss(task_model)evaluation_loss.backward()# gradients w.r.t. maml.parameters()opt.step()Meta-Descent with HypergradientLearn any kind of optimization algorithm with theLearnableOptimizer. (exampleanddocumentation)linear=nn.Linear(784,10)transform=l2l.optim.ModuleTransform(l2l.nn.Scale)metaopt=l2l.optim.LearnableOptimizer(linear,transform,lr=0.01)# metaopt has .step()opt=torch.optim.SGD(metaopt.parameters(),lr=0.001)# metaopt also has .parameters()metaopt.zero_grad()opt.zero_grad()error=loss(linear(X),y)error.backward()opt.step()# update metaoptmetaopt.step()# update linearLearning DomainsCustom Few-Shot DatasetMany standardized datasets (Omniglot, mini-/tiered-ImageNet, FC100, CIFAR-FS) are readily available inlearn2learn.vision.datasets.
(documentation)dataset=l2l.data.MetaDataset(MyDataset())# any PyTorch datasettransforms=[# Easy to define your own transforml2l.data.transforms.NWays(dataset,n=5),l2l.data.transforms.KShots(dataset,k=1),l2l.data.transforms.LoadData(dataset),]taskset=Taskset(dataset,transforms,num_tasks=20000)fortaskintaskset:X,y=task# Meta-train on the taskEnvironments and Utilities for Meta-RLParallelize your own meta-environments withAsyncVectorEnv, or use the standardized ones.
(documentation)defmake_env():env=l2l.gym.HalfCheetahForwardBackwardEnv()env=cherry.envs.ActionSpaceScaler(env)returnenvenv=l2l.gym.AsyncVectorEnv([make_envfor_inrange(16)])# uses 16 threadsfortask_configinenv.sample_tasks(20):env.set_task(task)# all threads receive the same taskstate=env.reset()# use standard Gym APIaction=my_policy(env)env.step(action)Low-Level UtilitiesDifferentiable OptimizationLearn and differentiate through updates of PyTorch Modules.
(documentation)model=MyModel()transform=l2l.optim.KroneckerTransform(l2l.nn.KroneckerLinear)learned_update=l2l.optim.ParameterUpdate(# learnable update functionmodel.parameters(),transform)clone=l2l.clone_module(model)# torch.clone() for nn.Moduleserror=loss(clone(X),y)updates=learned_update(# similar API as torch.autograd.graderror,clone.parameters(),create_graph=True,)l2l.update_module(clone,updates=updates)loss(clone(X),y).backward()# Gradients w.r.t model.parameters() and learned_update.parameters()ChangelogA human-readable changelog is available in theCHANGELOG.mdfile.CitationTo cite thelearn2learnrepository in your academic publications, please use the following reference.Arnold, Sebastien M. R., Praateek Mahajan, Debajyoti Datta, Ian Bunner, and Konstantinos Saitas Zarkias. 2020. “learn2learn: A Library for Meta-Learning Research.” arXiv [cs.LG].http://arxiv.org/abs/2008.12284.You can also use the following Bibtex entry.@article{Arnold2020-ss,title="learn2learn: A Library for {Meta-Learning} Research",author="Arnold, S{\'e}bastien M R and Mahajan, Praateek and Datta,Debajyoti and Bunner, Ian and Zarkias, Konstantinos Saitas",month=aug,year=2020,url="http://arxiv.org/abs/2008.12284",archivePrefix="arXiv",primaryClass="cs.LG",eprint="2008.12284"}Acknowledgements & FriendsTorchMetais similar library, with a focus on datasets for supervised meta-learning.higheris a PyTorch library that enables differentiating through optimization inner-loops. While they monkey-patchnn.Moduleto be stateless, learn2learn retains the stateful PyTorch look-and-feel. For more information, refer totheir ArXiv paper.We are thankful to the following open-source implementations which helped guide the design of learn2learn:Tristan Deleu'spytorch-maml-rlJonas Rothfuss'ProMPKwonjoon Lee'sMetaOptNetHan-Jia Ye's and Hexiang Hu'sFEAT