Deep Learning with PyTorch: A hands-on intro to cutting-edge AI

8 min read
Deep learning with pytorch

This article is part of “AI education”, a series of posts that review and explore educational content on data science and machine learning. (In partnership with Paperspace)

If I wanted to learn deep learning with Python again, I would probably start with PyTorch, an open-source library developed by Facebook’s AI Research Lab that is powerful, easy to learn, and very versatile.

When it comes to training material, however, PyTorch lags behind TensorFlow, Google’s flagship deep learning library.

There are fewer books on PyTorch than TensorFlow, and even fewer online courses. Among them is Deep Learning with PyTorch by Eli Stevens, Luca Antiga, and Thomas Viehmann, three engineers who have contributed to the project and have extensive experience developing deep learning solutions.

Deep Learning with PyTorch is split across two main sections, first teaching the basics of deep learning and then delving into an advanced, real-world application of medical imaging analysis. Soumith Chintala, the co-creator of PyTorch, has described the book as “a definitive treatise on PyTorch.”

On both the basics and advanced concepts, the book delivers on its promises. But in the in-between area, things get a bit complicated.

An excellent overview of deep learning

deep learning with pytorch book cover
Deep Learning with PyTorch

The first part of Deep Learning with PyTorch spans across eight chapters and provides an introduction to key deep learning concepts. The authors have done a terrific job of breaking down complicated topics, such as gradient descent, with cartoonish drawings and concrete examples. A lot of the stuff you’ll see in this section overlaps with deep learning intros in Python machine learning books but with more depth added.

Deep Learning with PyTorch takes you through the history of the PyTorch and its evolution into one of the most popular Python libraries for deep learning. You’ll also see a very intuitive and high-level overview of the deep learning pipeline with PyTorch. This zoomed-out view will help you put the pieces together when you go deep into the details of the functions and classes of the library.

One of the things I really like about the book is the practical approach it takes to Python deep learning. Before going through the manual creation of deep neural networks, the authors introduce TorchVision, a library of pretrained deep learning models you can integrate into your applications.

So, before creating your own models, you’ll learn how to use what others have already gone through the pain to create, which is a common practice in the field. In fact, you’ll get to play around with a pretrained generative adversarial network and turn a horse into a zebra.

The deep learning intro also has an in-depth chapter on tensors, the basic building block of deep learning computations. Deep Learning with PyTorch covers math, the coding, and the hardware side of tensors, including the storage and differences between CPU and GPU computation of tensors.

cycle gan zebra
In “Deep Learning with PyTorch,” you’ll use CycleGAN to turn a horse into a zebra

Data preprocessing is well-covered in the book, given that a lot of the must-avoid pitfalls of machine learning are in the data preparation phase. The fact that the book has an entire chapter focusing on just preparing data for neural networks is very welcome and underlines the importance of this often-disregard aspect of deep learning.

With the basics out of the way, the authors introduce the implementation of key deep learning constructs in PyTorch, including the base Module and ready-made constructs such as convolutional neural networks (Conv2d), max pooling layers (MaxPool2d), dropouts, and batch normalization. In case you already have experience with another Python deep learning library such as TensorFlow, you’ll be able to map that knowledge to PyTorch. If this is your first book on the topic, Deep Learning with PyTorch provides comprehensive explanation of each of the concepts before demonstrating the coding technique.

cartoon depiction of convolutional neural network
“Deep Learning with PyTorch” uses fun, cartoonish depictions to show how different deep learning techniques work.

You’ll also get to do some PyTorch customization, including the creation of residual networks (resnet), a very popular construction in computer vision applications. The authors’ goal (which they accomplish to perfection in my opinion) is, in their own words, “to proficiently translate the math behind a paper into actual PyTorch code, or at least understand the code that others have written with the same intention.”

Great Python deep learning coding samples

The authors of Deep Learning with PyTorch have taken a simple-to-advanced approach to coding, starting with step-by-step hand-coded walkthroughs of deep learning techniques to explain concepts such as loss functions, weights, and biases. Then one by one, they introduce PyTorch functions and classes to replace the hand-coded deep learning techniques.

The code samples in the book are also very well annotated, providing key explanations of the functions of each part. This is a very useful addition for people who just want to skim over the code without following it line by line.

deep learning code annotation
The code in “Deep Learning with PyTorch” is very well annotated and helps you get the gist of the samples without following them line by line.

What’s also very welcome is the use of good coding techniques. While the code samples in many Python machine learning books just try to demonstrate machine learning techniques, the authors of Deep Learning with PyTorch have taken great care to adhere to the recommended Python coding practices. The book’s samples make ample use of convenient Python features such as list comprehensions, parameter unpacking, “with” structures, named tuples, and functools annotations. The book also makes use of Python’s object-oriented programming features to extend PyTorch’s functionality.

