skorch
Version 1.0.0




包裹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 )在一個滑雪管道中:
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或更高。
您需要一個工作的Conda安裝。從這裡獲取正確的系統的正確minoconda。
要安裝Skorch,您需要使用Conda-Forge頻道:
conda install -c conda-forge skorch我們建議使用Conda虛擬環境。
注意:Conda頻道沒有由Skorch Mainters管理。更多信息可在此處提供。
要與PIP安裝,請運行:
python -m pip install -U skorch同樣,我們建議為此使用虛擬環境。
如果您想使用最近的添加劑或幫助開發,則應從Source中安裝Skorch。
要使用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 checks您可以將Python版本調整為任何支持的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版本取決於您的操作系統和設備。有關Pytorch的安裝說明,請訪問Pytorch網站。 Skorch正式支持最近的四個小型Pytorch版本,該版本目前是:
但是,這並不意味著舊版本不起作用,只是沒有對其進行測試。由於Skorch主要依賴於Pytorch API的穩定部分,因此較舊的Pytorch版本應該很好。
通常,運行此操作以安裝Pytorch應該有效:
# using conda:
conda install pytorch pytorch-cuda -c pytorch
# using pip
python -m pip install torch