2 Deep learning with PyTorch

 

This chapter covers

  • PyTorch tensors and basic operations
  • Preparing data for deep learning in PyTorch
  • Building and training deep neural networks with PyTorch
  • Conducting binary and multicategory classifications with deep learning
  • Creating a validation set to decide training stop points

In this book, we’ll use deep neural networks to generate a wide range of content, including text, images, shapes, music, and more. I assume you already have a foundational understanding of machine learning (ML) and, in particular, artificial neural networks. In this chapter, I’ll refresh your memory on essential concepts such as loss functions, activation functions, optimizers, and learning rates, which are crucial for developing and training deep neural networks. If you find any gaps in your understanding of these topics, I strongly encourage you to address them before proceeding with the projects in this book. Appendix B provides a summary of the basic skills and concepts needed, including the architecture and training of artificial neural networks.

NOTE

There are plenty of great ML books out there for you to choose from. Examples include Hands-on Machine Learning with Scikit-Learn, Keras, and TensorFlow (2019, O’Reilly) and Machine Learning, Animated (2023, CRC Press). Both books use TensorFlow to create neural networks. If you prefer a book that uses PyTorch, I recommend Deep Learning with PyTorch (2020, Manning Publications).

2.1 Data types in PyTorch

2.1.1 Creating PyTorch tensors

2.1.2 Index and slice PyTorch tensors

2.1.3 PyTorch tensor shapes

2.1.4 Mathematical operations on PyTorch tensors

2.2 An end-to-end deep learning project with PyTorch

2.2.1 Deep learning in PyTorch: A high-level overview

2.2.2 Preprocessing data

2.3 Binary classification

2.3.1 Creating batches

2.3.2 Building and training a binary classification model

2.3.3 Testing the binary classification model

2.4 Multicategory classification

2.4.1 Validation set and early stopping

2.4.2 Building and training a multicategory classification model

Summary