



PytorchをラップするScikit-Learn互換性のあるニューラルネットワークライブラリ。
より詳細な例を見るには、こちらをご覧ください。
import numpy as np
from sklearn . datasets import make_classification
from torch import nn
from skorch import NeuralNetClassifier
X , y = make_classification ( 1000 , 20 , n_informative = 10 , random_state = 0 )
X = X . astype ( np . float32 )
y = y . astype ( np . int64 )
class MyModule ( nn . Module ):
def __init__ ( self , num_units = 10 , nonlin = nn . ReLU ()):
super (). __init__ ()
self . dense0 = nn . Linear ( 20 , num_units )
self . nonlin = nonlin
self . dropout = nn . Dropout ( 0.5 )
self . dense1 = nn . Linear ( num_units , num_units )
self . output = nn . Linear ( num_units , 2 )
self . softmax = nn . Softmax ( dim = - 1 )
def forward ( self , X , ** kwargs ):
X = self . nonlin ( self . dense0 ( X ))
X = self . dropout ( X )
X = self . nonlin ( self . dense1 ( X ))
X = self . softmax ( self . output ( X ))
return X
net = NeuralNetClassifier (
MyModule ,
max_epochs = 10 ,
lr = 0.1 ,
# Shuffle training data on each epoch
iterator_train__shuffle = True ,
)
net . fit ( X , y )
y_proba = net . predict_proba ( X )Sklearnパイプラインで:
from sklearn . pipeline import Pipeline
from sklearn . preprocessing import StandardScaler
pipe = Pipeline ([
( 'scale' , StandardScaler ()),
( 'net' , net ),
])
pipe . fit ( X , y )
y_proba = pipe . predict_proba ( X )グリッド検索で:
from sklearn . model_selection import GridSearchCV
# deactivate skorch-internal train-valid split and verbose logging
net . set_params ( train_split = False , verbose = 0 )
params = {
'lr' : [ 0.01 , 0.02 ],
'max_epochs' : [ 10 , 20 ],
'module__num_units' : [ 10 , 20 ],
}
gs = GridSearchCV ( net , params , refit = False , cv = 3 , scoring = 'accuracy' , verbose = 2 )
gs . fit ( X , y )
print ( "best score: {:.3f}, best params: {}" . format ( gs . best_score_ , gs . best_params_ ))Skorchはまた、多くの便利な機能を提供します。
SkorchにはPython 3.9以上が必要です。
動作するコンドラのインストールが必要です。ここからシステムの正しいミニコンダを入手してください。
Skorchをインストールするには、Conda-Forgeチャネルを使用する必要があります。
conda install -c conda-forge skorchConda Virtual環境を使用することをお勧めします。
注:Condaチャンネルは、Skorchメンテナーによって管理されていません。詳細については、こちらをご覧ください。
PIPでインストールするには、実行してください。
python -m pip install -U skorch繰り返しますが、これには仮想環境を使用することをお勧めします。
Skorchに最新の追加を使用したり、開発を支援したりする場合は、ScorchからScorchをインストールする必要があります。
Condaを使用してソースからSkorchをインストールするには、次のように進みます。
git clone https://github.com/skorch-dev/skorch.git
cd skorch
conda create -n skorch-env python=3.10
conda activate skorch-env
conda install -c pytorch pytorch
python -m pip install -r requirements.txt
python -m pip install .開発を支援したい場合は、実行してください。
git clone https://github.com/skorch-dev/skorch.git
cd skorch
conda create -n skorch-env python=3.10
conda activate skorch-env
conda install -c pytorch pytorch
python -m pip install -r requirements.txt
python -m pip install -r requirements-dev.txt
python -m pip install -e .
py.test # unit tests
pylint skorch # static code checksPythonバージョンをサポートされているPythonバージョンのいずれかに調整できます。
PIPについては、代わりに次の手順に従ってください。
git clone https://github.com/skorch-dev/skorch.git
cd skorch
# create and activate a virtual environment
python -m pip install -r requirements.txt
# install pytorch version for your system (see below)
python -m pip install .開発を支援したい場合は、実行してください。
git clone https://github.com/skorch-dev/skorch.git
cd skorch
# create and activate a virtual environment
python -m pip install -r requirements.txt
# install pytorch version for your system (see below)
python -m pip install -r requirements-dev.txt
python -m pip install -e .
py.test # unit tests
pylint skorch # static code checksPytorchは、必要なPytorchバージョンはOSとデバイスに依存しているため、依存関係の対象ではありません。 Pytorchのインストール手順については、PytorchのWebサイトをご覧ください。 Skorchは、現在の4つのマイナーなPytorchバージョンを正式にサポートしています。
ただし、それは古いバージョンが機能しないという意味ではなく、テストされていないということです。 Skorchは主にPytorch APIの安定した部分に依存しているため、古いPytorchバージョンは正常に動作するはずです。
一般に、これを実行してPytorchをインストールする必要があります。
# using conda:
conda install pytorch pytorch-cuda -c pytorch
# using pip
python -m pip install torch