Daily Dose of Data Science

Share this post

Skorch: The Power of PyTorch Combined with The Elegance of Sklearn

www.blog.dailydoseofds.com

Discover more from Daily Dose of Data Science

High-quality insights on Data Science and Python, along with best practices — shared daily. Get a free 550+ page data science PDF guide and 450+ practice questions notebook.
Over 50,000 subscribers
Continue reading
Sign in

Skorch: The Power of PyTorch Combined with The Elegance of Sklearn

The best of both worlds.

Avi Chawla
Sep 24, 2023
31
Share this post

Skorch: The Power of PyTorch Combined with The Elegance of Sklearn

www.blog.dailydoseofds.com
Share

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 :)

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:

  • 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.

  • Bayesian Optimization for Hyperparameter Tuning.

  • Formulating the PCA Algorithm From Scratch.

  • Where Did The Assumptions of Linear Regression Originate From?

To receive all full articles and support the Daily Dose of Data Science, consider subscribing:

I want to read full articles.


👉 Tell the world what makes this newsletter special for you by leaving a review here :)

Review Daily Dose of Data Science

👉 If you love reading this newsletter, feel free to share it with friends!

Share Daily Dose of Data Science

31
Share this post

Skorch: The Power of PyTorch Combined with The Elegance of Sklearn

www.blog.dailydoseofds.com
Share
Previous
Next
Comments
Top
New
Community

No posts

Ready for more?

© 2023 Avi Chawla
Privacy ∙ Terms ∙ Collection notice
Start WritingGet the app
Substack is the home for great writing