learn2learn

repository·master·Indexed 25 days ago

https://github.com/learnables/learn2learn

A PyTorch-based software library for meta-learning research. It provides low-level utilities, unified interfaces for algorithms like MAML, MetaSGD, and GBML, and standardized benchmarks for vision, reinforcement learning, and optimization. Key features include the LearnableOptimizer for meta-optimization, MetaDataset and Taskset for few-shot data management, and specialized PyTorch Lightning wrappers such as LightningMAML and LightningPrototypicalNetworks.

Tokens
9.4K
Snippets
25
Records
68
Agent score
83%

What's inside learn2learn

  1. Understand the ANIL algorithm

    master

    ANIL (Almost No Inner Loop) is a simplified version of MAML (Model-Agnostic Meta-Learning) designed for feature reuse.

    Unlike MAML, which performs inner-loop adaptation on all network parameters, ANIL keeps the feature extractor constant during the inner loop and only performs gradient descent on the task-specific head. This makes ANIL computationally more efficient while maintaining performance comparable to MAML on tasks like few-shot classification and reinforcement learning.

    Mathematical Difference:

    • MAML Inner Loop: Updates both feature extractor $\theta$ and head $w$.
    • ANIL Inner Loop: Keeps $\theta$ constant ($\theta'i = \theta_i$) and only updates the head $w$ ($\mathbf{w'i = w_i - \beta\nabla{w_i}\mathcal{L}{\tau}(w_i^T\phi_{\theta_i}(x), y)}$).
  2. Cache MetaDataset bookkeeping to disk

    master

    To avoid the high cost of re-indexing large datasets every time you run your code, you can use the _bookkeeping_path attribute. If your input dataset has this attribute defined, MetaDataset will cache the labels_to_indices, indices_to_labels, and labels attributes to a file (typically a .pkl file) for later use.

    To implement this in a custom dataset, assign a path to self._bookkeeping_path during initialization.

  3. Prepare a MetaDataset for task sampling

    master

    To use l2l.data.Taskset, first wrap your standard PyTorch dataset with l2l.data.MetaDataset. This wrapper automatically generates the bookkeeping information required to create new tasks.

    Example workflow:

    1. Load a dataset (e.g., using l2l.vision.datasets.FC100).
    2. Wrap it with l2l.data.MetaDataset.
    3. Pass the MetaDataset to l2l.data.Taskset along with a list of task_transforms.
    train_dataset = l2l.vision.datasets.FC100(root='~/data',
                                              transform=tv.transforms.ToTensor(),
                                              mode='train')
    train_dataset = l2l.data.MetaDataset(train_dataset)
  4. Generate few-shot tasks using the learn2learn pipeline

    master

    To generate customized few-shot tasks (e.g., N-way K-shot) from a dataset, follow this pipeline:

    1. Pre-process input data: Use standard transforms (like torchvision.transforms) for resizing or tensor conversion.
    2. Load the dataset: Initialize your dataset with the pre-processing transforms.
    3. Wrap with MetaDataset: Wrap your dataset in l2l.data.MetaDataset to enable fast indexing of samples.
    4. Define task transforms: Create a list of learn2learn task transforms to define the task structure (e.g., number of ways, shots, label remapping).
    5. Create a Taskset: Initialize l2l.data.Taskset with the MetaDataset and your list of transforms.
    6. Sample tasks: Use .sample() to get a single task or iterate over the Taskset to sample multiple tasks.

    Common task transforms include:

    • NWays(dataset, n): Selects $N$ random classes per task.
    • KShots(dataset, k): Selects $K$ samples per class from the selected $N$ classes.
    • LoadData(dataset): Loads the actual data samples.
    • RemapLabels(dataset): Remaps labels to start from zero.
    • ConsecutiveLabels(dataset): Re-orders samples so they are sorted in consecutive order.
    • RandomClassRotation(dataset, degrees): Randomly rotates vision samples (e.g., [0, 90, 180, 270]).
    import learn2learn as l2l
    import torchvision as tv
    from PIL.Image import LANCZOS
    from learn2learn.data.transforms import NWays, KShots, LoadData, RemapLabels, ConsecutiveLabels
    from learn2learn.vision.transforms import RandomClassRotation
    
    # 1. Apply transforms on input data
    data_transform = tv.transforms.Compose([tv.transforms.Resize((28, 28), interpolation=LANCZOS), tv.transforms.ToTensor()]) 
    
    # 2. Load the dataset
    dataset = l2l.vision.datasets.FullOmniglot(root='~\data', transform=data_transform, download=True)
    
    # 3. Wrap the dataset using MetaDataset for fast indexing
    omniglot = l2l.data.MetaDataset(dataset)
    
    # 4. Specify transforms to be used for generating tasks
    transforms = [
                        NWays(omniglot, 5),  # N = 5
                        KShots(omniglot, 1), # K = 1
                        LoadData(omniglot),
                        RemapLabels(omniglot),
                        ConsecutiveLabels(omniglot),
                        RandomClassRotation(omniglot, [0, 90, 180, 270])
                        ]
    
    # 5. Generate set of tasks
    taskset = l2l.data.Taskset(dataset=omniglot, task_transforms=transforms, num_tasks=10)
    
    # Sample a task
    X, y = taskset.sample()
    print(X.shape)
  5. Development commands for learn2learn

    master

    If you are contributing to or developing learn2learn, you can use the following make commands from the cloned source directory:

    • make build: Builds learn2learn in place.
    • make clean: Cleans previous installations.
    • make lint: Runs linting on the codebase.
    • make lint-examples: Runs linting on the examples.
    • make tests: Runs a light testing suite.
    • make alltests: Runs an extensive, longer testing suite.
    • make docs: Builds the documentation and serves the website locally.
  6. Implement Meta-Optimization using LearnableOptimizer

    master

    You can use learn2learn for meta-optimization or meta-descent tasks. A key component for this is the LearnableOptimizer class, which allows you to optimize hyperparameters (like learning rates) by treating them as learnable parameters rather than relying on analytical gradient formulations.

    In the provided example hypergrad_mnist.py, this is used to implement a version of 'Online Learning Rate Adaptation with Hypergradient Descent' where per-parameter learning rates are adapted instead of a single shared learning rate.

  7. Use PyTorch Lightning wrappers for meta-learning algorithms

    master

    For users integrating meta-learning into PyTorch Lightning workflows, learn2learn provides specialized Lightning modules. These wrappers encapsulate the meta-learning logic (inner and outer loops) into a standard LightningModule structure, making it compatible with Lightning's trainers and distributed training capabilities.

    Supported Lightning wrappers include:

    • LightningMAML
    • LightningANIL
    • LightningPrototypicalNetworks
    • LightningMetaOptNet
  8. Generate tasks using Taskset

    master

    The Taskset module is a core component used to generate tasks from an input dataset. It requires a dataset and a list of task_transforms (e.g., NWays, KShots) which define the structure of the tasks.

    Key Parameters:

    • dataset: The input dataset to sample from.
    • task_transforms: A list of transformations applied sequentially to define the task (e.g., [NWays, KShots, LoadData]).
    • num_tasks: An integer specifying the number of tasks to generate.
      • If set to -1 (default), an infinite number of tasks are generated, and a new task is computed every time you sample.
      • If set to a positive integer N, task descriptions are cached. If you sample more than N tasks, tasks will repeat.

    Task Generation Workflow:

    1. Index Selection: An index is randomly selected from [0, num_tasks).
    2. Description Generation: A task_description (a list of DataDescription objects) is either retrieved from cache or generated by passing a None description through the task_transforms list sequentially.
    3. Data Loading: For each DataDescription in the task, the sample at index is loaded and any sample-specific transforms (like LoadData or RemapLabels) are applied.
    4. Collation: All samples are combined using task_collate (defaults to collate.default_collate).
  9. Run MAML examples for Omniglot and mini-ImageNet

    master

    The MAML wrapper can be used to reproduce Model-Agnostic Meta-Learning (MAML).

    • To obtain First-Order MAML (FOMAML) results, set first_order=True in the MAML wrapper.
    • To use a CNN architecture on Omniglot instead of a fully-connected network, swap OmniglotFC with OmniglotCNN.

    Note: This implementation provides training code only. The original paper uses 5 fast adaptation steps for training and 10 for testing.

    python examples/vision/maml_omniglot.py
    # or
    python examples/vision/maml_miniimagenet.py