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