Book Image

The Deep Learning with PyTorch Workshop

By : Hyatt Saleh
Book Image

The Deep Learning with PyTorch Workshop

By: Hyatt Saleh

Overview of this book

Want to get to grips with one of the most popular machine learning libraries for deep learning? The Deep Learning with PyTorch Workshop will help you do just that, jumpstarting your knowledge of using PyTorch for deep learning even if you’re starting from scratch. It’s no surprise that deep learning’s popularity has risen steeply in the past few years, thanks to intelligent applications such as self-driving vehicles, chatbots, and voice-activated assistants that are making our lives easier. This book will take you inside the world of deep learning, where you’ll use PyTorch to understand the complexity of neural network architectures. The Deep Learning with PyTorch Workshop starts with an introduction to deep learning and its applications. You’ll explore the syntax of PyTorch and learn how to define a network architecture and train a model. Next, you’ll learn about three main neural network architectures - convolutional, artificial, and recurrent - and even solve real-world data problems using these networks. Later chapters will show you how to create a style transfer model to develop a new image from two images, before finally taking you through how RNNs store memory to solve key data issues. By the end of this book, you’ll have mastered the essential concepts, tools, and libraries of PyTorch to develop your own deep neural networks and intelligent apps.
Table of Contents (8 chapters)

About the Book

Want to get to grips with one of the most popular machine learning libraries for deep learning? The Deep Learning with PyTorch Workshop will help you do just that, jumpstarting your knowledge of using PyTorch for deep learning even if you're starting from scratch.

It's no surprise that deep learning's popularity has risen steeply in the past few years, thanks to intelligent applications such as self-driving vehicles, chatbots, and voice-activated assistants that are making our lives easier. This book will take you inside the world of deep learning, where you'll use PyTorch to understand the complexity of neural network architectures. 

The Deep Learning with PyTorch Workshop starts with an introduction to deep learning and its applications. You'll explore the syntax of PyTorch and learn how to define a network architecture and train a model. Next, you'll learn about three main neural network architectures - convolutional, artificial, and recurrent - and even solve real-world data problems using these networks. Later chapters will show you how to create a style transfer model to develop a new image from two images, before finally taking you through how RNNs store memory to solve key data issues. 

By the end of this book, you'll have mastered the essential concepts, tools, and libraries of PyTorch to develop your own deep neural networks and intelligent apps.

Audience

This deep learning book is ideal for anyone who wants to create and train deep learning models using PyTorch. A solid understanding of the Python programming language and its packages will help you grasp the topics covered in the book more quickly.

About the Chapters

Chapter 1, Introduction to Deep Learning and PyTorch, introduces deep learning and its applications as well as the main syntax of PyTorch. The chapter also shows how you can define a network architecture and train a model.

Chapter 2, Building Blocks of Neural Networks, introduces the concept of neural networks and explains the three main network architectures nowadays: artificial neural networks, convolutional neural networks, and recurrent neural networks. For each architecture, an explanation of the training process and the layers is provided.

Chapter 3, A Classification Problem Using DNNs, introduces a real-life data problem to be solved using an artificial neural network. The preprocessing of the dataset is explored, as well as the process of defining and training the model, and improving its accuracy through the use of error analysis.

Chapter 4, Convolutional Neural Networks, looks at convolutional neural networks in greater detail. Using a real-life data problem, you'll learn how to construct the network architecture and train it, as well as how to improve the results by using data augmentation and batch normalization.

Chapter 5, Style Transfer, demonstrates the process of performing the task of style transfer, where two images are taken as input to create a new image, with elements from both input images.

Chapter 6, Analyzing the Sequences of Data with RNNs, explores recurrent neural networks in greater detail. In the chapter, three popular data problems are solved using sequenced data as input.

Conventions

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows:

"Import torch, the optim package from PyTorch, and matplotlib:"

Words that you see on the screen, for example, in menus or dialog boxes, also appear in the same format.

A block of code is set as follows:

import torch
import torch.optim as optim
import matplotlib.pyplot as plt

New terms and important words are shown like this: "The chapter will also explore the concept of Natural Language Processing (NLP)."

Code Presentation

Lines of code that span multiple lines are split using a backslash ( \ ). When the code is executed, Python will ignore the backslash, and treat the code on the next line as a direct continuation of the current line.

For example:

history = model.fit(X, y, epochs=100, batch_size=5, verbose=1, \
                   validation_split=0.2, shuffle=False)

Comments are added into code to help explain specific bits of logic. Single-line comments are denoted using the # symbol, as follows:

