Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying The TensorFlow Workshop
  • Table Of Contents Toc
  • Feedback & Rating feedback
The TensorFlow Workshop

The TensorFlow Workshop

By : Matthew Moocarme, Abhranshu Bagchi, Anthony So , Maddalone
4.6 (25)
close
close
The TensorFlow Workshop

The TensorFlow Workshop

4.6 (25)
By: Matthew Moocarme, Abhranshu Bagchi, Anthony So , Maddalone

Overview of this book

Getting to grips with tensors, deep learning, and neural networks can be intimidating and confusing for anyone, no matter their experience level. The breadth of information out there, often written at a very high level and aimed at advanced practitioners, can make getting started even more challenging. If this sounds familiar to you, The TensorFlow Workshop is here to help. Combining clear explanations, realistic examples, and plenty of hands-on practice, it’ll quickly get you up and running. You’ll start off with the basics – learning how to load data into TensorFlow, perform tensor operations, and utilize common optimizers and activation functions. As you progress, you’ll experiment with different TensorFlow development tools, including TensorBoard, TensorFlow Hub, and Google Colab, before moving on to solve regression and classification problems with sequential models. Building on this solid foundation, you’ll learn how to tune models and work with different types of neural network, getting hands-on with real-world deep learning applications such as text encoding, temperature forecasting, image augmentation, and audio processing. By the end of this deep learning book, you’ll have the skills, knowledge, and confidence to tackle your own ambitious deep learning projects with TensorFlow.
Table of Contents (13 chapters)
close
close
Preface
chevron up

About the Book

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. 

About the Authors

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.

Who This Book Is For

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.

About the Chapters

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.

Conventions

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')

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,

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

Minimum Hardware Requirements

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

  • Processor: Dual-core or better
  • Memory: 4 GB RAM
  • Storage: 10 GB available space

Downloading the Code Bundle

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

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

Figure 0.2: GitHub code directory structure

Setting Up Your Environment

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.

Installing Anaconda on Your System

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/:

  1. From the page that opens, click the Download button (annotated by 1). Make sure you are downloading the Individual Edition.

    Figure 0.3: Anaconda home page

    Figure 0.3: Anaconda home page

  2. The installer should start downloading immediately. The website will, by default, choose an installer based on your system configuration. If you prefer downloading Anaconda for a different operating system (Windows, macOS, or Linux) and system configuration (32- or 64-bit), click the 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.4: Downloading Anaconda based on the OS

  3. Follow the installation steps presented on the screen.
    Figure 0.5: Anaconda setup

    Figure 0.5: Anaconda setup

  4. On Windows, if you've never installed Python on your system before, you can select the checkbox that prompts you to add Anaconda to your 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

    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/).

  5. To check if Anaconda Navigator is correctly installed, look for 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

    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

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

  6. 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.9: Anaconda Navigator screen

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

Launching Jupyter Notebook

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:

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

    Figure 0.10: Anaconda Navigator screen

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

    Figure 0.11: Jupyter notebook launch option

  3. On clicking the 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

    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.

  4. You can use this dashboard as a file explorer to navigate to the directory where you have downloaded or stored the code files for the book (refer to the Downloading the Code Bundle section on how to download the files from GitHub). Once you have navigated to your desired directory, you can start by creating a new notebook. Alternatively, if you've downloaded the code from our repository, you can open an existing notebook as well (notebook files will have a .inpyb extension). The menus here are quite simple to use:
    Figure 0.13: Jupyter notebook navigator menu options walk-through

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

  5. Clicking the New button will open a dropdown menu as follows:
    Figure 0.14: Creating a new Jupyter notebook

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.

Installing the tensorflow Virtual Environment

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:

  1. Install all the packages and libraries required for this book at once.
  2. Make sure that the version numbers of your libraries match the ones we've used to write the code for this book.
  3. Make sure that the code you write based on this course remains separate from any other coding environment you may have.

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:

  1. On macOS, open Terminal from Launchpad (you can find more information about Terminal here: https://support.apple.com/en-in/guide/terminal/apd5265185d-f365-44cb-8b09-71a064a42125/mac). On Linux, open the Terminal application that's native to your distribution. On Windows, you can open Anaconda Prompt instead by simply searching for the application. You can do this by opening the Start menu and searching for Anaconda Prompt.
    Figure 0.15: Searching for Anaconda Prompt on Windows

    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

    Figure 0.16: Anaconda terminal prompt

    In the case of Linux, it will look like the following:

    Figure 0.17: Terminal in Linux

    Figure 0.17: Terminal in Linux

  2. In the terminal, navigate to the directory where you've saved the 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.

  3. Now that you've navigated to the correct folder, create a new 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.

  4. Once complete, type or paste the following command in the shell to activate the newly installed environment – 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

    Figure 0.18: Environment name showing up in the shell

  5. Run the following command to install ipykernel in the newly activated conda environment:
    pip install ipykernel

    Note

    On macOS and Linux, you'll need to specify pip3 instead of pip.

  6. In the same environment, run the following command to add ipykernel as a Jupyter kernel:
    python -m ipykernel install --user --name=tensorflow
  7. Windows only: If you're on Windows, type or paste the following command. Otherwise, you may skip this step and exit the terminal:
    conda install pywin32
  8. Select the created tensorflow kernel when you start your Jupyter notebook.
    Figure 0.19: Selecting the tensorflow kernel

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

Figure 0.20: A new Jupyter notebook

Get in Touch

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.

Please Leave a Review

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.

Unlock full access

Continue reading for free

A Packt free trial gives you instant online access to our library of over 7000 practical eBooks and videos, constantly updated with the latest in tech

Create a Note

Modal Close icon
You need to login to use this feature.
notes
bookmark search playlist download font-size

Change the font size

margin-width

Change margin width

day-mode

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Delete Bookmark

Modal Close icon
Are you sure you want to delete it?
Cancel
Yes, Delete

Delete Note

Modal Close icon
Are you sure you want to delete it?
Cancel
Yes, Delete

Edit Note

Modal Close icon
Write a note (max 255 characters)
Cancel
Update Note

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY