



ห้องสมุดเครือข่ายประสาทที่เข้ากันได้กับ Scikit-learn ที่ห่อหุ้ม pytorch
หากต้องการดูตัวอย่างที่ซับซ้อนมากขึ้นให้ดูที่นี่
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 หรือสูงกว่า
คุณต้องมีการติดตั้ง Conda ที่ใช้งานได้ รับ miniconda ที่ถูกต้องสำหรับระบบของคุณจากที่นี่
ในการติดตั้ง Skorch คุณต้องใช้ช่องทาง Conda-Forge:
conda install -c conda-forge skorchเราขอแนะนำให้ใช้สภาพแวดล้อมเสมือนจริงของ Conda
หมายเหตุ : ช่อง conda ไม่ได้ รับการจัดการโดยผู้ดูแล Skorch มีข้อมูลเพิ่มเติมที่นี่
ในการติดตั้งด้วย PIP, Run:
python -m pip install -U skorchอีกครั้งเราแนะนำให้ใช้สภาพแวดล้อมเสมือนจริงสำหรับสิ่งนี้
หากคุณต้องการใช้เพิ่มเติมล่าสุดกับ Skorch หรือ Help Development คุณควรติดตั้ง Skorch จากแหล่งที่มา
ในการติดตั้ง Skorch จากแหล่งที่มาโดยใช้ Conda ให้ดำเนินการดังนี้:
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