Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Machine Learning with TensorFlow 1.x
Machine Learning with TensorFlow 1.x

Machine Learning with TensorFlow 1.x: Second generation machine learning with Google's brainchild - TensorFlow 1.x

eBook
$24.99 $35.99
Paperback
$43.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

Machine Learning with TensorFlow 1.x

Your First Classifier

With TensorFlow now installed, we need to kick the tires. We will do so by writing our first classifier and then training and testing it from start to finish!

Our first classifier will be a handwriting recognizer. One of the most common datasets to train is the MNIST handwritten digits dataset. We'll be using a similar dataset called notMNIST, which features the first ten letters of the English alphabet.

The key parts

There are three key parts to most machine learning classifiers, which are as follows:

  • The training pipeline
  • The neural network setup and training outputs
  • The usage pipeline

The training pipeline obtains data, stages it, cleanses it, homogenizes it, and puts it in a format acceptable to the neural network. Do not be surprised if the training pipeline takes 80% to 85% of your effort initially—this is the reality of most machine learning work. Generally, the more realistic the training data, the more time spent on the training pipeline. In enterprise settings, the training pipeline might be an ongoing effort being enhanced perpetually. This is especially true as datasets get larger.

The second part, the neural network setup, and training, can be quick for routine problems and can be a research-grade effort for harder problems. You may find yourself making small...

Obtaining training data

Machine learning requires training data—often a lot of training data. One of the great things about machine learning is the availability of standard training datasets. These are often used to benchmark node models and configurations and provide a consistent yardstick to gauge performance against previous progress. Many of the datasets are also used in annual global competitions.

This chapter uses training data, which is kindly provided by Yaroslav Bulatov, a machine learning researcher.

Downloading training data

You should start by downloading the training data from the following links:

We will download this programmatically, but we should start with a manual download just to peek at the data and structure of the archive. This will be important when we write the pipeline, as we'll need to understand the structure so we can manipulate the data.

The small set is ideal for peeking. You can do this via the following command line, or just use a browser to download the file with an unarchiver to extract the files (I suggest getting familiarized with the command line as all of this needs to be automated):

cd ~/workdir
wget http://yaroslavvb.com/upload/notMNIST/notMNIST_small.tar.gz
tar xvf notMNIST_small.tar.gz

The preceding command line will...

Understanding classes

The classifier we're writing seeks to assign unknown images to a class. Classes can be of the following types:

  • Feline versus canine
  • Two versus seven
  • Tumor versus normal
  • Smiling versus frowning

In our case, we are considering each letter a class for a total of 10 classes. The training set will reveal 10 subfolders with thousands of images underneath each subfolder. The name of the subfolder is important as it is the label for each of the images. These details will be used by the pipeline to prepare data for TensorFlow.

Automating the training data setup

Ideally, we will want the entire process automated. This way, we can easily run the process end to end on any computer we use without having to...

Additional setup

The next part will focus on image processing and manipulation. This requires some extra libraries you may not have. At this point, it may make sense to just install all the typical packages required in scientific computing, which can be done as follows:

sudo apt-get install python-numpy python-scipy python-matplotlib 
ipython ipython-notebook python-pandas python-sympy python-nose

Additionally, install the image processing library, some external matrix mathematics libraries, and underlying requirements, which can be done as follows:

sudo pip install ndimage
sudo apt-get install libatlas3-base-dev gcc gfortran g++

Converting images to matrices

Much of machine learning is just operations on matrices. We will...

Logical stopping points

Downloading our training file took a long time. Even extracting all the images took a while. To avoid repeating all this, we will try to do all the work just once and then create pickle files—archives of the Python data structures.

