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

Arrow left icon
Profile Icon Hua Profile Icon Ahmed Profile Icon Ul Azeem
Arrow right icon
€18.99 per month
Full star icon Full star icon Full star icon Half star icon Empty star icon 3.8 (8 Ratings)
Paperback Nov 2017 304 pages 1st Edition
eBook
€17.99 €26.99
Paperback
€32.99
Subscription
Free Trial
Renews at €18.99p/m
Arrow left icon
Profile Icon Hua Profile Icon Ahmed Profile Icon Ul Azeem
Arrow right icon
€18.99 per month
Full star icon Full star icon Full star icon Half star icon Empty star icon 3.8 (8 Ratings)
Paperback Nov 2017 304 pages 1st Edition
eBook
€17.99 €26.99
Paperback
€32.99
Subscription
Free Trial
Renews at €18.99p/m
eBook
€17.99 €26.99
Paperback
€32.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
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 : 9781786462961
Vendor :
Google
Category :
Languages :
Tools :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

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

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.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
€189.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
€264.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 115.97
TensorFlow 1.x Deep Learning Cookbook
€36.99
Machine Learning with TensorFlow 1.x
€32.99
TensorFlow Machine Learning Cookbook
€45.99
Total 115.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

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.