Package: skorch
A scikit-learn compatible neural network library that wraps PyTorch.ResourcesDocumentationSource CodeInstallationExamplesTo see more elaborate examples, lookhere.importnumpyasnpfromsklearn.datasetsimportmake_classificationfromtorchimportnnfromskorchimportNeuralNetClassifierX,y=make_classification(1000,20,n_informative=10,random_state=0)X=X.astype(np.float32)y=y.astype(np.int64)classMyModule(nn.Module):def__init__(self,num_units=10,nonlin=nn.ReLU()):super().__init__()self.dense0=nn.Linear(20,num_units)self.nonlin=nonlinself.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)defforward(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))returnXnet=NeuralNetClassifier(MyModule,max_epochs=10,lr=0.1,# Shuffle training data on each epochiterator_train__shuffle=True,)net.fit(X,y)y_proba=net.predict_proba(X)In ansklearn Pipeline:fromsklearn.pipelineimportPipelinefromsklearn.preprocessingimportStandardScalerpipe=Pipeline([('scale',StandardScaler()),('net',net),])pipe.fit(X,y)y_proba=pipe.predict_proba(X)Withgrid search:fromsklearn.model_selectionimportGridSearchCV# deactivate skorch-internal train-valid split and verbose loggingnet.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 also provides many convenient features, among others:Learning rate schedulers(Warm restarts, cyclic LR and many more)Scoring using sklearn (and custom) scoring functionsEarly stoppingCheckpointingParameter freezing/unfreezingProgress bar(for CLI as well as jupyter)Automatic inference of CLI parametersIntegration with GPyTorch for Gaussian ProcessesIntegration with Hugging Face 🤗Installationskorch requires Python 3.8 or higher.conda installationYou need a working conda installation. Get the correct miniconda for
your system fromhere.To install skorch, you need to use the conda-forge channel:condainstall-cconda-forgeskorchWe recommend to use aconda virtual environment.Note: The conda channel isnotmanaged by the skorch
maintainers. More information is availablehere.pip installationTo install with pip, run:python-mpipinstall-UskorchAgain, we recommend to use avirtual environmentfor this.From sourceIf you would like to use the most recent additions to skorch or
help development, you should install skorch from source.Using condaTo install skorch from source using conda, proceed as follows:gitclonehttps://github.com/skorch-dev/skorch.gitcdskorchcondacreate-nskorch-envpython=3.10condaactivateskorch-envcondainstall-cpytorchpytorchpython-mpipinstall-rrequirements.txtpython-mpipinstall.If you want to help developing, run:gitclonehttps://github.com/skorch-dev/skorch.gitcdskorchcondacreate-nskorch-envpython=3.10condaactivateskorch-envcondainstall-cpytorchpytorchpython-mpipinstall-rrequirements.txtpython-mpipinstall-rrequirements-dev.txtpython-mpipinstall-e.py.test# unit testspylintskorch# static code checksYou may adjust the Python version to any of the supported Python versions.Using pipFor pip, follow these instructions instead:gitclonehttps://github.com/skorch-dev/skorch.gitcdskorch# create and activate a virtual environmentpython-mpipinstall-rrequirements.txt# install pytorch version for your system (see below)python-mpipinstall.If you want to help developing, run:gitclonehttps://github.com/skorch-dev/skorch.gitcdskorch# create and activate a virtual environmentpython-mpipinstall-rrequirements.txt# install pytorch version for your system (see below)python-mpipinstall-rrequirements-dev.txtpython-mpipinstall-e.py.test# unit testspylintskorch# static code checksPyTorchPyTorch is not covered by the dependencies, since the PyTorch version
you need is dependent on your OS and device. For installation
instructions for PyTorch, visit thePyTorch website. skorch officially supports the last four
minor PyTorch versions, which currently are:1.11.01.12.11.13.12.0.1However, that doesn’t mean that older versions don’t work, just that
they aren’t tested. Since skorch mostly relies on the stable part of
the PyTorch API, older PyTorch versions should work fine.In general, running this to install PyTorch should work:# using conda:condainstallpytorchpytorch-cuda-cpytorch# using pippython-mpipinstalltorchExternal resources@jakubczakon:blog post“8 Creators and Core Contributors Talk About Their Model Training
Libraries From PyTorch Ecosystem” 2020@BenjaminBossan:talk 1“skorch: A
scikit-learn compatible neural network library” at PyCon/PyData 2019@githubnemo:posterfor the PyTorch developer conference 2019@thomasjpfan:talk 2“Skorch: A Union of Scikit learn and PyTorch” at SciPy 2019@thomasjpfan:talk 3“Skorch - A Union of Scikit-learn and PyTorch” at PyData 2018CommunicationGitHub discussions:
user questions, thoughts, install issues, general discussions.GitHub issues: bug
reports, feature requests, RFCs, etc.Slack: We run the #skorch channel on thePyTorch Slack server, for which you canrequest access
here.