The following procedure runs through each class in our training and test set and creates a separate pickle file for each. In future runs, we'll just begin from here:

 def makePickle(imgSrcPath): 
    data_folders = [os.path.join(tst_files, d) for d in 
os.listdir(tst_files) if os.path.isdir(os.path.join(tst_files,
d))] dataset_names = [] for folder in data_folders: set_filename = folder + '.pickle' dataset_names.append(set_filename) print('Pickling %s.' % set_filename) dataset = loadClass(folder) try: with open(set_filename...

The machine learning briefcase

We just created nice, clean, pickle files with preprocessed images to train and test our classifier. However, we've ended up with 20 pickle files. There are two problems with this. First, we have too many files to keep track of easily. Secondly, we've only completed part of our pipeline, where we've processed our image sets but have not prepared a TensorFlow consumable file.

Now we will need to create our three major sets—the training set, the validation set, and the test set. The training set will be used to nudge our classifier, while the validation set will be used to gauge progress on each iteration. The test set will be kept secret until the end of the training, at which point, it will be used to test how well we've trained the model.

The code to do all this is long, so we'll leave you to review the Git repository...

Training day

Now, we arrive at the fun part—the neural network. The complete code to train this model is available at the following link: https://github.com/mlwithtf/mlwithtf/blob/master/chapter_02/training.py

To train the model, we'll import several more modules:

 import sys, os
import tensorflow as tf
import numpy as np
sys.path.append(os.path.realpath('..'))
import data_utils
import logmanager

Then, we will define a few parameters for the training process:

 batch_size = 128
num_steps = 10000
learning_rate = 0.3
data_showing_step = 500

After that, we will use the data_utils package to load the dataset that was downloaded in the previous section:

 dataset, image_size, num_of_classes, num_of_channels =  
data_utils.prepare_not_mnist_dataset(root_dir="..")
dataset = data_utils.reformat(dataset, image_size, num_of_channels,
num_of_classes)
print...

Saving the model for ongoing use

To save variables from the TensorFlow session for future use, you can use the Saver() function, which is as follows:

 saver = tf.train.Saver() 

Later, you can retrieve the state of the model and avoid tedious retraining by restoring the following checkpoint:

 ckpt = tf.train.get_checkpoint_state(FLAGS.checkpoint_dir) 
 if ckpt and ckpt.model_checkpoint_path: 
 saver.restore(sess, ckpt.model_checkpoint_path) 

Why hide the test set?

Notice how we did not use the test set until the last step. Why not? This is a pretty important detail to ensure that the test remains a good one. As we iterate through the training set and nudge our classifier one way or another, we can sometimes wrap the classifier around the images or overtrain. This happens when you learn the training set rather than learn the features inside each of the classes.

When we overtrain, our accuracy on the iterative rounds of the training set will look promising, but that is all false hope. Having a never-before-seen test set should introduce reality back into the process. Great accuracy on the training set followed by poor results on the test set suggests overfitting.

This is why we've kept a separate test set. It helps indicate the real accuracy of our classifier. This is also why you should never shuffle your dataset...

Using the classifier

We will demonstrate the usage of the classifier with notMNIST_small.tar.gz, which becomes the test set. For ongoing use of the classifier, you can source your own images and run them through a similar pipeline to test, not train.

You can create some 28x28 images yourself and place them into the test set for evaluation. You will be pleasantly surprised!

The practical issue with field usage is the heterogeneity of images in the wild. You may need to find images, crop them, downscale them, or perform a dozen other transformations. This all falls into the usage pipeline, which we discussed earlier.

Another technique to cover larger images, such as finding a letter on a page-sized image, is to slide a small window across the large image and feed every subsection of the image through the classifier.

We'll be taking our models into production in future chapters...

Deep diving into the network

Notice how we achieved 86% accuracy. This is a great result for two hours of work, but we can do much better. Much of the future potential is in changing the neural network. Our preceding application used a fully-connected setup, where each node on a layer is connected to each node on the previous layer and looks like this:

As you will learn with more complex network setups in coming chapters, this setup is fast but not ideal. The biggest issue is the large number of parameters, which can cause overfitting of the model on the training data.

Skills learned

You should have learned these skills in the chapter:

  • Preparing training and test data
  • Creating a training set consumable by TensorFlow
  • Setting up a basic neural network graph
  • Training the TensorFlow classifier
  • Validating the classifier
  • Piping in real-world data

Summary

Superb progress! We just built a handwriting classifier that would have been world class a decade ago. Also, we built an entire pipeline around the process to fully automate the training setup and execution. This means that our program can be migrated to almost any server and continue to function almost turn-key.

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Enter the new era of second-generation machine learning with Python with this practical and insightful guide
  • Set up TensorFlow 1.x for actual industrial use, including high-performance setup aspects such as multi-GPU support
  • Create pipelines for training and using applying classifiers using raw real-world data

Description

Google's TensorFlow is a game changer in the world of machine learning. It has made machine learning faster, simpler, and more accessible than ever before. This book will teach you how to easily get started with machine learning using the power of Python and TensorFlow 1.x. Firstly, you’ll cover the basic installation procedure and explore the capabilities of TensorFlow 1.x. This is followed by training and running the first classifier, and coverage of the unique features of the library including data ?ow graphs, training, and the visualization of performance with TensorBoard—all within an example-rich context using problems from multiple industries. You’ll be able to further explore text and image analysis, and be introduced to CNN models and their setup in TensorFlow 1.x. Next, you’ll implement a complete real-life production system from training to serving a deep learning model. As you advance you’ll learn about Amazon Web Services (AWS) and create a deep neural network to solve a video action recognition problem. Lastly, you’ll convert the Caffe model to TensorFlow and be introduced to the high-level TensorFlow library, TensorFlow-Slim. By the end of this book, you will be geared up to take on any challenges of implementing TensorFlow 1.x in your machine learning environment.

Who is this book for?

This book is for data scientists and researchers who are looking to either migrate from an existing machine learning library or jump into a machine learning platform headfirst. The book is also for software developers who wish to learn deep learning by example. Particular focus is placed on solving commercial deep learning problems from several industries using TensorFlow’s unique features. No commercial domain knowledge is required, but familiarity with Python and matrix math is expected.

What you will learn

  • Explore how to use different machine learning models to ask different questions of your data
  • Learn how to build deep neural networks using TensorFlow 1.x
  • Cover key tasks such as clustering, sentiment analysis, and regression analysis using TensorFlow 1.x
  • Find out how to write clean and elegant Python code that will optimize the strength of your algorithms
  • Discover how to embed your machine learning model in a web application for increased accessibility
  • Learn how to use multiple GPUs for faster training using AWS

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Nov 21, 2017
Length: 304 pages
Edition : 1st
Language : English
ISBN-13 : 9781786461988
Vendor :
Google
Category :
Languages :
Tools :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Nov 21, 2017
Length: 304 pages
Edition : 1st
Language : English
ISBN-13 : 9781786461988
Vendor :
Google
Category :
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total $ 153.97
TensorFlow 1.x Deep Learning Cookbook
$48.99
Machine Learning with TensorFlow 1.x
$43.99
TensorFlow Machine Learning Cookbook
$60.99
Total $ 153.97 Stars icon
Banner background image

Table of Contents

12 Chapters
Getting Started with TensorFlow Chevron down icon Chevron up icon
Your First Classifier Chevron down icon Chevron up icon
The TensorFlow Toolbox Chevron down icon Chevron up icon
Cats and Dogs Chevron down icon Chevron up icon
Sequence to Sequence Models-Parlez-vous Français? Chevron down icon Chevron up icon
Finding Meaning Chevron down icon Chevron up icon
Making Money with Machine Learning Chevron down icon Chevron up icon
The Doctor Will See You Now Chevron down icon Chevron up icon
Cruise Control - Automation Chevron down icon Chevron up icon
Go Live and Go Big Chevron down icon Chevron up icon
Going Further - 21 Problems Chevron down icon Chevron up icon
Advanced Installation Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Half star icon Empty star icon 3.8
(8 Ratings)
5 star 62.5%
4 star 0%
3 star 12.5%
2 star 0%
1 star 25%
Filter icon Filter
Top Reviews

Filter reviews by




Igidok Dec 14, 2017
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This Book is very detailed and walks you through the Tensorflow library very well. I am fairly new to Machine Learning and I think this is a good step by guide.
Amazon Verified review Amazon
Amazon Customer Feb 28, 2018
Full star icon Full star icon Full star icon Full star icon Full star icon 5
A very useful resource!
Amazon Verified review Amazon
K. Witek Feb 28, 2018
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Always interested in what technologies Google is using to run its systems and this is one of them. Machine Learning and AI are the next big thing and this is one of the enabling technologies that's "hot" right now; and for the right reasons. This book is a good way to learn and get established with this technology, although you do have to be fairly technical to make the most out of it. What is particularly interesting for me is how Machine Learning bridges the gap between how computer think and how people think - Machine learning is comprised of algorithms that teach computers to perform tasks that human beings do naturally on a daily basis. This book explores this very aspect and it is very eye opening.
Amazon Verified review Amazon
SRA Feb 28, 2018
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I’m brand new to using TensorFlow so this was a good Primer. It also features advanced chapters to take things forward if you’re already familiar with the basics. There were some simplistic examples which were perfect for a starter. All in all, I would recommend this book to new learners and advanced learners as both can benefit.
Amazon Verified review Amazon
Madeeha Husain Feb 28, 2018
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This book is great and has helped me immensely. I’m quite new to TensorFlow and this book has been so much help!
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.