PyTorch has always been my go-to library for building any deep learning model.
However, one thing I particularly dislike about PyTorch is manually writing its long training loops, which go as follows:
For every epoch:
For every batch:
Run the forward pass
Calculate the loss
Compute the gradients
Run backpropagation
Compute epoch accuracy
Print the accuracy, loss, etc.
That’s too much work and code, isn’t it?
Skorch immensely simplifies training neural networks with PyTorch.
Skorch (Sklearn + PyTorch) is an open-source library that provides full Scikit-learn compatibility to PyTorch.
This means we can train PyTorch models in a way similar to Scikit-learn, using functions such as fit(), predict(), score(), etc.
Isn’t that cool?
Let’s see how to use it!
First, we define our PyTorch neural network as we usually would (no change here):
Make sure you have installed Skorch: pip install skorch
.
As we are creating a classifier, we import and create an object of Skorch’s NeuralNetClassifier
class.
There’s a class for regression models as well:
NeuralNetRegressor
.
The first argument is the PyTorch model class (
MyClassifier
).Next, we specify training hyperparameters like learning rate, batch size, etc.
We also specify the optimizer and loss function as a parameter.
Done!
Now, we can directly invoke fit()
method to train the model as follows:
As shown above, Skorch automatically prints all training metrics for us.
What’s more, we can also call the predict()
and score()
methods to generate predictions and output accuracy, respectively.
Isn’t that simple, cool, and elegant?
👉 Over to you: Are you aware of any other utility libraries to simplify model training? Let me know :)
👉 If you liked this post, don’t forget to leave a like ❤️. It helps more people discover this newsletter on Substack and tells me that you appreciate reading these daily insights. The button is located towards the bottom of this email.
Thanks for reading!
Latest full articles
If you’re not a full subscriber, here’s what you missed last month:
Model Compression: A Critical Step Towards Efficient Machine Learning.
Generalized Linear Models (GLMs): The Supercharged Linear Regression.
Gaussian Mixture Models (GMMs): The Flexible Twin of KMeans.
Where Did The Assumptions of Linear Regression Originate From?
To receive all full articles and support the Daily Dose of Data Science, consider subscribing:
👉 Tell the world what makes this newsletter special for you by leaving a review here :)
👉 If you love reading this newsletter, feel free to share it with friends!