Machine Learning with TensorFlow 1.x Q M Quan Hua, Shams Ul Azeem, Saif Ahmed u an a H c u h a , Sh in am e Gthoeo wgloe’rsl dT eonf smoraFclhoiwne isle aar ngianmg.e I tc hhaans gmera dine Things you will learn: s Ul A Lea z machine learning faster, simpler, and more • Explore how to use different machine ee r m n accessible than ever before. This book will learning models to ask different , S in Machine tmeaacchhi nyeo lue ahronwin gt uos ienags tilhye gpeotw setra rotfe Pdy twhiothn questions of your data aif A g h and TensorFlow 1.x. m w e d i t Firstly, you’ll cover the basic installation • Learn how to build deep neural h procedure and explore the capabilities networks using TensorFlow 1.x T Learning with of TensorFlow 1.x. This is followed by e training and running the fi rst classifi er, n and coverage of the unique features of the s • Cover key tasks such as clustering, o library including data fl ow graphs, training, r and the visualization of performance with sentiment analysis, and regression F TensorBoard—all within an example-rich analysis using TensorFlow 1.x l o context using problems from multiple w TensorFlow 1.x industries. You’ll be able to further explore 1 text and image analysis, and be introduced • Find out how to write clean and .x to CNN models and their setup in elegant Python code that will optimize TensorFlow 1.x. Next, you’ll implement a the strength of your algorithms 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 • Discover how to embed your machine network to solve a video action recognition learning model in a web application problem. Lastly, you’ll convert the Caffe for increased accessibility model to TensorFlow and be introduced Second generation machine learning with Google’s to the high-level TensorFlow library, brainchild – TensorFlow 1.x TensorFlow-Slim. • Learn how to use multiple GPUs for By the end of this book, you will be geared faster training using AWS up to take on any challenges of implementing TensorFlow 1.x in your machine learning environment. www.packtpub.com Machine Learning with TensorFlow 1.x Second generation machine learning with Google's brainchild - TensorFlow 1.x Quan Hua Shams Ul Azeem Saif Ahmed BIRMINGHAM - MUMBAI Machine Learning with TensorFlow 1.x Copyright © 2017 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. First published: November 2017 Production reference: 1171117 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78646-296-1 www.packtpub.com Credits Authors Copy Editor Quan Hua Zainab Bootwala Shams Ul Azeem Saif Ahmed Reviewer Project Coordinator Nathan Lintz Prajakta Naik Commissioning Editor Proofreader Kunal Parikh Safis Editing Acquisition Editor Indexer Tushar Gupta Rekha Nair Content Development Editor Graphics Siddhi Chavan Jason Monteiro Technical Editor Production Coordinator Mehul Singh Deepika Naik About the Authors Quan Hua is a Computer Vision and Machine Learning Engineer at BodiData, a data platform for body measurements, where he focuses on developing computer vision and machine learning applications for a handheld technology capable of acquiring a body avatar while a person is fully clothed. He earned a bachelor of science degree from the University of Science, Vietnam, specializing in Computer Vision. He has been working in the field of computer vision and machine learning for about 3 years at start-ups. Quan has been writing for Packt since 2015 for a Computer Vision book, OpenCV 3 Blueprints. I wish to thank everyone who has encouraged me on the way while writing this book. I want to express my sincere gratitude to my co-authors, editors, and reviewers for their advice and assistance. I would like to thank the members of my family and my wife, Kim Ngoc, who supported and encouraged me in spite of all the time it took me away from them. They all kept me going, and this book would not have been possible without them. I would also like to thank my teachers who gave me knowledge of Computer Vision and Machine Learning. Shams Ul Azeem is an undergraduate in electrical engineering from NUST Islamabad, Pakistan. He has a great interest in the computer science field, and he started his journey with Android development. Now, he’s pursuing his career in Machine Learning, particularly in deep learning, by doing medical-related freelancing projects with different companies. He was also a member of the RISE lab, NUST, and he has a publication credit at the IEEE International Conference, ROBIO as a co-author of Designing of motions for humanoid goalkeeper robots. Saif Ahmed is an accomplished quantitative analyst and data scientist with 15 years of industry experience. His career started in management consulting at Accenture and lead him to quantitative and senior management roles at Goldman Sachs and AIG Investments. Most recently, he co-founded and runs a start-up focused on applying Deep Learning to automating medical imaging. He obtained his bachelor's degree in computer science from Cornell University and is currently pursuing a graduate degree in data science at U.C. Berkeley. About the Reviewer Nathan Lintz is a Machine Learning researcher, focusing on text classification. When he began with Machine Learning, he primarily used Theano but quickly switched to TensorFlow when it was released. TensorFlow has greatly reduced the time it takes to build Machine Learning systems thanks to its intuitive and powerful neural network utilities. I want to thank my family and professors for all the help they have given me. Without them, I would have never been able to pursue my passion for software engineering and Machine Learning. www.PacktPub.com For support files and downloads related to your book, please visit www.PacktPub.com. Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details. At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks. https://www.packtpub.com/mapt Get the most in-demand software skills with Mapt. Mapt gives you full access to all Packt books and video courses, as well as industry-leading tools to help you plan your personal development and advance your career. Why subscribe? Fully searchable across every book published by Packt Copy and paste, print, and bookmark content On demand and accessible via a web browser</li> Customer Feedback Thanks for purchasing this Packt book. At Packt, quality is at the heart of our editorial process. To help us improve, please leave us an honest review on this book's Amazon page at https://www.amazon.com/dp/1787123421. If you'd like to join our team of regular reviewers, you can email us at [email protected]. We award our regular reviewers with free eBooks and videos in exchange for their valuable feedback. Help us be relentless in improving our products! Table of Contents Preface 1 Chapter 1: Getting Started with TensorFlow 6 Current use 7 Installing TensorFlow 7 Ubuntu installation 8 macOS installation 9 Windows installation 11 Virtual machine setup 16 Testing the installation 24 Summary 26 Chapter 2: Your First Classifier 27 The key parts 27 Obtaining training data 28 Downloading training data 28 Understanding classes 29 Automating the training data setup 29 Additional setup 31 Converting images to matrices 32 Logical stopping points 37 The machine learning briefcase 37 Training day 41 Saving the model for ongoing use 45 Why hide the test set? 45 Using the classifier 46 Deep diving into the network 46 Skills learned 47 Summary 48 Chapter 3: The TensorFlow Toolbox 49 A quick preview 50 Installing TensorBoard 54 Incorporating hooks into our code 55 Handwritten digits 55 AlexNet 60