pytorch meta
1.0.0
在Pytorch中,用于几次学习和元学习的扩展程序和数据加载器的集合。 Torchmeta包含流行的元学习基准测试,与torchvision和Pytorch的DataLoader完全兼容。
Module的薄扩展名为MetaModule ,简化了某些元学习模型的创建(例如基于梯度的元学习方法)。有关使用MetaModule的示例,请参见MAML示例。 您可以使用Python的软件包管理器PIP或从源来安装Torchmeta。为了避免与您现有的Python设置发生任何冲突,建议使用virtualenv在虚拟环境中起作用。安装virtualenv :
pip install --upgrade virtualenv
virtualenv venv
source venv/bin/activate这是安装火炬的推荐方法:
pip install torchmeta您也可以从源安装Torchmeta。如果您想为Torchmeta做出贡献,建议这样做。
git clone https://github.com/tristandeleu/pytorch-meta.git
cd pytorch-meta
python setup.py install下面的这个最小示例显示了如何使用Torchmeta创建5-Shot 5-Way Omniglot数据集的数据加载器。数据加载器加载一批随机生成的任务,所有样品都将其串联成一个张量。有关更多示例,请检查示例文件夹。
from torchmeta . datasets . helpers import omniglot
from torchmeta . utils . data import BatchMetaDataLoader
dataset = omniglot ( "data" , ways = 5 , shots = 5 , test_shots = 15 , meta_train = True , download = True )
dataloader = BatchMetaDataLoader ( dataset , batch_size = 16 , num_workers = 4 )
for batch in dataloader :
train_inputs , train_targets = batch [ "train" ]
print ( 'Train inputs shape: {0}' . format ( train_inputs . shape )) # (16, 25, 1, 28, 28)
print ( 'Train targets shape: {0}' . format ( train_targets . shape )) # (16, 25)
test_inputs , test_targets = batch [ "test" ]
print ( 'Test inputs shape: {0}' . format ( test_inputs . shape )) # (16, 75, 1, 28, 28)
print ( 'Test targets shape: {0}' . format ( test_targets . shape )) # (16, 75) 助手功能仅适用于一些可用的数据集。但是,所有这些都可以通过Torchmeta提供的统一接口获得。上面定义的变量dataset集等效于以下
from torchmeta . datasets import Omniglot
from torchmeta . transforms import Categorical , ClassSplitter , Rotation
from torchvision . transforms import Compose , Resize , ToTensor
from torchmeta . utils . data import BatchMetaDataLoader
dataset = Omniglot ( "data" ,
# Number of ways
num_classes_per_task = 5 ,
# Resize the images to 28x28 and converts them to PyTorch tensors (from Torchvision)
transform = Compose ([ Resize ( 28 ), ToTensor ()]),
# Transform the labels to integers (e.g. ("Glagolitic/character01", "Sanskrit/character14", ...) to (0, 1, ...))
target_transform = Categorical ( num_classes = 5 ),
# Creates new virtual classes with rotated versions of the images (from Santoro et al., 2016)
class_augmentations = [ Rotation ([ 90 , 180 , 270 ])],
meta_train = True ,
download = True )
dataset = ClassSplitter ( dataset , shuffle = True , num_train_per_class = 5 , num_test_per_class = 15 )
dataloader = BatchMetaDataLoader ( dataset , batch_size = 16 , num_workers = 4 )请注意,接收数据集的数据加载程序保持不变。
Tristan Deleu,TobiasWürfl,Mandana Samiei,Joseph Paul Cohen和Yoshua Bengio。 Torchmeta:Pytorch的元学习库,2019 [Arxiv]
如果要引用Torchmeta,请使用以下Bibtex条目:
@misc{deleu2019torchmeta,
title={{Torchmeta: A Meta-Learning library for PyTorch}},
author={Deleu, Tristan and W"urfl, Tobias and Samiei, Mandana and Cohen, Joseph Paul and Bengio, Yoshua},
year={2019},
url={https://arxiv.org/abs/1909.06576},
note={Available at: https://github.com/tristandeleu/pytorch-meta}
}