Pytorchでの少数のショット学習とメタ学習のための拡張機能とデータローダーのコレクション。 Torchmetaには、 torchvisionとPytorchのDataLoader両方と完全に互換性がある人気のあるメタラーニングベンチマークが含まれています。
MetaModuleと呼ばれるPytorchのModuleの薄い拡張は、特定のメタラーニングモデルの作成を簡素化します(勾配ベースのメタラーニングメソッドなど)。 MetaModuleを使用した例については、MAMLの例を参照してください。 PythonのパッケージマネージャーPIPを使用するか、ソースからTorchmetaをインストールできます。既存のPythonセットアップとの競合を回避するために、 virtualenvを使用して仮想環境で動作することが提案されています。 virtualenvをインストールするには:
pip install --upgrade virtualenv
virtualenv venv
source venv/bin/activateこれは、Torchmetaをインストールするための推奨される方法です。
pip install torchmetaソースからTorchmetaをインストールすることもできます。これは、トーチマに貢献したい場合にお勧めします。
git clone https://github.com/tristandeleu/pytorch-meta.git
cd pytorch-meta
python setup.py install以下のこの最小限の例は、Torchmetaを使用した5ショット5ウェイOmniglotデータセット用のデータローダーを作成する方法を示しています。 Dataloaderは、ランダムに生成されたタスクのバッチをロードし、すべてのサンプルが単一のテンソルに連結されます。その他の例については、例フォルダーを確認してください。
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 )データセットを受信するDataloaderは同じままであることに注意してください。
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}
}