Mastering PyTorch - Second Edition

Book description

Master advanced techniques and algorithms for machine learning with PyTorch using real-world examples Updated for PyTorch 2.x, including integration with Hugging Face, mobile deployment, diffusion models, and graph neural networks Purchase of the print or Kindle book includes a free eBook in PDF format

Key Features

  • Understand how to use PyTorch to build advanced neural network models
  • Get the best from PyTorch by working with Hugging Face, fastai, PyTorch Lightning, PyTorch Geometric, Flask, and Docker
  • Unlock faster training with multiple GPUs and optimize model deployment using efficient inference frameworks

Book Description

PyTorch is making it easier than ever before for anyone to build deep learning applications. This PyTorch deep learning book will help you uncover expert techniques to get the most out of your data and build complex neural network models.

You’ll build convolutional neural networks for image classification and recurrent neural networks and transformers for sentiment analysis. As you advance, you'll apply deep learning across different domains, such as music, text, and image generation, using generative models, including diffusion models. You'll not only build and train your own deep reinforcement learning models in PyTorch but also learn to optimize model training using multiple CPUs, GPUs, and mixed-precision training. You’ll deploy PyTorch models to production, including mobile devices. Finally, you’ll discover the PyTorch ecosystem and its rich set of libraries. These libraries will add another set of tools to your deep learning toolbelt, teaching you how to use fastai to prototype models and PyTorch Lightning to train models. You’ll discover libraries for AutoML and explainable AI (XAI), create recommendation systems, and build language and vision transformers with Hugging Face.

By the end of this book, you'll be able to perform complex deep learning tasks using PyTorch to build smart artificial intelligence models.

What you will learn

  • Implement text, vision, and music generation models using PyTorch
  • Build a deep Q-network (DQN) model in PyTorch
  • Deploy PyTorch models on mobile devices (Android and iOS)
  • Become well versed in rapid prototyping using PyTorch with fastai
  • Perform neural architecture search effectively using AutoML
  • Easily interpret machine learning models using Captum
  • Design ResNets, LSTMs, and graph neural networks (GNNs)
  • Create language and vision transformer models using Hugging Face

Who this book is for

This deep learning with PyTorch book is for data scientists, machine learning engineers, machine learning researchers, and deep learning practitioners looking to implement advanced deep learning models using PyTorch. This book is ideal for those looking to switch from TensorFlow to PyTorch. Working knowledge of deep learning with Python is required.

