让未来触手可及 Tensorflow Machine Learning Cookbook
Total Page:16
File Type:pdf, Size:1020Kb
www.aibbt.com 让未来触手可及 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 www.aibbt.com 让未来触手可及 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 www.aibbt.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 www.aibbt.com 让未来触手可及 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. www.aibbt.com 让未来触手可及 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.aibbt.com 让未来触手可及 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 atwww.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 www.aibbt.com 让未来触手可及 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]. www.aibbt.com 让未来触手可及 www.aibbt.com 让未来触手可及 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 www.aibbt.com 让未来触手可及 Table of Contents Implementing Lasso and Ridge Regression 78 Implementing Elastic Net Regression 80 Implementing Logistic Regression 83 Chapter 4: Support Vector Machines 89 Introduction 90 Working with a Linear SVM 91 Reduction to Linear Regression 98 Working with Kernels in TensorFlow 102 Implementing a Non-Linear SVM 109 Implementing a Multi-Class SVM 113 Chapter 5: Nearest Neighbor Methods 119 Introduction 119 Working with Nearest Neighbors 121 Working with Text-Based Distances 125 Computing with Mixed Distance Functions 129 Using an Address Matching Example 133 Using Nearest Neighbors for Image Recognition 137 Chapter 6: Neural Networks 143 Introduction 143 Implementing Operational Gates 145 Working with Gates and Activation Functions 149 Implementing a One-Layer Neural Network 153 Implementing Different Layers 157 Using a Multilayer Neural Network 164 Improving the Predictions of Linear Models 170 Learning to Play Tic Tac Toe 176 Chapter 7: Natural Language Processing 185 Introduction 185 Working with bag of words 187 Implementing TF-IDF 193 Working with Skip-gram Embeddings 199 Working with CBOW Embeddings 208 Making Predictions with Word2vec 214 Using Doc2vec for Sentiment Analysis 221 Chapter 8: Convolutional Neural Networks 231 Introduction 232 Implementing a Simpler CNN 233 Implementing an Advanced CNN 240 Retraining Existing CNNs models 250 ii www.aibbt.com 让未来触手可及 Table of Contents Applying Stylenet/Neural-Style 254 Implementing DeepDream 261 Chapter 9: Recurrent Neural Networks 269 Introduction 269 Implementing RNN for Spam Prediction 271 Implementing an LSTM Model 277 Stacking multiple LSTM Layers 287 Creating Sequence-to-Sequence Models 290 Training a Siamese Similarity Measure 298 Chapter 10: Taking TensorFlow to Production 309 Introduction 309 Implementing unit tests 310 Using Multiple Executors 315 Parallelizing TensorFlow 318 Taking TensorFlow to Production 319 Productionalizing TensorFlow – An Example 322 Chapter 11: More with TensorFlow 327 Introduction 327 Visualizing graphs in Tensorboard 327 There's more… 331 Working with a Genetic Algorithm 334 Clustering Using K-Means 339 Solving a System of ODEs 344 Index 347 iii www.aibbt.com 让未来触手可及 www.aibbt.com 让未来触手可及 Preface TensorFlow was open sourced in November of 2015 by Google, and since then it has become the most starred machine learning repository on GitHub.