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}
}