### Install learn2learn from source Source: https://github.com/learnables/learn2learn/blob/master/docs/tutorials/getting_started.md Install the latest development version of learn2learn by cloning the repository and installing it in editable mode. Ensure Cython is installed first. ```bash pip install -e . ``` -------------------------------- ### Install Cython for source installation Source: https://github.com/learnables/learn2learn/blob/master/docs/tutorials/getting_started.md Install Cython, a prerequisite for building learn2learn from source. This command should be run before installing from source. ```bash pip install cython ``` -------------------------------- ### Run linting on examples Source: https://github.com/learnables/learn2learn/blob/master/docs/tutorials/getting_started.md Runs code linting specifically on the example files within the learn2learn repository. This ensures examples adhere to coding standards. ```bash make lint-examples ``` -------------------------------- ### Install learn2learn using pip Source: https://github.com/learnables/learn2learn/blob/master/docs/tutorials/getting_started.md Install the latest stable version of learn2learn from PyPI. This command ensures you have the most recent release available. ```bash pip install -U learn2learn ``` -------------------------------- ### Initialize Task Description with All Dataset Samples Source: https://github.com/learnables/learn2learn/blob/master/docs/tutorials/task_transform_tutorial/transform_tutorial.md This method loads all samples from the dataset into the task description when no prior description exists. It's a foundational step for transforms like NWays and KShots when starting with the full dataset. ```python def new_task(self): n = len(self.dataset) task_description = [None] * n # generate an empty matrix for i in range(n): task_description[i] = DataDescription(i) # Add samples wrapped around DataDescription to the list. return task_description ``` -------------------------------- ### Clean previous installation Source: https://github.com/learnables/learn2learn/blob/master/docs/tutorials/getting_started.md Removes previous build artifacts and installations of learn2learn. Use this command to ensure a clean state before rebuilding. ```bash make clean ``` -------------------------------- ### Meta-Descent with LearnableOptimizer Source: https://github.com/learnables/learn2learn/blob/master/docs/index.md Illustrates meta-descent using the LearnableOptimizer to learn optimization algorithms. This example shows how to initialize the optimizer, compute loss, backpropagate, and step both the meta-optimizer and the inner model. ```python 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 metaopt metaopt.step() # update linear ``` -------------------------------- ### Run Hypergradient MNIST Example Source: https://github.com/learnables/learn2learn/blob/master/docs/examples/optim.md Execute the hypergradient descent script for MNIST. Note that parameters are not finely tuned. ```shell python examples/optimization/hypergrad_mnist.py ``` -------------------------------- ### Parallelize Meta-RL Environments Source: https://github.com/learnables/learn2learn/blob/master/docs/index.md Set up and use parallelized meta-environments for Reinforcement Learning. This example demonstrates using AsyncVectorEnv for parallel execution and ActionSpaceScaler for environment utilities. ```python def make_env(): env = l2l.gym.HalfCheetahForwardBackwardEnv() env = cherry.envs.ActionSpaceScaler(env) return env env = l2l.gym.AsyncVectorEnv([make_env for _ in range(16)]) # uses 16 threads for task_config in env.sample_tasks(20): env.set_task(task) # all threads receive the same task state = env.reset() # use standard Gym API action = my_policy(env) env.step(action) ``` -------------------------------- ### Build and serve documentation locally Source: https://github.com/learnables/learn2learn/blob/master/docs/tutorials/getting_started.md Builds the learn2learn documentation and serves the website locally. This command is useful for previewing documentation changes. ```bash make docs ``` -------------------------------- ### KShots: Initialize Task Description with All Dataset Samples Source: https://github.com/learnables/learn2learn/blob/master/docs/tutorials/task_transform_tutorial/transform_tutorial.md This section demonstrates initializing the task description with all samples from the dataset when the initial task description is None. This is the first step for the KShots transform. ```python # Step 1 : Task description is None # Load all samples in dataset task_description = [None] * len(dataset) for i in range(len(dataset)): task_description[i] = DataDescription(i) ``` -------------------------------- ### Build learn2learn in place Source: https://github.com/learnables/learn2learn/blob/master/docs/tutorials/getting_started.md Builds the learn2learn library in the current directory, useful for development. This command compiles the necessary components. ```bash make build ``` -------------------------------- ### Run light testing suite Source: https://github.com/learnables/learn2learn/blob/master/docs/tutorials/getting_started.md Executes a subset of tests for learn2learn, similar to those run in CI environments. This provides a quick check of core functionality. ```bash make tests ``` -------------------------------- ### Meta-Descent with LearnableOptimizer Source: https://github.com/learnables/learn2learn/blob/master/README.md Illustrates meta-descent using the LearnableOptimizer to learn optimization algorithms. This snippet shows how to initialize and step both the meta-optimizer and the learned optimizer. ```python 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() # u000dupdate metaopt metaopt.step() # update linear ``` -------------------------------- ### Set Up Optimizer and Loss Function Source: https://github.com/learnables/learn2learn/blob/master/docs/tutorials/anil_tutorial/ANIL_tutorial.md Configures the Adam optimizer with meta-learning rate and defines the cross-entropy loss function for training. ```python all_parameters = list(features.parameters()) + list(head.parameters()) optimizer = torch.optim.Adam(all_parameters, lr=meta_lr) loss = nn.CrossEntropyLoss(reduction='mean') ``` -------------------------------- ### Run Supervised Pretraining Script Source: https://github.com/learnables/learn2learn/blob/master/docs/examples/vision.md Execute the supervised pretraining script to reproduce baseline results. Ensure you have manually edited the script as needed for your specific configuration. ```shell python examples/vision/supervised_pretraining.py ``` -------------------------------- ### Loading and Transforming Dataset Source: https://github.com/learnables/learn2learn/blob/master/docs/tutorials/task_transform_tutorial/transform_tutorial.md Downloads a dataset (e.g., FullOmniglot) and applies specified data transformations to each sample. Ensure the root directory is correctly set for dataset storage. ```python dataset = l2l.vision.datasets.FullOmniglot(root='~\data', transform=data_transform, download=True) # Load your custom dataset ``` -------------------------------- ### Few-Shot Learning with MAML Wrapper Source: https://github.com/learnables/learn2learn/blob/master/docs/index.md Demonstrates how to use the MAML wrapper for few-shot learning tasks. This snippet shows the training loop, including cloning the model, computing adaptation loss, adapting the task model, computing evaluation loss, and updating the meta-optimizer. ```python maml = l2l.algorithms.MAML(model, lr=0.1) opt = torch.optim.SGD(maml.parameters(), lr=0.001) for iteration in range(10): opt.zero_grad() task_model = maml.clone() # torch.clone() for nn.Modules adaptation_loss = compute_loss(task_model) task_model.adapt(adaptation_loss) # computes gradient, update task_model in-place evaluation_loss = compute_loss(task_model) evaluation_loss.backward() # gradients w.r.t. maml.parameters() opt.step() ``` -------------------------------- ### Partitioning Data for K-Shot Learning Source: https://github.com/learnables/learn2learn/blob/master/docs/tutorials/task_transform_tutorial/transform_tutorial.md Partitions data and labels into support and query sets for k-shot learning scenarios. This utility is helpful for setting up few-shot experiments. ```python # k-shot learning scenario (adapt_data, adapt_labels), (eval_data, eval_labels) = partition_task(data, labels, shots=k) ``` -------------------------------- ### Create Custom Few-Shot Dataset Source: https://github.com/learnables/learn2learn/blob/master/README.md Demonstrates how to create a custom few-shot dataset using learn2learn's data transforms. This is useful for meta-learning tasks where standard datasets are not sufficient. ```python dataset = l2l.data.MetaDataset(MyDataset()) # any PyTorch dataset transforms = [ # Easy to define your own transform l2l.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) for task in taskset: X, y = task # Meta-train on the task ``` -------------------------------- ### Create and Prepare FC100 Dataset Source: https://github.com/learnables/learn2learn/blob/master/docs/tutorials/anil_tutorial/ANIL_tutorial.md Obtains and prepares the FC100 dataset for meta-learning. Converts images to tensors and wraps the dataset for task generation. ```python train_dataset = l2l.vision.datasets.FC100(root='~/data', transform=tv.transforms.ToTensor(), mode='train') train_dataset = l2l.data.MetaDataset(train_dataset) ``` -------------------------------- ### KShots: Create Class to Data Mapping Source: https://github.com/learnables/learn2learn/blob/master/docs/tutorials/task_transform_tutorial/transform_tutorial.md This code creates a dictionary mapping each class label to a list of its corresponding samples (DataDescription objects) from the current task description. This is essential for sampling K shots per class. ```python # Step 2 : Create a dictionary that stores labels and their corresponding samples class_to_data = defaultdict(list) for dd in task_description: cls = indices_to_labels[dd.index] class_to_data[cls].append(dd) ``` -------------------------------- ### Run Prototypical Networks on mini-ImageNet (5-shot) Source: https://github.com/learnables/learn2learn/blob/master/docs/examples/vision.md Execute the Prototypical Networks implementation on the mini-ImageNet dataset for 5-shot learning. This script provides training and testing code. ```shell python examples/vision/protonet_miniimagenet.py --shot 5 --train-way 20 ``` -------------------------------- ### LightningMetaOptNet Source: https://github.com/learnables/learn2learn/blob/master/docs/docs/learn2learn.algorithms.md PyTorch Lightning implementation of Meta-OptNet. ```APIDOC ## LightningMetaOptNet ### Description This class implements Meta-OptNet using PyTorch Lightning, enabling users to leverage this meta-learning approach within a structured and efficient training environment. ``` -------------------------------- ### Run extensive testing suite Source: https://github.com/learnables/learn2learn/blob/master/docs/tutorials/getting_started.md Runs a comprehensive suite of tests for learn2learn, which may take a significant amount of time. This is useful for thorough validation. ```bash make alltests ``` -------------------------------- ### Creating UnionMetaDataset Source: https://github.com/learnables/learn2learn/blob/master/docs/tutorials/task_transform_tutorial/transform_tutorial.md Demonstrates how to wrap multiple datasets using UnionMetaDataset to merge them into a single dataset. This is useful for sampling heterogenous tasks. ```python import learn2learn as l2l from learn2learn.data.meta_dataset import MetaDataset from learn2learn.vision.datasets import FullOmniglot, FC100, CIFARFS data_transform = None # Placeholder for actual transform dataset_omniglot = FullOmniglot(root='~\data', transform=data_transform, download=True) dataset_omniglot = MetaDataset(dataset_omniglot) # If not wrapped, UnionMetaDataset takes care of it. dataset_fc100 = FC100(root='~\data', transform=data_transform, download=True) dataset_fc100 = MetaDataset(dataset_fc100) dataset_cifarfs = CIFARFS(root='~\data', transform=data_transform, download=True) dataset_cifarfs = MetaDataset(dataset_cifarfs) datasets = [dataset_omniglot, dataset_fc100, dataset_cifarfs] union = l2l.data.UnionMetaDataset(datasets) # a list of datasets is fed as argument ``` -------------------------------- ### Sample Task Description Generation Source: https://github.com/learnables/learn2learn/blob/master/docs/tutorials/task_transform_tutorial/transform_tutorial.md Generates a sample task description by applying a sequence of transforms. This function is useful for understanding the flow of data transformation. ```python def sample_task_description(self): # Samples a new task description. description = None # initialize description as None at the start if callable(self.task_transforms): return self.task_transforms(description) for transform in self.task_transforms: # iterate on the transfroms list [NWays, Kshots, LoadData, ...] description = transform(description) # use the description generated by the previous transform for the current transform return description # A description modified by all the transforms present in the list. ``` -------------------------------- ### Run Prototypical Networks on mini-ImageNet (1-shot) Source: https://github.com/learnables/learn2learn/blob/master/docs/examples/vision.md Execute the Prototypical Networks implementation on the mini-ImageNet dataset for 1-shot learning. This script provides training and testing code. ```shell python examples/vision/protonet_miniimagenet.py ``` -------------------------------- ### learn2learn.vision.datasets.Quickdraw Source: https://github.com/learnables/learn2learn/blob/master/docs/docs/learn2learn.vision.md Documentation for the Quickdraw dataset. ```APIDOC ## learn2learn.vision.datasets.Quickdraw ### Description Represents the Quickdraw dataset. ### Usage ```python import learn2learn as l2l dataset = l2l.vision.datasets.Quickdraw(root='path/to/data') ``` ``` -------------------------------- ### Instantiate Feature Extractor and MAML Head Source: https://github.com/learnables/learn2learn/blob/master/docs/tutorials/anil_tutorial/ANIL_tutorial.md Sets up the model architecture, including a convolutional base for feature extraction and a linear head wrapped with the MAML algorithm for adaptation. ```python features = l2l.vision.models.ConvBase(output_size=64, channels=3, max_pool=True) features = torch.nn.Sequential(features, Lambda(lambda x: x.view(-1, 256))) features.to(device) head = torch.nn.Linear(256, ways) head = l2l.algorithms.MAML(head, lr=fast_lr) head.to(device) ``` -------------------------------- ### Building Indexing Dictionaries for MetaDataset Source: https://github.com/learnables/learn2learn/blob/master/docs/tutorials/task_transform_tutorial/transform_tutorial.md Constructs dictionaries for efficient sample indexing within MetaDataset. `labels_to_indices` maps labels to sample indices, and `indices_to_labels` maps indices to their corresponding labels. This is crucial for optimizing task generation. ```python from collections import defaultdict labels_to_indices = defaultdict(list) # each label will store list of sample indices indices_to_labels = defaultdict(int) # each index will store corresponding sample's label for i in range(len(dataset)): # iterate over all samples label = dataset[i][1] # load label if hasattr(label, 'item'): label = dataset[i][1].item() # if label is a Tensor, then take get the scalar value labels_to_indices[label].append(i) # append sample's index to the given class indices_to_labels[i] = label # assign label to the given index ``` -------------------------------- ### LightningPrototypicalNetworks Source: https://github.com/learnables/learn2learn/blob/master/docs/docs/learn2learn.algorithms.md PyTorch Lightning implementation of Prototypical Networks. ```APIDOC ## LightningPrototypicalNetworks ### Description Provides a PyTorch Lightning implementation of Prototypical Networks, a popular meta-learning algorithm, optimized for use with the PyTorch Lightning framework. ``` -------------------------------- ### Cached Task Description Method Source: https://github.com/learnables/learn2learn/blob/master/docs/tutorials/task_transform_tutorial/transform_tutorial.md Demonstrates the method for retrieving a task description from a cache if it exists for a given task index. ```python # Method 1: Cached Description # If there's a cached description for the given index, the `task_description` is assigned the cached description. sampled_descriptions = {} # a dictionary for caching descriptions if i not in sampled_descriptions: # i is the index of task between [0, num_tasks] sampled_descriptions[i] = 'Call Method 2' # call sample_task_description() task_description = sampled_descriptions[i] ``` -------------------------------- ### Accessing Vision Dataset Benchmarks Source: https://github.com/learnables/learn2learn/blob/master/docs/tutorials/task_transform_tutorial/transform_tutorial.md Shows how to retrieve tasksets for common computer vision datasets like Omniglot, CIFAR-FS, and Mini-ImageNet. These tasksets can then be sampled for training, validation, and testing. ```python # data_name: 'omniglot', 'fc100', 'cifarfs', 'mini-imagenet', 'tiered-imagenet' # N: No of ways, K: No of shots tasksets = l2l.vision.benchmarks.get_tasksets(data_name, train_ways=N, train_samples=2*K, test_ways=N, test_samples=2*K, num_tasks=-1, root='~/data') X1, y1 = tasksets.train.sample() X2, y2 = tasksets.validation.sample() X3, y3 = tasksets.test.sample() ``` -------------------------------- ### Applying Task Transforms Sequentially Source: https://github.com/learnables/learn2learn/blob/master/docs/tutorials/task_transform_tutorial/transform_tutorial.md Illustrates how a series of task transformations are applied sequentially to a dataset description, modifying the number of samples and their properties. ```python # Transforms: [NWays, KShots, LoadData, RemapLabels, ConsecutiveLabels, RandomClassRotation] description = None for transform in transforms: description = transform(description) print(len(description), description) # Initially, there are all samples present in the dataset # NWays chooses N classes (5 in our case), and samples all the datapoints belonging to only these N classes -> 100 samples (as each class has 20 samples) # Next, KShot chooses K samples (1 samples per class in our case) from each of these N classes -> thus reducing the total samples in the task_description to 5 # And rest of the task transforms do other specicial transformations on the samples without changing the number of samples present in the description ``` -------------------------------- ### LightningMAML Source: https://github.com/learnables/learn2learn/blob/master/docs/docs/learn2learn.algorithms.md PyTorch Lightning implementation of the MAML algorithm. ```APIDOC ## LightningMAML ### Description This class provides a PyTorch Lightning implementation of the MAML algorithm, integrating meta-learning capabilities with the PyTorch Lightning framework for streamlined training and experimentation. ``` -------------------------------- ### On-the-fly Task Description Generation Source: https://github.com/learnables/learn2learn/blob/master/docs/tutorials/task_transform_tutorial/transform_tutorial.md Represents the alternative method for generating a task description when a cached version is not available or when `num_tasks` is -1. ```python # Method 2: If method 1 fails, or num_tasks = -1 ``` -------------------------------- ### learn2learn.vision.benchmarks.list_tasksets Source: https://github.com/learnables/learn2learn/blob/master/docs/docs/learn2learn.vision.md Function to list available tasksets for benchmarks. ```APIDOC ## learn2learn.vision.benchmarks.list_tasksets ### Description Lists the available tasksets for meta-learning benchmarks. ### Usage ```python import learn2learn as l2l tasksets = l2l.vision.benchmarks.list_tasksets() print(tasksets) ``` ``` -------------------------------- ### learn2learn.vision.datasets.FullOmniglot Source: https://github.com/learnables/learn2learn/blob/master/docs/docs/learn2learn.vision.md Documentation for the FullOmniglot dataset. ```APIDOC ## learn2learn.vision.datasets.FullOmniglot ### Description Represents the Full Omniglot dataset. ### Usage ```python import learn2learn as l2l dataset = l2l.vision.datasets.FullOmniglot(root='path/to/data') ``` ``` -------------------------------- ### learn2learn.vision.datasets.MiniImagenet Source: https://github.com/learnables/learn2learn/blob/master/docs/docs/learn2learn.vision.md Documentation for the MiniImagenet dataset. ```APIDOC ## learn2learn.vision.datasets.MiniImagenet ### Description Represents the MiniImagenet dataset. ### Usage ```python import learn2learn as l2l dataset = l2l.vision.datasets.MiniImagenet(root='path/to/data') ``` ``` -------------------------------- ### Run linting on codebase Source: https://github.com/learnables/learn2learn/blob/master/docs/tutorials/getting_started.md Executes code linting on the learn2learn codebase to check for style and potential errors. This helps maintain code quality. ```bash make lint ``` -------------------------------- ### Create Custom Few-Shot Dataset Source: https://github.com/learnables/learn2learn/blob/master/docs/index.md Define and use a custom PyTorch dataset for few-shot learning tasks. This involves creating a MetaDataset and applying transforms like NWays, KShots, and LoadData. ```python dataset = l2l.data.MetaDataset(MyDataset()) # any PyTorch dataset transforms = [ # Easy to define your own transform l2l.data.transforms.NWays(dataset, n=5), l2l.data.transforms.KShots(dataset, k=1), l2l.data.transforms.LoadData(dataset), ] taskset = TaskDataset(dataset, transforms, num_tasks=20000) for task in taskset: X, y = task # Meta-train on the task ``` -------------------------------- ### learn2learn.vision.models.OmniglotFC Source: https://github.com/learnables/learn2learn/blob/master/docs/docs/learn2learn.vision.md Documentation for the OmniglotFC model, a fully connected network architecture. ```APIDOC ## learn2learn.vision.models.OmniglotFC ### Description Represents the Omniglot Fully Connected model architecture. ### Usage ```python import learn2learn as l2l model = l2l.vision.models.OmniglotFC() ``` ``` -------------------------------- ### MetaModule Source: https://github.com/learnables/learn2learn/blob/master/docs/docs/learn2learn.nn.md Represents a meta-learning module. The `module` attribute is accessible. ```APIDOC ## MetaModule ### Description Represents a meta-learning module. This class is part of the learn2learn.nn module. ### Attributes - **module**: Access to the underlying module. ``` -------------------------------- ### Import Core learn2learn Modules Source: https://github.com/learnables/learn2learn/blob/master/docs/tutorials/task_transform_tutorial/transform_tutorial.md Imports essential modules from learn2learn and torchvision for data handling, task generation, and vision-specific transforms. These are typically needed at the beginning of a learn2learn task generation script. ```python import os, random, pickle, itertools, copy 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 from collections import defaultdict ``` -------------------------------- ### NWays: Sample Data from N Random Classes in Existing Task Description Source: https://github.com/learnables/learn2learn/blob/master/docs/tutorials/task_transform_tutorial/transform_tutorial.md Given an existing task description, this function first identifies unique labels, then randomly samples N of them, and finally collects all samples belonging to these N classes. This forms the new task description. ```python # Step 3 : Sample indices belonging the list of randomly choosen classes def n_samples(): result = [] classes = random.sample(unique_labels(), k=5) for dd in task_description: if indices_to_labels[dd.index] in classes: result.append(dd) # adds all the indices belonging to the 5 randomly choosen classes return result # return new task description ``` -------------------------------- ### LightningANIL Source: https://github.com/learnables/learn2learn/blob/master/docs/docs/learn2learn.algorithms.md PyTorch Lightning implementation of the ANIL algorithm. ```APIDOC ## LightningANIL ### Description This class offers a PyTorch Lightning implementation of the ANIL (Another Interesting Meta-Learning Algorithm) algorithm, designed for efficient meta-learning within the PyTorch Lightning ecosystem. ``` -------------------------------- ### Run ANIL on FC100 Source: https://github.com/learnables/learn2learn/blob/master/docs/examples/vision.md Execute the ANIL implementation on the FC100 dataset. Manually edit the script before running. ```shell python examples/vision/anil_fc100.py ``` -------------------------------- ### GBML Source: https://github.com/learnables/learn2learn/blob/master/docs/docs/learn2learn.algorithms.md Gradient-Based Meta-Learning (GBML) algorithm interface. Supports adaptation and cloning. ```APIDOC ## GBML ### Description Provides the Gradient-Based Meta-Learning (GBML) algorithm interface. Key functionalities include adapting the model to new tasks and cloning the model. ### Methods - **adapt**: Adapts the model to a new task. - **clone**: Creates a copy of the model. ``` -------------------------------- ### learn2learn.vision.datasets.VGGFlower102 Source: https://github.com/learnables/learn2learn/blob/master/docs/docs/learn2learn.vision.md Documentation for the VGGFlower102 dataset. ```APIDOC ## learn2learn.vision.datasets.VGGFlower102 ### Description Represents the VGG Flower 102 dataset. ### Usage ```python import learn2learn as l2l dataset = l2l.vision.datasets.VGGFlower102(root='path/to/data') ``` ``` -------------------------------- ### MetaSGD Source: https://github.com/learnables/learn2learn/blob/master/docs/docs/learn2learn.algorithms.md Meta-SGD algorithm interface. Allows for task adaptation and model cloning. ```APIDOC ## MetaSGD ### Description Provides the Meta-SGD algorithm interface. This algorithm supports adapting the model to specific tasks and cloning the model for reuse. ### Methods - **adapt**: Adapts the model to a new task. - **clone**: Creates a copy of the model. ``` -------------------------------- ### Scale Source: https://github.com/learnables/learn2learn/blob/master/docs/docs/learn2learn.nn.md A module for scaling. No specific methods are detailed in the source. ```APIDOC ## Scale ### Description A module used for scaling operations within the learn2learn.nn module. ``` -------------------------------- ### Data Preprocessing with Compose Source: https://github.com/learnables/learn2learn/blob/master/docs/tutorials/task_transform_tutorial/transform_tutorial.md Applies a sequence of transformations to input data, such as resizing images and converting them to tensors. This is useful for preparing raw data for machine learning models. ```python data_transform = tv.transforms.Compose([ tv.transforms.Resize((28, 28), interpolation=LANCZOS), tv.transforms.ToTensor() ]) ``` -------------------------------- ### Creating and Sampling from Taskset Source: https://github.com/learnables/learn2learn/blob/master/docs/tutorials/task_transform_tutorial/transform_tutorial.md Generates a set of tasks using the dataset and defined task transforms. Tasks can be sampled individually or iterated over to obtain batches of data and labels. ```python taskset = l2l.data.Taskset(dataset=omniglot, task_transforms=transforms, num_tasks=10) # Creates sets of tasks from the dataset # Now sample a task from the taskset X, y = taskset.sample() # or, you can also sample this way: for task in taskset: X, y = task print(X.shape) ``` -------------------------------- ### Wrapping Dataset with MetaDataset Source: https://github.com/learnables/learn2learn/blob/master/docs/tutorials/task_transform_tutorial/transform_tutorial.md Wraps a dataset with MetaDataset to enable fast indexing of samples. This optimizes the process of accessing data, especially for large datasets, by reducing time complexity. ```python omniglot = l2l.data.MetaDataset(dataset) # Use MetaDataset to do fast indexing of samples ``` -------------------------------- ### Run MAML on Omniglot Source: https://github.com/learnables/learn2learn/blob/master/docs/examples/vision.md Execute the MAML implementation on the Omniglot dataset using a fully-connected network. This script provides the training code. ```shell python examples/vision/maml_omniglot.py ``` -------------------------------- ### learn2learn.vision.datasets.CIFARFS Source: https://github.com/learnables/learn2learn/blob/master/docs/docs/learn2learn.vision.md Documentation for the CIFARFS dataset. ```APIDOC ## learn2learn.vision.datasets.CIFARFS ### Description Represents the CIFAR-FS dataset. ### Usage ```python import learn2learn as l2l dataset = l2l.vision.datasets.CIFARFS(root='path/to/data') ``` ``` -------------------------------- ### learn2learn.vision.datasets.DescribableTextures Source: https://github.com/learnables/learn2learn/blob/master/docs/docs/learn2learn.vision.md Documentation for the Describable Textures dataset. ```APIDOC ## learn2learn.vision.datasets.DescribableTextures ### Description Represents the Describable Textures dataset. ### Usage ```python import learn2learn as l2l dataset = l2l.vision.datasets.DescribableTextures(root='path/to/data') ``` ``` -------------------------------- ### Run MAML on mini-ImageNet Source: https://github.com/learnables/learn2learn/blob/master/docs/examples/vision.md Execute the MAML implementation on the mini-ImageNet dataset using a standard convolutional network. This script provides the training code. ```shell python examples/vision/maml_miniimagenet.py ``` -------------------------------- ### Loading Bookkept Dataset Source: https://github.com/learnables/learn2learn/blob/master/docs/tutorials/task_transform_tutorial/transform_tutorial.md Loads a dataset that has been previously cached using the bookkeeping utility. This is useful for large datasets to speed up instantiation. ```python import pickle # The variables can be loaded from the saved .pkl file with open('~\data/omniglot-bookkeeping.pkl', 'rb') as f: _bookkeeping = pickle.load(f) # In _bookkeeping, the three attributes are saved in the form of a dictionary labels_to_indices = _bookkeeping['labels_to_indices'] indices_to_labels = _bookkeeping['indices_to_labels'] labels = _bookkeeping['labels'] print('labels_to_indices: label as keys, and list of sample indices as values') print(labels_to_indices) print('\n') print('indices_to_labels: index as key, and corresponding label as value') print(indices_to_labels) ``` -------------------------------- ### learn2learn.vision.datasets.CUBirds200 Source: https://github.com/learnables/learn2learn/blob/master/docs/docs/learn2learn.vision.md Documentation for the CUBirds200 dataset. ```APIDOC ## learn2learn.vision.datasets.CUBirds200 ### Description Represents the CUBirds200 dataset. ### Usage ```python import learn2learn as l2l dataset = l2l.vision.datasets.CUBirds200(root='path/to/data') ``` ``` -------------------------------- ### Task Generation from Data Description Source: https://github.com/learnables/learn2learn/blob/master/docs/tutorials/task_transform_tutorial/transform_tutorial.md Creates a batch of data from a task description by applying specified transforms to each data sample. It utilizes `task_collate` for aggregation. ```python def get_task(self, task_description): # Given a task description, creates the corresponding batch of data. all_data = [] for data_description in task_description: # iterate over all the samples in task description data = data_description.index # loads index of the sample present in the original dataset for transform in data_description.transforms: # There are two task_transforms (LoadData and RemapLabels) that apply transforms on the data # as of now in the learn2learn library. data = transform(data) # applies transform sequentially on the sample data all_data.append(data) return self.task_collate(all_data) # by default makes use of collate.default_colllate ``` -------------------------------- ### Differentiable Optimization with ParameterUpdate Source: https://github.com/learnables/learn2learn/blob/master/docs/index.md Implement and differentiate through PyTorch Module updates using learn2learn's ParameterUpdate utility. This allows for learning custom update functions and differentiating through optimization loops. ```python model = MyModel() transform = l2l.optim.KroneckerTransform(l2l.nn.KroneckerLinear) learned_update = l2l.optim.ParameterUpdate( # learnable update function model.parameters(), transform) clone = l2l.clone_module(model) # torch.clone() for nn.Modules error = loss(clone(X), y) updates = learned_update( # similar API as torch.autograd.grad error, 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() ``` -------------------------------- ### Run Reptile on mini-ImageNet Source: https://github.com/learnables/learn2learn/blob/master/docs/examples/vision.md Execute the Reptile implementation on the mini-ImageNet dataset. Manually edit the script before running. ```shell python examples/vision/reptile_miniimagenet.py ``` -------------------------------- ### learn2learn.vision.datasets.TieredImagenet Source: https://github.com/learnables/learn2learn/blob/master/docs/docs/learn2learn.vision.md Documentation for the TieredImagenet dataset. ```APIDOC ## learn2learn.vision.datasets.TieredImagenet ### Description Represents the TieredImagenet dataset. ### Usage ```python import learn2learn as l2l dataset = l2l.vision.datasets.TieredImagenet(root='path/to/data') ``` ``` -------------------------------- ### learn2learn.vision.datasets.FGVCFungi Source: https://github.com/learnables/learn2learn/blob/master/docs/docs/learn2learn.vision.md Documentation for the FGVCFungi dataset. ```APIDOC ## learn2learn.vision.datasets.FGVCFungi ### Description Represents the FGVC Fungi dataset. ### Usage ```python import learn2learn as l2l dataset = l2l.vision.datasets.FGVCFungi(root='path/to/data') ``` ``` -------------------------------- ### Accessing and Modifying Data in a Dataset Source: https://github.com/learnables/learn2learn/blob/master/docs/tutorials/task_transform_tutorial/transform_tutorial.md Demonstrates how to access specific items within a dataset and modify their properties, such as labels. ```python def get_item(item, datasets, union): ds_count = 0 for dataset in datasets: # [D1, D2] (D1 -> 1600 samples, D2 -> 600 samples) if ds_count + len(dataset) > item: # For D1, condition fails data = list(dataset[item - ds_count]) # searches in the D2's index space data[1] = union.indices_to_labels[item] # changes label from 3 to 22 (20 + 3 - 1) return data ds_count += len(dataset) # Now, ds_count = 1600 get_item(62000, datasets, union)[1] # Returns modified label ``` -------------------------------- ### Define Taskset with Transformations Source: https://github.com/learnables/learn2learn/blob/master/docs/tutorials/anil_tutorial/ANIL_tutorial.md Creates a taskset using specified transformations for meta-learning. This includes sampling data, remapping labels, and ensuring consecutive labeling. ```python train_transforms = [ FusedNWaysKShots(train_dataset, n=ways, k=2*shots), LoadData(train_dataset), RemapLabels(train_dataset), ConsecutiveLabels(train_dataset), ] train_tasks = l2l.data.Taskset(train_dataset, task_transforms=train_transforms, num_tasks=20000) ``` -------------------------------- ### Outer Loop Training Iteration Source: https://github.com/learnables/learn2learn/blob/master/docs/tutorials/anil_tutorial/ANIL_tutorial.md Initiates the outer training loop, sampling tasks and cloning the learner head for adaptation. ```python for iteration in range(iters): ... for task in range(meta_bsz): learner = head.clone() batch = train_tasks.sample() ... ``` -------------------------------- ### MAML Source: https://github.com/learnables/learn2learn/blob/master/docs/docs/learn2learn.algorithms.md Meta-Agnostic Meta-Learning (MAML) algorithm interface. Provides methods for adaptation and cloning. ```APIDOC ## MAML ### Description Provides the Meta-Agnostic Meta-Learning (MAML) algorithm interface. Users can adapt the model to new tasks and clone it for different purposes. ### Methods - **adapt**: Adapts the model to a new task. - **clone**: Creates a copy of the model. ``` -------------------------------- ### learn2learn.vision.datasets.FGVCAircraft Source: https://github.com/learnables/learn2learn/blob/master/docs/docs/learn2learn.vision.md Documentation for the FGVCAircraft dataset. ```APIDOC ## learn2learn.vision.datasets.FGVCAircraft ### Description Represents the FGVC Aircraft dataset. ### Usage ```python import learn2learn as l2l dataset = l2l.vision.datasets.FGVCAircraft(root='path/to/data') ``` ``` -------------------------------- ### ConsecutiveLabels Transform Implementation Source: https://github.com/learnables/learn2learn/blob/master/docs/tutorials/task_transform_tutorial/transform_tutorial.md Re-orders samples in `task_description` based on label order. Recommended to use after `RemapLabels` with `shuffle=True`, or before `RemapLabels` with `shuffle=False` for ordered labels. ```python # How consecutive labels transform is implemented pairs = [(dd, indices_to_labels[dd.index]) for dd in task_description] pairs = sorted(pairs, key=lambda x: x[1]) # sort by labels x[0] : index, x[1] : label print([p[0] for p in pairs]) # Example demonstration toy_dataset = [i for i in range(1000)] # generate a toy dataset toy_indices_to_labels = {} for i in toy_dataset: toy_indices_to_labels[i] = random.randint(0, 99) # generate a toy indices to labels dictionary toy_list = [i for i in range(1000)] # generate a toy samples list toy_task_description = [random.choice(toy_list) for _ in range(10)] # generate a random task description pairs = [(dd, toy_indices_to_labels[dd]) for dd in toy_task_description] pairs = sorted(pairs, key=lambda x: x[1]) # sort the pairs list by using the second element (labels) in the tuple print('\n') print([p[0] for p in pairs]) # prints index (not ordered) print([p[1] for p in pairs]) # prints label (ordered list) ``` -------------------------------- ### learn2learn.vision.datasets.FC100 Source: https://github.com/learnables/learn2learn/blob/master/docs/docs/learn2learn.vision.md Documentation for the FC100 dataset. ```APIDOC ## learn2learn.vision.datasets.FC100 ### Description Represents the FC100 dataset. ### Usage ```python import learn2learn as l2l dataset = l2l.vision.datasets.FC100(root='path/to/data') ``` ``` -------------------------------- ### learn2learn.vision.transforms.RandomClassRotation Source: https://github.com/learnables/learn2learn/blob/master/docs/docs/learn2learn.vision.md Documentation for the RandomClassRotation transform. ```APIDOC ## learn2learn.vision.transforms.RandomClassRotation ### Description Applies a random rotation to class labels. ### Usage ```python import learn2learn as l2l transform = l2l.vision.transforms.RandomClassRotation(num_classes=10) ``` ``` -------------------------------- ### learn2learn.data.transforms.KShots Source: https://github.com/learnables/learn2learn/blob/master/docs/docs/learn2learn.data.md A transform that selects K-shots for tasks. ```APIDOC ## learn2learn.data.transforms.KShots ### Description A transform that selects K-shots for tasks. ``` -------------------------------- ### learn2learn.vision.models.ResNet12 Source: https://github.com/learnables/learn2learn/blob/master/docs/docs/learn2learn.vision.md Documentation for the ResNet12 model, a Residual Network architecture with 12 layers. ```APIDOC ## learn2learn.vision.models.ResNet12 ### Description Represents the ResNet12 model architecture. ### Usage ```python import learn2learn as l2l model = l2l.vision.models.ResNet12() ``` ``` -------------------------------- ### learn2learn.vision.models.WRN28 Source: https://github.com/learnables/learn2learn/blob/master/docs/docs/learn2learn.vision.md Documentation for the WRN28 model, a Wide Residual Network architecture with 28 layers. ```APIDOC ## learn2learn.vision.models.WRN28 ### Description Represents the WRN28 model architecture. ### Usage ```python import learn2learn as l2l model = l2l.vision.models.WRN28() ``` ``` -------------------------------- ### learn2learn.vision.models.OmniglotCNN Source: https://github.com/learnables/learn2learn/blob/master/docs/docs/learn2learn.vision.md Documentation for the OmniglotCNN model, a convolutional neural network architecture. ```APIDOC ## learn2learn.vision.models.OmniglotCNN ### Description Represents the Omniglot Convolutional Neural Network model architecture. ### Usage ```python import learn2learn as l2l model = l2l.vision.models.OmniglotCNN() ``` ``` -------------------------------- ### Average Gradients and Optimize Parameters Source: https://github.com/learnables/learn2learn/blob/master/docs/tutorials/anil_tutorial/ANIL_tutorial.md This snippet shows how to average accumulated gradients across a meta-batch and then update the model parameters using an optimizer. It's typically used at the end of an outer loop iteration in meta-learning. ```python for p in all_parameters: p.grad.data.mul_(1.0 / meta_bsz) optimizer.step() ``` -------------------------------- ### KShots: Sample K Datapoints Per Class Source: https://github.com/learnables/learn2learn/blob/master/docs/tutorials/task_transform_tutorial/transform_tutorial.md This is the final step in the KShots transform, where K samples are drawn from each class. The sampling can be done with or without replacement, based on the specific requirements. ```python # Step 3 : Sample K datapoints from each class with or without replacement ``` -------------------------------- ### learn2learn.vision.benchmarks.get_tasksets Source: https://github.com/learnables/learn2learn/blob/master/docs/docs/learn2learn.vision.md Function to retrieve tasksets for meta-learning benchmarks. ```APIDOC ## learn2learn.vision.benchmarks.get_tasksets ### Description Retrieves tasksets for meta-learning benchmarks. ### Parameters #### Query Parameters - **name** (str) - Required - The name of the taskset to retrieve. - **root** (str) - Optional - The root directory to download/load the dataset from. ### Usage ```python import learn2learn as l2l tasksets = l2l.vision.benchmarks.get_tasksets(name='omniglot-train', root='path/to/data') ``` ``` -------------------------------- ### learn2learn.data.utils.OnDeviceDataset Source: https://github.com/learnables/learn2learn/blob/master/docs/docs/learn2learn.data.md A utility class for datasets that are on a specific device. ```APIDOC ## learn2learn.data.utils.OnDeviceDataset ### Description A utility class for datasets that are on a specific device. ``` -------------------------------- ### ANIL Meta-Training Loop Source: https://github.com/learnables/learn2learn/blob/master/docs/tutorials/anil_tutorial/ANIL_tutorial.md This snippet shows the closing steps of the outer meta-training loop in ANIL, accumulating evaluation errors and accuracies. ```python evaluation_error.backward() meta_train_error += evaluation_error.item() meta_train_accuracy += evaluation_accuracy.item() ... ``` -------------------------------- ### learn2learn.data.UnionMetaDataset Source: https://github.com/learnables/learn2learn/blob/master/docs/docs/learn2learn.data.md Combines multiple meta-datasets into a single one. Supports initialization and item retrieval. ```APIDOC ## learn2learn.data.UnionMetaDataset ### Description Combines multiple meta-datasets into a single one. Supports initialization and item retrieval. ### Methods - `__init__(self, ...)`: Initializes the UnionMetaDataset. - `__getitem__(self, index)`: Retrieves a task from the combined dataset at the specified index. ``` -------------------------------- ### learn2learn.data.MetaDataset Source: https://github.com/learnables/learn2learn/blob/master/docs/docs/learn2learn.data.md Represents a meta-dataset, providing an interface for accessing tasks. Supports initialization and item retrieval. ```APIDOC ## learn2learn.data.MetaDataset ### Description Represents a meta-dataset, providing an interface for accessing tasks. Supports initialization and item retrieval. ### Methods - `__init__(self, ...)`: Initializes the MetaDataset. - `__getitem__(self, index)`: Retrieves a task from the dataset at the specified index. ``` -------------------------------- ### NWays: Sample N Random Classes (No Existing Task Description) Source: https://github.com/learnables/learn2learn/blob/master/docs/tutorials/task_transform_tutorial/transform_tutorial.md When no task description is available, this code randomly samples N classes from the dataset and collects all their corresponding sample indices. This is the first step in the NWays transform. ```python # Step 1 : When no task description is available, sample pool is entire dataset classes = random.sample(labels, k=5) # Randomly sample K classes that will be used for generating the task example = [] for cl in classes: # add only samples belonging to these classes for idx in labels_to_indices[cl]: # Adds the sample index to the task description wrapped in DataDescription object # task_description.append(DataDescription(idx)) # For sake of explaination I am adding the next step example.append(idx) print(example) print("Number of samples:", len(example)) # should be 100 as each class has 20 samples in omniglot ``` -------------------------------- ### RemapLabels Before LoadData Error Output Source: https://github.com/learnables/learn2learn/blob/master/docs/tutorials/task_transform_tutorial/transform_tutorial.md The expected traceback output when RemapLabels is incorrectly placed before LoadData in the transform list. ```shell (output): Traceback (most recent call last): File "", line 13, in print(len(toy_taskset.sample())) # Expected error as RemapLabels is used before LoadData File "learn2learn/data/task_dataset.pyx", line 158, in learn2learn.data.task_dataset.CythonTaskset.sample File "learn2learn/data/task_dataset.pyx", line 173, in learn2learn.data.task_dataset.CythonTaskset.__getitem__ File "learn2learn/data/task_dataset.pyx", line 142, in learn2learn.data.task_dataset.CythonTaskset.get_task File "learn2learn/data/transforms.pyx", line 201, in learn2learn.data.transforms.RemapLabels.remap TypeError: 'int' object is not iterable ``` -------------------------------- ### Filtering Labels with FilteredMetaDataset Source: https://github.com/learnables/learn2learn/blob/master/docs/tutorials/task_transform_tutorial/transform_tutorial.md Shows how to create a FilteredMetaDataset to retain only specific labels from an original MetaDataset. The original label values are preserved. ```python toy_omniglot = l2l.vision.datasets.FullOmniglot(root='~\data', transform=data_transform, download=True) toy_omniglot = l2l.data.MetaDataset(toy_omniglot) filtered = l2l.data.FilteredMetaDataset(toy_omniglot, [4, 8, 2, 1, 9]) print('Original Labels:', len(toy_omniglot.labels)) print('Filtered Labels:', len(filtered.labels)) ```