Book Image

The Applied TensorFlow and Keras Workshop

By : Harveen Singh Chadha, Luis Capelo
Book Image

The Applied TensorFlow and Keras Workshop

By: Harveen Singh Chadha, Luis Capelo

Overview of this book

Machine learning gives computers the ability to learn like humans. It is becoming increasingly transformational to businesses in many forms, and a key skill to learn to prepare for the future digital economy. As a beginner, you’ll unlock a world of opportunities by learning the techniques you need to contribute to the domains of machine learning, deep learning, and modern data analysis using the latest cutting-edge tools. The Applied TensorFlow and Keras Workshop begins by showing you how neural networks work. After you’ve understood the basics, you will train a few networks by altering their hyperparameters. To build on your skills, you’ll learn how to select the most appropriate model to solve the problem in hand. While tackling advanced concepts, you’ll discover how to assemble a deep learning system by bringing together all the essential elements necessary for building a basic deep learning system - data, model, and prediction. Finally, you’ll explore ways to evaluate the performance of your model, and improve it using techniques such as model evaluation and hyperparameter optimization. By the end of this book, you'll have learned how to build a Bitcoin app that predicts future prices, and be able to build your own models for other projects.
Table of Contents (6 chapters)

About the Book

Machine learning gives computers the ability to learn like humans. It is becoming increasingly transformational to businesses in many forms, and a key skill to learn to prepare for the future digital economy.

As a beginner, you'll unlock a world of opportunities by learning the techniques you need to contribute to the domains of machine learning, deep learning, and modern data analysis using the latest cutting-edge tools.

The Applied TensorFlow and Keras Workshop begins by showing you how neural networks work. After you've understood the basics, you will train a few networks by altering their hyperparameters. To build on your skills, you'll learn how to select the most appropriate model to solve the problem in hand. While tackling advanced concepts, you'll discover how to assemble a deep learning system by bringing together all the essential elements necessary for building a basic deep learning system - data, model, and prediction. Finally, you'll explore ways to evaluate the performance of your model, and improve it using techniques such as model evaluation and hyperparameter optimization.

By the end of this book, you'll have learned how to build a Bitcoin app that predicts future prices, and be able to build your own models for other projects.

Audience

If you are a data scientist or a machine learning and deep learning enthusiast, who is looking to design, train, and deploy TensorFlow and Keras models into real-world applications, then this workshop is for you. Knowledge of computer science and machine learning concepts and experience in analyzing data will help you to understand the topics explained in this book with ease.

About the Chapters

Chapter 1, Introduction to Neural Networks and Deep Learning, gets us to select a TensorFlow-trained neural network using TensorBoard and trains the neural network by varying epochs and learning rates. This will give you hands-on experience of how to train a highly performant neural network and allow you to explore some of its limitations.

Chapter 2, Real-World Deep Learning: Predicting the Price of Bitcoin, teaches us how to assemble a complete deep learning system, from data input to prediction. The model created will serve as a benchmark from which we will be able to make improvements.

Chapter 3, Real-World Deep Learning: Evaluating the Bitcoin Model, focuses on how to evaluate a neural network model. Using hyperparameter tuning, we will improve the performance of the network. However, before altering any parameters, we need to measure how the model performs. By the end of this chapter, you will be able to evaluate a model using different functions and techniques.

Chapter 4, Productization, covers the handling of new data. We will create a model that is able to learn continuously from the patterns it's shown and make better predictions. We will use a web application as an example to demonstrate how to deploy deep learning models.

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:

"After activating your virtual environment, make sure that the right components are installed by executing pip over the requirements.txt file."

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

A block of code is set as follows:

$ python -m venv venv
$ venv/bin/activate

New terms and important words are shown like this:

"In machine learning, it is common to define two distinct terms: parameter and hyperparameter."

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.

Installation

The following section will help you to install Python in Windows, macOS, and Linux systems.

Installing Python on Windows

Python is installed on Windows as follows:

  1. Ensure that you select Python 3.7 (for compatibility with TensorFlow 2.0) from the download page on the official installation page at https://www.anaconda.com/distribution/#windows.
  2. Ensure that you install the correct architecture for your computer system; that is, either 32-bit or 64-bit. You can find out this information in the System Properties window of your OS.
  3. After you download the installer, simply double-click on the file and follow the user-friendly prompts on screen.

Installing Python on Linux

To install Python on Linux, you have a couple of good options; namely, Command Prompt and Anaconda.

Use Command Prompt as follows:

  1. Open Command Prompt and verify that p\Python 3 is not already installed by running python3 --version.
  2. To install Python 3, run this command:
    sudo apt-get update
    sudo apt-get install python3.7
  3. If you encounter problems, there are numerous resources online that can help you to troubleshoot the issue.

Alternatively, you can install Anaconda Linux by downloading the installer from https://www.anaconda.com/distribution/#linux and following the instructions.

Installing Python on macOS

Similar to Linux, you have a couple of methods for installing Python on a Mac. One option for installing Python on macOS X is as follows:

  1. Open the Terminal for Mac by pressing CMD + Spacebar, type terminal in the open search box, and hit Enter.
  2. Install Xcode through the command line by running xcode-select --install.
  3. The easiest way to install Python 3 is by using Homebrew, which is installed through the command line by running ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)".
  4. Add Homebrew to your $PATH environment variable. Open your profile in the command line by running sudo nano ~/.profile and inserting export PATH="/usr/local/opt/python/libexec/bin:$PATH" at the bottom.
  5. The final step is to install Python. In the command line, run brew install python.

Again, you can also install Python via the Anaconda installer, available from https://www.anaconda.com/distribution/#macos.

Installing pip

Installing Python via the Anaconda installer comes with pip (the package manager for Python) pre-installed. However, if you have installed Python directly, you will have to install pip manually. The steps to install pip are as follows:

  1. Go to https://bootstrap.pypa.io/get-pip.py and save the file as get-pip.py.
  2. Go to the folder where you have saved get-pip.py. Open the command line in that folder (that's Bash for Linux users and Terminal for Mac users).
  3. Execute the following command in the command line:
    python get-pip.py
  4. Please note that you should have Python installed before executing this command.
  5. Once pip is installed, you can install the desired libraries. To install pandas, you can simply execute pip install pandas. To install a specific version of a library, for example, version 0.24.2 of pandas, you can execute pip install pandas=0.24.2.

Jupyter Notebook

If you have not installed Python via the Anaconda installer, you will need to install Jupyter manually. Refer to the Alternative for experienced Python users: Installing Jupyter with pip, section at https://jupyter.readthedocs.io/en/latest/install.html#id4.

JupyterLab

The Anaconda distribution includes JupyterLab, which allows you to run Jupyter Notebooks. Jupyter Notebooks are accessed via your browser and allow you to interactively run code as well as embed images and text in an integrated environment.

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/3haRJp0.

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.

Accessing the Code Files

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

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].