Skorch is one of the useful libraries in Pytorch to work on machine learning models especially neural networks. It is a robust library that works in the combination of PyTorch and Scikit-learn. Previously we used the scikit-learn library but its applications are only limited to classical machine learning models while skorch is a compatible library for developing the neural networks.
Topics we cover in the article:
- What is skorch?
- How to install and use skorch?
- Basic Functions we use in skorch
- Neural Network using Skorch
- Hands-on Binary classification model using skorch
What is SKORCH…? Basic Functions we use in Skorch
Skorch enables programmers to implement code using the customizability scikit-learn and power of PyTorch. Just call model.fit() and you don’t have to worry about writing your own callback functions, skorch handles everything for you. Skorch is an inbuilt library in PyTorch used for Neural networks. In building deep neural networks, we are required to train our model, evaluate, dividing the train & validation data, and many other operations required to build a model. Here skorch library will reduce the effort of the programmer similar to scikit learn library in machine learning. Skorch also provides many features like learning rate schedulers, early stopping, checkpointing, parameter freezing and unfreezing, etc.
So What are callback functions………?
A callback function is passed into another function as an argument and then it is invoked into the outer function and it is used to perform routine actions.
Sign up for your weekly dose of what's up in emerging technology.
How to Install and Use Skorch
#Installing skorch pip install -U skorch #Using Skorch import skorch
Basic Functions we use in Skorch
from skorch import NeuralNet from skorch.callbacks import EpochTimer from skorch.callbacks import PrintLog from skorch.callbacks import EpochScoring from skorch.callbacks import PassthroughScoring from skorch.dataset import CVSplit from skorch.utils import get_dim from skorch.utils import is_dataset from skorch.utils import to_numpy
Binary classification model using skorch learn
Here we are importing the required libraries.
import skorch import numpy as np from sklearn.datasets import make_classification from torch import nn from skorch import NeuralNetClassifier
In the below code snippet we are implementing the basic classification toy model using skorch.
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(MyModule, self).__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 model = NeuralNetClassifier( MyModule, max_epochs=10, lr=0.1, # Shuffle training data on each epoch iterator_train__shuffle=True, ) model.fit(X, y) y_proba = net.predict_proba(X) #Output
In the above demonstration, we have seen that how the work for programmers can be reduced by implementing only a few lines of code by deploying the skorch library. This library is specially designed for neural network developers to reduce the programming efforts. We can be able to build the neural net model so easily by using a model.fit(), specifying the number of epochs, declaring a few parameters, and so we get a model built.