



Eine scikit-larn-kompatible neuronale Netzwerkbibliothek, die Pytorch umhüllt.
Um aufwändige Beispiele zu sehen, schauen Sie sich hier an.
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 )In einer Sklearn -Pipeline:
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 )Mit Gittersuche:
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 bietet unter anderem auch viele bequeme Funktionen:
Skorch benötigt Python 3.9 oder höher.
Sie benötigen eine funktionierende Konda -Installation. Holen Sie sich von hier die richtige Miniconda für Ihr System.
Um Skorch zu installieren, müssen Sie den Conda-Forge-Kanal verwenden:
conda install -c conda-forge skorchWir empfehlen, eine virtuelle Konda -Umgebung zu verwenden.
Hinweis : Der Conda -Kanal wird nicht von den Skorch -Betreuern verwaltet. Weitere Informationen finden Sie hier.
Zum Installieren mit PIP ausführen:
python -m pip install -U skorchAuch hier empfehlen wir, dafür eine virtuelle Umgebung zu verwenden.
Wenn Sie die neuesten Ergänzungen für Skorch oder Hilfe bei der Entwicklung verwenden möchten, sollten Sie Skorch von Quelle installieren.
Um Skorch von Quelle mit Conda zu installieren, fahren Sie wie folgt fort:
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 .Wenn Sie helfen möchten, sich zu entwickeln, rennen Sie:
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 checksSie können die Python -Version an eine der unterstützten Python -Versionen anpassen.
Befolgen Sie für PIP stattdessen folgende Anweisungen:
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 .Wenn Sie helfen möchten, sich zu entwickeln, rennen Sie:
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 wird nicht von den Abhängigkeiten abgedeckt, da die von Ihnen benötigte Pytorch -Version von Ihrem Betriebssystem und Ihrem Gerät abhängt. Für Installationsanweisungen für Pytorch finden Sie die Pytorch -Website. Skorch unterstützt offiziell die letzten vier kleinen Pytorch -Versionen, die derzeit sind:
Das bedeutet jedoch nicht, dass ältere Versionen nicht funktionieren, sondern nur, dass sie nicht getestet werden. Da Skorch hauptsächlich auf den stabilen Teil der Pytorch -API angewiesen ist, sollten ältere Pytorch -Versionen gut funktionieren.
Im Allgemeinen sollte das Ausführen von Pytorch funktionieren:
# using conda:
conda install pytorch pytorch-cuda -c pytorch
# using pip
python -m pip install torch