-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating

The TensorFlow Workshop
By :

If you want to learn to build deep learning models in TensorFlow to solve real-world problems, then this is the book for you.
Beginning with an introduction to TensorFlow, this book gives you a tour of the basic mathematical operations of tensors, as well as various methods of data-preparation for modeling and time-saving model-development using TensorFlow resources. You will build regression and classification models, use regularization to prevent models from overfitting training data, and create convolutional neural networks to solve classification tasks on image datasets. Finally, you'll learn to implement pre-trained, recurrent, and generative models and create your own custom TensorFlow components to use within your models.
By the end of this book, you'll have the practical skills to build, train, and evaluate deep learning models using the TensorFlow framework.
Matthew Moocarme is an accomplished data scientist with more than eight years of experience in creating and utilizing machine learning models. He comes from a background in the physical sciences, in which he holds a Ph.D. in physics from the Graduate Center of CUNY. Currently, he leads a team of data scientists and engineers in the media and advertising space to build and integrate machine learning models for a variety of applications. In his spare time, Matthew enjoys sharing his knowledge with the data science community through published works, conference presentations, and workshops.
Anthony So is a renowned leader in data science. He has extensive experience in solving complex business problems using advanced analytics and AI in different industries including financial services, media, and telecommunications. He is currently the chief data officer of one of the most innovative fintech start-ups. He is also the author of several best-selling books on data science, machine learning, and deep learning. He has won multiple prizes at several hackathon competitions, such as Unearthed, GovHack, and Pepper Money. Anthony holds two master's degrees, one in computer science and the other in data science and innovation.
Anthony Maddalone is a research engineer at TieSet, a Silicon Valley-based leader in distributed artificial intelligence and federated learning. He is a former founder and CEO of a successful start-up. Anthony lives with his wife and two children in Colorado, where they enjoy spending time outdoors. He is also a master's candidate in analytics with a focus on industrial engineering at the Georgia Institute of Technology.
This TensorFlow book is for anyone who wants to develop their understanding of deep learning and get started building neural networks with TensorFlow. Basic knowledge of Python programming and its libraries, as well as a general understanding of the fundamentals of data science and machine learning, will help you grasp the topics covered in this book more easily.
Chapter 1, Introduction to Machine Learning with TensorFlow, introduces you to the mathematical concepts that underly TensorFlow and machine learning model development, which include tensors and linear algebra.
Chapter 2, Loading and Processing Data, teaches you how to load and process a variety of different data types including tabular, images, audio, and text so that they can be input into machine learning models.
Chapter 3, TensorFlow Development, introduces you to a variety of development tools that TensorFlow offers to aid your model building, including TensorBoard, TensorFlow Hub, and Google Colab. These tools can help speed up development as well as aiding your understanding of the architecture and performance of your models.
Chapter 4, Regression and Classification Models, guides you through building models using TensorFlow for regression and classification tasks. You will learn how to build simple models, which layers to use, and the appropriate loss functions to use for each.
Chapter 5, Classification Models, demonstrates how to build classification models using TensorFlow. You will learn how to customize the architecture of neural networks for binary, multi-class, or multi-label classification.
Chapter 6, Regularization and Hyperparameter Tuning, discusses the different methods that can help prevent models from overfitting, such as regularization, dropout, or early stopping. You will also learn how to perform automatic hyperparameter tuning.
Chapter 7, Convolutional Neural Networks, demonstrates how to build neural networks with convolutional layers. These networks are popular due to their good performance when working with images because of the convolutional layers they contain.
Chapter 8, Pre-Trained Networks, teaches you how to leverage pre-trained models in order to achieve better performance without having to train a model from scratch.
Chapter 9, Recurrent Neural Networks, introduces a different type of deep learning architecture known as recurrent neural networks, which are best suited for sequential data such as time-series or text.
Chapter 10, Custom TensorFlow Components, expands your repertoire by teaching you how to build your own custom TensorFlow components such as loss functions and neural network layers.
Chapter 11, Generative Models, shows you how you can generate new and novel data by training models on a dataset to discover the underlying patterns and representations. The trained model will then be able to generate convincingly real examples for itself that are completely novel.
Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, and user input are shown as follows:
"TensorFlow can be used in Python by importing certain libraries. You can import libraries in Python using the import
statement."
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:
int_variable = tf.Variable(4113, tf.int16) int_variable
New important words are shown like this: "Backpropagation is the process of determining the derivative of the loss with respect to the model parameter."
Key parts of code snippets are emboldened as follows:
df = pd.read_csv('Bias_correction_ucl.csv')
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,
year_dummies = pd.get_dummies(df['Date'].dt.year, \ prefix='year') year_dummies
Comments are added into code to help explain specific bits of logic. Single-line comments are denoted using the #
symbol, as follows:
# Importing the matplotlib library import matplotlib.pyplot as plt
For an optimal experience, we recommend the following hardware configuration:
Download the code files from GitHub at https://packt.link/Z7pcq. Refer to these code files for the complete code bundle. The files here contain the exercises, activities, and some intermediate code for each chapter. This can be a useful reference when you become stuck.
On the GitHub repo's page, you can click the green Code
button and then click the Download ZIP
option to download the complete code as a ZIP file to your disk (refer to Figure 0.1). You can then extract these code files to a folder of your choice, for example, C:\Code
.
Figure 0.1: Download ZIP option
On your system, the extracted ZIP file should contain all the files present in the GitHub repository:
Figure 0.2: GitHub code directory structure
Before you explore the book in detail, you need to set up specific software and tools. In the following section, you will see how to do that.
The code for all the exercises and activities in this book can be executed using the Jupyter Notebook. You'll first need to install Anaconda Navigator, which is an interface through which you can access your Jupyter notebooks. Anaconda Navigator will be installed as part of Anaconda Individual Edition, which is an open source Python distribution platform available for Windows, macOS, and Linux. Installing Anaconda will also install Python. Head to https://www.anaconda.com/distribution/:
Download
button (annotated by 1). Make sure you are downloading the Individual Edition
.
Figure 0.3: Anaconda home page
Get Additional Installers
link at the bottom of the box (refer to Figure 0.3). The page should scroll down to a section (refer to Figure 0.4) that lets you choose from various options based on the operating system and configuration you desire. For this book, it is recommended that you use the latest version of Python (3.8 or higher).Figure 0.4: Downloading Anaconda based on the OS
Figure 0.5: Anaconda setup
PATH
. This will let you run Anaconda-specific commands (like conda
) from the default command prompt. If you have Python installed or have installed an earlier version of Anaconda in the past, it is recommended that you leave it unchecked (you may run Anaconda commands from the Anaconda Prompt application instead). The installation may take a while depending on your system configuration.Figure 0.6: Anaconda installation steps
For more detailed instructions, you may refer to the official documentation for Linux by clicking this link (https://docs.anaconda.com/anaconda/install/linux/); for macOS using this link (https://docs.anaconda.com/anaconda/install/mac-os/); for Windows using this link (https://docs.anaconda.com/anaconda/install/windows/).
Anaconda Navigator
in your applications. Look for an application that has the following icon. Depending on your operating system, the icon's aesthetics may vary slightly.Figure 0.7: Anaconda Navigator icon
You can also search for the application using your operating system's search functionality. For example, on Windows 10, you can use the Windows Key + S combination and type in Anaconda Navigator. On macOS, you can use Spotlight search. On Linux, you can open the terminal and type the anaconda-navigator
command and press the Return key.
Figure 0.8: Searching for Anaconda Navigator on Windows 10
For detailed steps on how to verify if Anaconda Navigator is installed, refer to the following link: https://docs.anaconda.com/anaconda/install/verify-install/.
Figure 0.9: Anaconda Navigator screen
If you have more questions about the installation process, you may refer to the list of frequently asked questions from the Anaconda documentation: https://docs.anaconda.com/anaconda/user-guide/faq/.
Once Anaconda Navigator is open, you can launch the Jupyter Notebook interface from this screen. The following steps will show you how to do that:
Figure 0.10: Anaconda Navigator screen
Launch
under the Jupyter Notebook
panel to start the notebook interface on your local system:Figure 0.11: Jupyter notebook launch option
Launch
button, you'll notice that even though nothing changes in the window shown in the preceding screenshot, a new tab opens up in your default browser. This is known as the Notebook Dashboard. It will, by default, open to your root folder. For Windows users, this path would be something similar to C:\Users\<username>
. On macOS and Linux, it will be /home/<username>/
.Figure 0.12: Notebook Dashboard
Note that you can also open a Jupyter notebook by simply running the command jupyter notebook
in the terminal or command prompt. Or, you can search for Jupyter Notebook
in your applications just like you did in Figure 0.8.
.inpyb
extension). The menus here are quite simple to use: Figure 0.13: Jupyter notebook navigator menu options walk-through
If you make any changes to the directory using your operating system's file explorer and the changed file isn't showing up in the Jupyter Notebook navigator, click the Refresh Notebook List
button (annotated as 1). To quit, click the Quit button
(annotated as 2). To create a new file (a new Jupyter notebook), you can click the New
button (annotated as 3).
New
button will open a dropdown menu as follows:Figure 0.14: Creating a new Jupyter notebook
You can get started and create your first notebook by selecting Python 3
; however, it's recommended that you also install the virtual environment we've provided to help you install all the packages required for the title. The following section will show you how to install it.
Note
A detailed tutorial on the interface and the keyboard shortcuts for Jupyter notebooks can be found here: https://jupyter-notebook.readthedocs.io/en/stable/notebook.html#the-jupyter-notebook.
As you run the code for the exercises and activities, you'll notice that even after installing Anaconda, there are certain libraries that you'll need to install separately as you progress through the book. Then again, you may already have these libraries installed, but their versions may be different from the ones we've used, which may lead to varying results. That's why we've provided an environment.yml
file with this book that will:
You can download the environment.yml
file by clicking the following link: https://packt.link/Z7pcq.
Save this file, ideally in the same folder where you'll be running the code for this book. If you've downloaded the code from GitHub as detailed in the Downloading the Code Bundle section, this file should already be present in the parent directory, and you won't need to download it separately.
To set up the environment, follow these steps:
Start
menu and searching for Anaconda Prompt
. Figure 0.15: Searching for Anaconda Prompt on Windows
A new terminal like the following should open. By default, it will start in your home directory:
Figure 0.16: Anaconda terminal prompt
In the case of Linux, it will look like the following:
Figure 0.17: Terminal in Linux
environment.yml
file on your computer using the cd
command. Say you've saved the file in Documents\The-TensorFlow-Workshop
. In that case, you'll type the following command in the prompt and press Enter:cd Documents\The-TensorFlow-Workshop
Note that the command may vary slightly based on your directory structure and your operating system.
conda
environment by typing or pasting the following command in the terminal. Press Enter to run the command:conda env create -f environment.yml
This will install the tensorflow
virtual environment along with the libraries that are required to run the code in this book. If you see a prompt asking you to confirm before proceeding, type y
and press Enter to continue creating the environment. Depending on your system configuration, it may take a while for the process to complete.
Note
For a complete list of conda
commands, visit the following link: https://conda.io/projects/conda/en/latest/index.html.
For a detailed guide on how to manage conda
environments, please visit the following link: https://conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html.
tensorflow
:conda activate tensorflow
If the installation is successful, you'll see the environment name in brackets change from base
to tensorflow
:
Figure 0.18: Environment name showing up in the shell
ipykernel
in the newly activated conda
environment:pip install ipykernel
Note
On macOS and Linux, you'll need to specify pip3
instead of pip
.
ipykernel
as a Jupyter kernel: python -m ipykernel install --user --name=tensorflow
conda install pywin32
tensorflow
kernel when you start your Jupyter notebook.Figure 0.19: Selecting the tensorflow kernel
A new tab will open with a fresh, untitled Jupyter notebook where you can start writing your code:
Figure 0.20: A new Jupyter notebook
Feedback from our readers is always welcome.
General feedback: If you have any questions about this book, please mention the book title in the subject of your message and email us at [email protected]
.
Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you could report this to us. Please visit www.packtpub.com/support/errata and complete the form.
Piracy: If you come across any illegal copies of our works in any form on the internet, we would be grateful if you could provide us with the location address or website name. Please contact us at [email protected]
with a link to the material.
If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.
Let us know what you think by leaving a detailed, impartial review on Amazon. We appreciate all feedback – it helps us continue to make great products and help aspiring developers build their skills. Please spare a few minutes to give your thoughts – it makes a big difference to us. You can leave a review by clicking the following link: https://packt.link/r/1800205252.
Change the font size
Change margin width
Change background colour