# Print the sizes of the dataset
print("Number of Examples in the Dataset = ", X.shape[0])
print("Number of Features for each example = ", X.shape[1])

Multi-line comments are enclosed by triple quotes, as shown below:

"""
Define a seed for the random number generator to ensure the result will be reproducible
"""
seed = 1
np.random.seed(seed)
random.set_seed(seed)

Hardware Requirements

For the optimal student experience, we recommend the following hardware configuration:

  • Processor: Intel Core i3 or equivalent
  • Memory: 4 GB RAM
  • Storage: 35 GB available space

Software Requirements

You'll also need the following software installed in advance:

  • OS: Windows 7 SP1 64-bit, Windows 8.1 64-bit, or Windows 10 64-bit, Ubuntu Linux, or the latest version of macOS
  • Browser: Google Chrome/Mozilla Firefox (the latest version)
  • Notepad++/Sublime Text as an IDE (optional, as you can practice everything using Jupyter notebooks in your browser)
  • Python 3.7 with libraries as required (Jupyter, NumPy, pandas, Matplotlib, pillow, flask, xlrd, and scikit-learn)
  • PyTorch 1.3+ (preferably PyTorch 1.4, with or without CUDA)

Setting up Your Environment

Before we explore the book in detail, we need to set up specific software and tools. In the following section, we shall see how to do that.

Installing Python on Windows and macOS

  1. Visit the following link to download Python 3.7: https://www.python.org/downloads/release/python-376/.
  2. At the bottom of the page, locate the table under the heading Files:

    For Windows, click on Windows x86-64 executable installer for 64-bit or Windows x86 executable installer for 32-bit.

    For macOS, click on macOS 64-bit/32-bit installer for macOS X 10.6 and later, or macOS 64-bit installer for OS X 10.9 and later.

  3. Run the installer that you have downloaded.
  4. You can also install Python using the Anaconda distribution. Follow the instructions given in the this link for more details: https://www.anaconda.com/products/individual

Installing Python on Linux

  1. Open your Terminal and type the following command:
    sudo apt-get install python3.7

You can also install Python using the Anaconda distribution. Follow the instructions given in the this link for more details: https://www.anaconda.com/products/individual

Installing pip

pip is included by default with the installation of Python 3.7. However, it may be the case that it did not get installed. To check whether it was installed, execute the following command in your terminal or command prompt:

pip --version

You might need to use the pip3 command, due to previous versions of pip on your computer that are already using the pip command.

If the pip (or pip3) command is not recognized by your machine, follow these steps to install it:

  1. To install pip, visit the following link and download the get-pip.py file: https://pip.pypa.io/en/stable/installing/.
  2. Then, on the Terminal or Command Prompt, use the following command to install it:
    python get-pip.py 

You might need to use the python3 get-pip.py command, due to previous versions of Python on your machine that are already using the python command.

Installing PyTorch

To install PyTorch, with or without CUDA, follow these steps:

  1. Visit the following link: https://pytorch.org/get-started/locally/.
  2. Under the Start Locally heading, select the options that apply to you. This will give you the command that you need to execute to download PyTorch on your local machine. Use pip as the package to download PyTorch.
  3. Copy the command and run it in your Terminal or Command Prompt.

Installing Libraries

pip comes pre-installed with Anaconda. Once Anaconda is installed on your machine, all the required libraries can be installed using pip, for example, pip install numpy. Alternatively, you can install all the required libraries using pip install –r requirements.txt. You can find the requirements.txt file at https://packt.live/3ih86lh.

The exercises and activities will be executed in Jupyter Notebooks. Jupyter is a Python library and can be installed in the same way as the other Python libraries – that is, with pip install jupyter, but fortunately, it comes pre-installed with Anaconda. To open a notebook, simply run the command jupyter notebook in the Terminal or Command Prompt.

Opening a Jupyter Notebook

  1. Open a Terminal/Command Prompt.
  2. In the Terminal/Command Prompt, go to the directory location where you have downloaded the book's GitHub repository.
  3. Open a Jupyter Notebook by typing in the following command:
    jupyter notebook

    By executing the previous command, you will be able to use Jupyter Notebooks through the default browser of your machine.

Accessing the Code Files

You can find the complete code files of this book at https://packt.live/38qLadV. You can also run many activities and exercises directly in your web browser by using the interactive lab environment at https://packt.live/3ieFDg1.

We've tried to support interactive versions of all activities and exercises, but we recommend a local installation as well for instances where this support isn't available.

If you have any issues or questions about installation, please email us at [email protected].