Table of contents

  1. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Get in touch
  2. Overview of Deep Learning Using PyTorch
    1. A refresher on deep learning
      1. Activation functions
    2. Optimization schedule
    3. Exploring the PyTorch library in contrast to TensorFlow
      1. Tensor modules
      2. PyTorch modules
        1. torch.nn
        2. torch.optim
        3. torch.utils.data
      3. Training a neural network using PyTorch
    4. Summary
    5. Reference list
  3. Deep CNN Architectures
    1. Why are CNNs so powerful?
    2. Evolution of CNN architectures
    3. Developing LeNet from scratch
      1. Using PyTorch to build LeNet
      2. Training LeNet
      3. Testing LeNet
    4. Fine-tuning the AlexNet model
      1. Using PyTorch to fine-tune AlexNet
    5. Running a pretrained VGG model
    6. Exploring GoogLeNet and Inception v3
      1. Inception modules
      2. 1x1 convolutions
      3. Global average pooling
      4. Auxiliary classifiers
      5. Inception v3
    7. Discussing ResNet and DenseNet architectures
      1. ResNet
      2. DenseNet
    8. Understanding EfficientNets and the future of CNN architectures
    9. Summary
    10. References
  4. Combining CNNs and LSTMs
    1. Building a neural network with CNNs and LSTMs
      1. Text encoding demo
    2. Building an image caption generator using PyTorch
      1. Downloading the image captioning datasets
      2. Preprocessing caption (text) data
      3. Preprocessing image data
      4. Defining the image captioning data loader
      5. Defining the CNN-LSTM model
      6. Training the CNN-LSTM model
      7. Generating image captions using the trained model
    3. Summary
    4. References
  5. Deep Recurrent Model Architectures
    1. Exploring the evolution of recurrent networks
      1. Types of recurrent neural networks
      2. RNNs
      3. Bidirectional RNNs
      4. LSTMs
      5. Extended and bidirectional LSTMs
      6. Multi-dimensional RNNs
      7. Stacked LSTMs
      8. GRUs
      9. Grid LSTMs
      10. Gated orthogonal recurrent units
    2. Training RNNs for sentiment analysis
      1. Loading and preprocessing the text dataset
      2. Instantiating and training the model
    3. Building a bidirectional LSTM
      1. Loading and preprocessing the text dataset
      2. Instantiating and training the LSTM model
    4. Discussing GRUs and attention-based models
      1. GRUs and PyTorch
      2. Attention-based models
    5. Summary
    6. References
  6. Advanced Hybrid Models
    1. Building a transformer model for language modeling
      1. Reviewing language modeling
      2. Understanding the transformer model architecture
      3. Defining a transformer model in PyTorch
        1. Loading and processing the dataset
        2. Training the transformer model
    2. Developing a RandWireNN model from scratch
      1. Understanding RandWireNNs
      2. Developing RandWireNNs using PyTorch
        1. Defining a training routine and loading data
        2. Defining the randomly wired graph
        3. Defining RandWireNN model modules
        4. Transforming a random graph into a neural network
        5. Training the RandWireNN model
        6. Evaluating and visualizing the RandWireNN model
    3. Summary
    4. References
  7. Graph Neural Networks
    1. Introduction to GNNs
      1. Understanding the intuition behind GNNs
      2. Using regular NNs on graph data – a thought experiment
      3. Understanding the power of GNNs with computational graphs
    2. Types of graph learning tasks
      1. Understanding node-level tasks
      2. Understanding edge-level tasks
      3. Understanding graph-level tasks
    3. Reviewing prominent GNN models
      1. Understanding graph convolutions with GCNs
      2. Using attention in graphs with GAT
      3. Performing graph sampling with GraphSAGE
      4. Building a GCN model using PyTorch Geometric
      5. Loading and exploring the citation networks dataset
      6. Building a simple NN-based node classifier
      7. Building a GCN model for node classification
    4. Training a GAT model with PyTorch Geometric
    5. Summary
    6. Reference list
  8. Music and Text Generation with PyTorch
    1. Building a transformer-based text generator with PyTorch
      1. Training the transformer-based language model
      2. Saving and loading the language model
      3. Using the language model to generate text
    2. Using GPT models as text generators
      1. Out-of-the-box text generation with GPT-2
      2. Text generation strategies using PyTorch
        1. Greedy search
        2. Beam search
        3. Top-k and top-p sampling
      3. Text generation with GPT-3
    3. Generating MIDI music with LSTMs using PyTorch
      1. Loading the MIDI music data
      2. Defining the LSTM model and training routine
      3. Training and testing the music generation model
    4. Summary
    5. References
  9. Neural Style Transfer
    1. Understanding how to transfer style between images
    2. Implementing neural style transfer using PyTorch
      1. Loading the content and style images
      2. Loading and trimming the pretrained VGG19 model
      3. Building the neural style transfer model
      4. Training the style transfer model
      5. Experimenting with the style transfer system
    3. Summary
    4. References
  10. Deep Convolutional GANs
    1. Defining the generator and discriminator networks
      1. Understanding the DCGAN generator and discriminator
    2. Training a DCGAN using PyTorch
      1. Defining the generator
      2. Defining the discriminator
      3. Loading the image dataset
      4. Training loops for DCGANs
    3. Using GANs for style transfer
      1. Understanding the pix2pix architecture
        1. Exploring the pix2pix generator
        2. Exploring the pix2pix discriminator
    4. Summary
    5. References
  11. Image Generation Using Diffusion
    1. Understanding image generation using diffusion
      1. Understanding how diffusion works
      2. Training a forward diffusion model
      3. Performing reverse diffusion or denoising
    2. Training a diffusion model for image generation
      1. Loading the dataset using Hugging Face datasets
      2. Processing the dataset using torchvision transforms
      3. Adding noise to images using diffusers
      4. Defining the UNet model
      5. Training the UNet model
        1. Defining the optimizer and learning schedule
        2. Using Hugging Face Accelerate to accelerate training
        3. Running the model training loop
        4. Generating realistic anime images using (reverse) diffusion
    3. Understanding text-to-image generation using diffusion
      1. Encoding text input into an embedding vector
      2. Ingesting additional text data in the (conditional) UNet model
    4. Using the Stable Diffusion model to generate images from text
    5. Summary
    6. Reference list
  12. Deep Reinforcement Learning
    1. Reviewing RL concepts
      1. Types of RL algorithms
        1. Model-based
        2. Model-Free
    2. Discussing Q-learning
    3. Understanding deep Q-learning
      1. Using two separate DNNs
      2. Experience replay buffer
    4. Building a DQN model in PyTorch
      1. Initializing the main and target CNN models
      2. Defining the experience replay buffer
      3. Setting up the environment
      4. Defining the CNN optimization function
      5. Managing and running episodes
      6. Training the DQN model to learn Pong
    5. Summary
    6. Reference list
  13. Model Training Optimizations
    1. Distributed training with PyTorch
      1. Training the MNIST model in a regular fashion
      2. Training the MNIST model in a distributed fashion
    2. Distributed training on GPUs with CUDA
      1. Automatic mixed precision training
      2. Regular model training on a GPU
      3. Mixed precision training on a GPU
    3. Summary
    4. Reference list
  14. Operationalizing PyTorch Models into Production
    1. Model serving in PyTorch
      1. Creating a PyTorch model inference pipeline
      2. Saving and loading a trained model
      3. Building the inference pipeline
    2. Building a basic model server
      1. Writing a basic app using Flask
      2. Using Flask to build our model server
        1. Setting up model inference for Flask serving
        2. Building a Flask app to serve model
        3. Using a Flask server to run predictions
    3. Creating a model microservice
    4. Serving a PyTorch model using TorchServe
      1. Installing TorchServe
      2. Launching and using a TorchServe server
    5. Exporting universal PyTorch models using TorchScript and ONNX
      1. Understanding the utility of TorchScript
      2. Model tracing with TorchScript
      3. Model scripting with TorchScript
    6. Running a PyTorch model in C++
    7. Using ONNX to export PyTorch models
    8. Serving PyTorch models in the cloud
      1. Using PyTorch with AWS
      2. Serving a PyTorch model using an AWS instance
      3. Using TorchServe with Amazon SageMaker
      4. Serving PyTorch models on Google Cloud
      5. Serving PyTorch models with Azure
      6. Working with Azure’s DSVMs
      7. Discussing Azure Machine Learning Service
    9. Summary
      1. Reference list
  15. PyTorch on Mobile Devices
    1. Deploying a PyTorch model on Android
      1. Converting the PyTorch model to a mobile-friendly format
      2. Setting up the Android app development environment
    2. Using the phone camera in the Android app to capture images
      1. Enabling the camera during app startup
        1. Handling camera permissions in Android
        2. Opening the camera for image capture
        3. Capturing images using the phone camera
      2. Running ML model inference on camera-captured images
        1. Validating the ML model binary path
        2. Performing image classification on camera-captured images
      3. Launching the app on an Android mobile device
    3. Building PyTorch apps on iOS
      1. Setting up the iOS development environment
      2. Using a phone camera in the iOS app to capture images
      3. Running ML model inference on camera-captured images
    4. Summary
    5. Reference list
  16. Rapid Prototyping with PyTorch
    1. Using fastai to set up model training in a few minutes
      1. Setting up fastai and loading data
      2. Training an MNIST model using fastai
      3. Evaluating and interpreting the model using fastai
    2. Training models on any hardware using PyTorch Lightning
      1. Defining the model components in PyTorch Lightning
      2. Training and evaluating the model using PyTorch Lightning
    3. Profiling MNIST model inference using PyTorch Profiler
      1. Profiling on a CPU
      2. Profiling model inference on the GPU
        1. Visualizing model profiling results
    4. Summary
    5. Reference list
  17. PyTorch and AutoML
    1. Finding the best neural architectures with AutoML
      1. Using Auto-PyTorch for optimal MNIST model search
        1. Loading the MNIST dataset
        2. Running a neural architecture search with Auto-PyTorch
        3. Visualizing the optimal AutoML model
    2. Using Optuna for hyperparameter search
      1. Defining the model architecture and loading the dataset
      2. Defining the model training routine and optimization schedule
      3. Running Optuna’s hyperparameter search
    3. Summary
    4. Reference list
  18. PyTorch and Explainable AI
    1. Model interpretability in PyTorch
      1. Training the handwritten digits classifier – a recap
      2. Visualizing the convolutional filters of the model
      3. Visualizing the feature maps of the model
    2. Using Captum to interpret models
      1. Setting up Captum
      2. Exploring Captum’s interpretability tools
    3. Summary
    4. Reference List
  19. Recommendation Systems with PyTorch
    1. Using deep learning for recommendation systems
      1. Understanding a movie recommendation system dataset
      2. Understanding embedding-based recommender systems
    2. Understanding and processing the MovieLens dataset
      1. Downloading the MovieLens dataset
      2. Loading and analyzing the MovieLens dataset
      3. Processing the MovieLens dataset
      4. Creating the MovieLens dataloader
    3. Training and evaluating a recommendation system model
      1. Defining the EmbeddingNet architecture
      2. Training EmbeddingNet
      3. Evaluating the trained EmbeddingNet model
    4. Building a recommendation system using the trained model
    5. Summary
    6. Reference list
  20. PyTorch and Hugging Face
    1. Understanding Hugging Face within the PyTorch context
      1. Exploring Hugging Face components relevant to PyTorch
      2. Integrating Hugging Face with PyTorch
    2. Using the Hugging Face Hub for pre-trained models
    3. Using the Hugging Face Datasets library with PyTorch
    4. Using Accelerate to speed up PyTorch model training
    5. Using Optimum to optimize PyTorch model deployment
    6. Summary
    7. Reference list
  21. Index

Product information

  • Title: Mastering PyTorch - Second Edition
  • Author(s): Ashish Ranjan Jha
  • Release date: May 2024
  • Publisher(s): Packt Publishing
  • ISBN: 9781801074308