This means that, while Deep Learning with PyTorch provides much cleaner code than many other books, it also requires readers to have a solid foundation in Python to fully understand the samples. I don’t think this is a downside, however, because if you’re going to be writing Python deep learning applications, you’ll have to be a good Python coder anyway.

And this reality becomes even more highlighted in the second part of the book, in which you create a full-fledged deep learning application.

The deep learning cancer detection application

What sets Deep Learning with PyTorch apart from other books on Python deep learning is its second part, where the authors take a deep dive in medical imaging analysis, an application that has become very popular in machine learning.

The goal is to develop an AI application that can analyze chest CT scans, localize nodules, and help in determining which ones might be cancerous. The book provides an in-depth exploration of the deep learning development pipeline for real-world applications. Out of necessity, you’ll also have to learn a great deal about CT scans, CT scan data, lung nodules, and lung cancer. Luckily, one of the authors is a biomedical engineer and has a long history in researching and developing medical imaging applications.

Many articles try to frame deep learning as an end-to-end, magic black box that you feed with data, and let it make sense of the process and give you the answer you need. Deep Learning with PyTorch does a very good job at showing that most problems can’t be solved end-to-end, and you often need to piece together several deep learning models and other components. The lung cancer detection application developed in Deep Learning with PyTorch requires the sequential combination of classification and segmentation models sequentially.

Deep learning lung cancer diagnosis
“Deep Learning with PyTorch” brings together different deep learning models to solve a real-world problem: Detecting lung cancer.

The book also introduces some of the scientific and technical challenges you face in developing real-world deep learning applications. For instance, you’ll get an in-depth look at the “class imbalance” problem, which happens often in medical imaging applications because most scans are benign and very few represent malignant patterns.

Another problem the book shows (which I’ve seen less discussed elsewhere) is the storage and caching of training data. When you’re dealing with huge datasets, you’ll have to have a strategy to load and store your data efficiently or else data retrieval becomes a bottleneck when training your deep learning model, and you won’t be able to leverage the full power of your GPUs.

Gradually, Deep Learning with PyTorch builds up the different components of the cancer detection application and brings them together in a complicated object-oriented Python application. In the process, the authors continue to teach new concepts and techniques such as confusion matrices, performance metrics (recall, precision, and F1 score), data augmentation, transfer learning, and more. You’ll also get to use evaluation tools such as TensorBoard (even though TensorBoard was created for TensorFlow, the authors show that you can also use it to track progress on PyTorch applications).

The real-world setting also lays the groundwork for some invaluable deep learning lessons, such as the need to test modifications one at a time—when you make several modifications to your deep learning model at the same time, you won’t be able to draw consistent and reliable conclusions.

As the second section comes to a close, you’ll put all the pieces of your deep learning application together and learn to serve them on the web.

Things get too complicated


Deep Learning with PyTorch is one of the few books that take you through a full-fledged application, which is great, because it helps highlight aspects of the deep learning development pipeline that other books miss.

But the tradeoff is that somewhere along the book, the code gets too complicated for readers who are having their first taste of deep learning. Now, I wouldn’t make this criticism of an intermediate or advanced Python deep learning book. But with all its intuitive descriptions and cartoonish depictions, Deep Learning with PyTorch is undoubtedly a book for beginners. But unfortunately, novices will not be able to benefit to the full from the second section, especially since each new chapter builds up on previous chapters. On the other hand, experienced deep learning developers will be bored by some of the explanations the book gives on basic deep learning topics.

You can’t satisfy both newbies and veterans in the same book. In my opinion, the medical application section minus the introductory material could have been an independent book for people who already have experience with deep learning algorithms.

But even if you don’t understand all of what Deep Learning with PyTorch has to offer, I still suggest you read the entire book. The impression it will leave is invaluable: Deep learning is a complicated field, a mix of science and art where learning never ends.

Perhaps the best lesson in deep learning the authors teach us comes at the end of Chapter 14: “In all likelihood, the vast majority of your experiments will not work out. Not every idea will be a discovery, and not every change will be a breakthrough. Deep learning is fiddly. Deep learning is fickle. And remember that deep learning is literally pushing at the forefront of human knowledge; it’s a frontier that we are exploring and mapping further every day, right now. It’s an exciting time to be in the field, but as with most fieldwork, you’re going to get some mud on your boots.”


  1. Thank you for the in-depth review! It’s a fair point about the book covering a lot of ground. We’d written it that way to try and provide value to a wide range of readers, but I can certainly see the argument that the second half moves a bit quickly for a reader who first started with deep learning in chapter 1. I hope that such readers treat the advanced topics as aspirational, rather than discouraging.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.