Discover more from Daily Dose of Data Science
Skorch: The Power of PyTorch Combined with The Elegance of Sklearn
The best of both worlds.
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
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
There’s a class for regression models as well:
The first argument is the PyTorch model class (
Next, we specify training hyperparameters like learning rate, batch size, etc.
We also specify the optimizer and loss function as a parameter.
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
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 :)
Thanks for reading Daily Dose of Data Science! Subscribe for free to learn something new and insightful about Python and Data Science every day. Also, get a Free Data Science PDF (550+ pages) with 320+ tips.
👉 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:
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!