TensorFlow Machine Learning Cookbook Explore machine learning concepts using the latest numerical computing library — TensorFlow — with the help of this comprehensive cookbook Nick McClure BIRMINGHAM - MUMBAI TensorFlow Machine Learning Cookbook 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 author, 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: February 2017 Production reference: 1090217 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78646-216-9 www.packtpub.com Credits Author Project Coordinator Nick McClure Shweta H Birwatkar Reviewer Proofreader Chetan Khatri Safis Editing Commissioning Editor Indexer Veena Pagare Mariammal Chettiyar Acquisition Editor Graphics Manish Nainani Disha Haria Content Development Editor Production Coordinator Sumeet Sawant Arvindkumar Gupta Technical Editor Cover Work Akash Patel Arvindkumar Gupta Copy Editor Safis Editing About the Author Nick McClure is currently a senior data scientist at PayScale, Inc. in Seattle, WA. Prior to this, he has worked at Zillow and Caesar's Entertainment. He got his degrees in Applied Mathematics from The University of Montana and the College of Saint Benedict and Saint John's University. He has a passion for learning and advocating for analytics, machine learning, and artificial intelligence. Nick occasionally puts his thoughts and musings on his blog, http://fromdata.org/, or through his Twitter account, @nfmcclure. I am very grateful to my parents, who have always encouraged me to pursue knowledge. I also want to thank my friends and partner, who have endured my long monologues about the subjects in this book and always have been encouraging and listening to me. Writing this book was made easier by the amazing efforts of the open source community and the great documentation of many projects out there related to TensorFlow. A special thanks goes out to the TensorFlow developers at Google. Their great product and skill speaks volumes for itself, and is accompanied by great documentation, tutorials, and examples. About the Reviewer Chetan Khatri is a Data Science Researcher with a total of 5 years of experience in research and development. He works as a Lead – Technology at Accionlabs India. Prior to that he worked with Nazara Games where he was leading Data Science practice as a Principal Big Data Engineer for Gaming and Telecom Business. He has worked with leading data companies and a Big 4 companies, where he has managed the Data Science Practice Platform and one of the Big 4 company's resources teams. He completed his master's degree in computer science and minor data science at KSKV Kachchh University and awarded a "Gold Medalist" by the Governer of Gujarat for his "University 1st Rank" achievements. He contributes to society in various ways, including giving talks to sophomore students at universities and giving talks on the various fields of data science, machine learning, AI, and IoT in academia and at various conferences. He has excellent correlative knowledge of both academic research and industry best practices. Hence, he always comes forward to remove the gap between Industry and Academia, where he has good number of achievements. He is the co-author of various courses, such as Data Science, IoT, Machine Learning/AI, and Distributed Databases in PG/UG cariculla at University of Kachchh. Hence, University of Kachchh became first government university in Gujarat to introduce Python as the first programming language in Cariculla and India's first government university to introduce Data Science, AI, and IoT courses in cariculla entire success story presented by Chetan at Pycon India 2016 conference. He is one of the founding members of PyKutch—A Python Community. Currently, he is working on Intelligent IoT Devices with Deep Learning , Reinforcement learning and Distributed computing with various modern architectures. I would like to thanks Prof. Devji Chhanga, head of the Computer Science Department, University of Kachchh, for guiding me to the correct path and for his valuable guidance in the field of data science research. I would also like to thanks Prof. Shweta Gorania for being the first to introduce Genetic Algorithms and Neural Networks. Last but not least I would like to thank my beloved family for their support. www.PacktPub.com eBooks, discount offers, and more 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? f Fully searchable across every book published by Packt f Copy and paste, print, and bookmark content f On demand and accessible via a web browser Customer Feedback Thank you for purchasing this Packt book. We take our commitment to improving our content and products to meet your needs seriously—that's why your feedback is so valuable. Whatever your feelings about your purchase, please consider leaving a review on this book's Amazon page. Not only will this help us, more importantly it will also help others in the community to make an informed decision about the resources that they invest in to learn. You can also review for us on a regular basis by joining our reviewers' club. If you're interested in joining, or would like to learn more about the benefits we offer, please contact us: [email protected]. Table of Contents Preface v Chapter 1: Getting Started with TensorFlow 1 Introduction 1 How TensorFlow Works 2 Declaring Tensors 5 Using Placeholders and Variables 7 Working with Matrices 10 Declaring Operations 12 Implementing Activation Functions 16 Working with Data Sources 19 Additional Resources 25 Chapter 2: The TensorFlow Way 27 Introduction 27 Operations in a Computational Graph 28 Layering Nested Operations 29 Working with Multiple Layers 32 Implementing Loss Functions 35 Implementing Back Propagation 41 Working with Batch and Stochastic Training 47 Combining Everything Together 51 Evaluating Models 55 Chapter 3: Linear Regression 61 Introduction 61 Using the Matrix Inverse Method 62 Implementing a Decomposition Method 64 Learning The TensorFlow Way of Linear Regression 67 Understanding Loss Functions in Linear Regression 70 Implementing Deming regression 74 i