Book Image

The Deep Learning Workshop

By : Mirza Rahim Baig, Thomas V. Joseph, Nipun Sadvilkar, Mohan Kumar Silaparasetty, Anthony So
Book Image

The Deep Learning Workshop

By: Mirza Rahim Baig, Thomas V. Joseph, Nipun Sadvilkar, Mohan Kumar Silaparasetty, Anthony So

Overview of this book

Are you fascinated by how deep learning powers intelligent applications such as self-driving cars, virtual assistants, facial recognition devices, and chatbots to process data and solve complex problems? Whether you are familiar with machine learning or are new to this domain, The Deep Learning Workshop will make it easy for you to understand deep learning with the help of interesting examples and exercises throughout. The book starts by highlighting the relationship between deep learning, machine learning, and artificial intelligence and helps you get comfortable with the TensorFlow 2.0 programming structure using hands-on exercises. You’ll understand neural networks, the structure of a perceptron, and how to use TensorFlow to create and train models. The book will then let you explore the fundamentals of computer vision by performing image recognition exercises with convolutional neural networks (CNNs) using Keras. As you advance, you’ll be able to make your model more powerful by implementing text embedding and sequencing the data using popular deep learning solutions. Finally, you’ll get to grips with bidirectional recurrent neural networks (RNNs) and build generative adversarial networks (GANs) for image synthesis. By the end of this deep learning book, you’ll have learned the skills essential for building deep learning models with TensorFlow and Keras.
Table of Contents (9 chapters)
Preface

About the Book

Are you fascinated by how deep learning powers intelligent applications such as self-driving cars, virtual assistants, facial recognition devices, and chatbots to process data and solve complex problems? Whether you are familiar with machine learning or are new to this domain, The Deep Learning Workshop will make it easy for you to understand deep learning with the help of interesting examples and exercises throughout.

The book starts by highlighting the relationship between deep learning, machine learning, and artificial intelligence and helps you get comfortable with the TensorFlow 2.0 programming structure using hands-on exercises. You'll understand neural networks, the structure of a perceptron, and how to use TensorFlow to create and train models. The book will then let you explore the fundamentals of computer vision by performing image recognition exercises with Convolutional Neural Networks (CNNs) using Keras. As you advance, you'll be able to make your model more powerful by implementing text embedding and sequencing the data using popular deep learning solutions. Finally, you'll get to grips with bidirectional Recurrent Neural Networks (RNNs) and build Generative Adversarial Networks (GANs) for image synthesis.

By the end of this deep learning book, you'll have learned the skills essential for building deep learning models with TensorFlow and Keras.

Audience

If you are interested in machine learning and want to create and train deep learning models using TensorFlow and Keras, this workshop is for you. A solid understanding of Python and its packages, along with basic machine learning concepts, will help you to learn the topics quickly.

About the Chapters

Chapter 1, Building Blocks of Deep Learning, discusses the practical applications of deep learning. One such application includes a hands-on code demo you can run right away to recognize an image from the internet. Through practical exercises, you'll also learn the key code implementations of TensorFlow 2.0 that will help you build exciting neural network models in the coming chapters.

Chapter 2, Neural Networks, teaches you the structure of artificial neural networks. Using TensorFlow 2.0, you will not only implement a neural network, but also train it. You will later build multiple deep neural networks with different configurations, thereby experiencing the neural network training process first-hand.

Chapter 3, Image Classification with Convolutional Neural Networks (CNNs), covers image processing, how it works, and how that knowledge can be applied to Convolutional Neural Networks (CNNs). Through practical exercises, you will create and train CNN models that can be used to recognize images of handwritten digits and even fruits. You'll also learn some key concepts such as pooling layers, data augmentation, and transfer learning.

Chapter 4, Deep Learning for Text – Embeddings, introduces you to the world of Natural Language Processing. You will first perform text preprocessing, an important skill when dealing with raw text data. You will implement classical approaches to text representation, such as one-hot encoding and the TF-lDF approach. Later in the chapter, you will learn about embeddings, and use the Skip-gram and Continuous Bag of Words algorithms to generate your own word embeddings.

Chapter 5, Deep Learning for Sequences, shows you how to work on a classic sequence processing task—stock price prediction. You will first create a model based on Recurrent Neural Networks (RNNs), then implement a 1D convolutions-based model and compare its performance with that RNN model. You will combine both approaches by combining RNNs with 1D convolutions in a hybrid model.

