Chapter 5. Implementing Neural Networks in PyTorch

Introduction to PyTorch

In this chapter, you will learn the basics of PyTorch, one of the most popular deep learning frameworks in use today. PyTorch was introduced by Facebook’s AI Research Lab in 2016 and gained users rapidly, both in industry and in research, through the following years. One reason for PyTorch’s widespread adoption was its intuitive, Pythonic feel, which fit naturally into the preexisting workstreams and coding paradigms followed by deep learning practitioners.

In particular, this chapter will discuss the data structures utilized by PyTorch, how to define neural models in PyTorch, and how to connect data with models for training and testing. Finally, we implement a practical example in PyTorch—a classifier for the MNIST digits dataset, complete with code for training and testing the classifier.

Installing PyTorch

Installing a CPU-compatible version of PyTorch is relatively simple. The PyTorch docs recommend using conda, a package management system. Within conda, you can create multiple environments, where an environment is a context that encapsulates all of your package installs. Access to a package does not transfer across environments—this allows the user to have a clean separation between different contexts by downloading packages within individual environments. We recommend that you create a conda environment for deep learning purposes that you can switch into whenever necessary. We refer you ...

Get Fundamentals of Deep Learning, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.