Chapter 6, LSTMs, GRUs, and Advanced RNNs, reviews RNNs' practical drawbacks and how Long Short Term Memory (LSTM) models help overcome them. You will build a model that analyzes sentiments in movie reviews and study the inner workings of Gated Recurring Units (GRUs). Throughout the chapter, you will create models based on plain RNNs, LSTMs, and GRUs and, at the end of the chapter, compare their performance.

Chapter 7, Generative Adversarial Networks, introduces you to generative adversarial networks (GANs) and their basic components. Through practical exercises, you will use GANs to generate a distribution that mimics a data distribution produced by a sine function. You will also learn about deep convolutional GANs and implement them in an exercise. Toward the end of the chapter, you will create GANs that are able to replicate images with convincing accuracy.

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:

"Load the MNIST dataset using mnist.load_data()"

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

A block of code is set as follows:

from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
train_scaled = scaler.fit_transform(train_data)
test_scaled = scaler.transform(test_data)

New terms and important words are shown like this:

The first step in preprocessing is inevitably tokenization—splitting the raw input text sequence into tokens. Long code snippets are truncated and the corresponding names of the code files on GitHub are placed at the top of the truncated code. The permalinks to the entire code are placed below the code snippet. It should look as follows:

Exercise7.04.ipynb

# Function to generate real samples
def realData(loc,batch):
    """
    loc is the random location or mean 
    around which samples are centered
    """
    # Generate numbers to right of the random point
    xr = np.arange(loc,loc+(0.1*batch/2),0.1)
    xr = xr[0:int(batch/2)]
    # Generate numbers to left of the random point
    xl = np.arange(loc-(0.1*batch/2),loc,0.1)

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)

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.

Hardware Requirements

For the optimal user experience, we recommend 8 GB RAM.

Installing Anaconda on your system

All the exercises and activities in this book will be executed in Jupyter Notebooks. To install Jupyter on Windows, macOS, or Linux we first need to install Anaconda. Installing Anaconda will also install Python.

  1. Head to https://www.anaconda.com/distribution/ to install the Anaconda Navigator, which is an interface through which you can access your local Jupyter Notebook.
  2. Now, based on your operating system (Windows, macOS, or Linux) you need to download the Anaconda Installer. Select your operating system first and then choose the Python version. For this book, it is recommended that you use the latest version of Python.
    Figure 0.1: The Anaconda home screen

    Figure 0.1: The Anaconda home screen

  3. To check if Anaconda Navigator is correctly installed, look for Anaconda Navigator in your applications. Look for the icon shown below. However, note that the icon's aesthetics may vary slightly depending on your operating system.
    Figure 0.2 Anaconda Navigator icon

    Figure 0.2 Anaconda Navigator icon

  4. Click the icon to open Anaconda Navigator. It may take a while to load for the first time, but upon successful installation, you should see a similar screen:
Figure 0.3 Anaconda Navigator icon

Figure 0.3 Anaconda Navigator icon

Launching Jupyter Notebook

To launch Jupyter Notebook from the Anaconda Navigator, follow these steps:

  1. Open Anaconda Navigator. You should see the following screen:
    Figure 0.4: Anaconda installation screen

    Figure 0.4: Anaconda installation screen

  2. Now, click Launch under the Jupyter Notebook panel to start the notebook on your local system:
Figure 0.5: Jupyter Notebook launch option

Figure 0.5: Jupyter Notebook launch option

You have successfully installed Jupyter Notebook onto your system. You can also open a Jupyter Notebook by simply running the command jupyter notebook in the Terminal or Anaconda 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/303E4dD.

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.

Installing TensorFlow 2.0

Before installing TensorFlow 2.0, ensure that you have the latest version of pip installed on your system. You can check that by using the following command:

pip --version

To install TensorFlow 2.0, the version of pip on your system must be greater than 19.0. You can upgrade your version of pip using the following command on Windows, Linux, or macOS:

pip install --upgrade pip

Once upgraded, use the following command to install TensorFlow on Windows, Linux, or macOS:

pip install --upgrade tensorflow

On Linux and macOS, if elevated rights are required, use the following command:

sudo pip install --upgrade tensorflow

Note

TensorFlow is not supported on Windows with Python 2.7.

Installing Keras

To install Keras on Windows, macOS, or Linux, use the following command:

pip install keras

On Linux and macOS, if elevated rights are required, use the following command:

sudo pip install keras

Accessing the Code Files

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

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.

Note

This book contains certain code snippets that read data from CSV files. It is assumed that the CSV files are stored in the same folder as the Jupyter Notebook. In case you have stored them elsewhere, you'll have to modify the path.

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