M A N N I N G Nishant Shukla with Kenneth Fricklas
Machine Learning with TensorFlowLicensed to Eduardo Guamán <guamane1992@gmail.com>
Licensed to Eduardo Guamán <guamane1992@gmail.com>
Machine Learning with TensorFlow NISHANT SHUKLA WITH KENNETH FRICKLAS M A N N I N G SHELTER ISLANDLicensed to Eduardo Guamán <guamane1992@gmail.com>
For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact Special Sales Department Manning Publications Co. 20 Baldwin Road PO Box 761 Shelter Island, NY 11964 Email: orders@manning.com ©2018 by Manning Publications Co. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps. Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15 percent recycled and processed without the use of elemental chlorine. Manning Publications Co. Development editor: Toni Arritola 20 Baldwin Road Technical development editor: Jerry Gaines PO Box 761 Review editor: Aleksandar Dragosavljević Shelter Island, NY 11964 Project editor: Tiffany Taylor Copy editor: Sharon Wilkey Proofreader: Katie Tennant Technical proofreader: David Fombella Pombal Typesetter: Dennis Dalinnik Cover designer: Marija Tudor ISBN: 9781617293870 Printed in the United States of America 1 2 3 4 5 6 7 8 9 10 – EBM – 23 22 21 20 19 18Licensed to Eduardo Guamán <guamane1992@gmail.com>
brief contents PART 1 YOUR MACHINE-LEARNING RIG.......................................1 1 ■ A machine-learning odyssey 3 2 ■ TensorFlow essentials 25 PART 2 CORE LEARNING ALGORITHMS .....................................51 3 ■ Linear regression and beyond 53 4 ■ A gentle introduction to classification 71 5 ■ Automatically clustering data 99 6 ■ Hidden Markov models 119 PART 3 THE NEURAL NETWORK PARADIGM .............................133 7 ■ A peek into autoencoders 135 8 ■ Reinforcement learning 153 9 ■ Convolutional neural networks 169 10 ■ Recurrent neural networks 189 11 ■ Sequence-to-sequence models for chatbots 201 12 ■ Utility landscape 223v Licensed to Eduardo Guamán <guamane1992@gmail.com>
Licensed to Eduardo Guamán <guamane1992@gmail.com>
contents preface xiii acknowledgments xv about this book xvii about the author xix about the cover xx PART 1 YOUR MACHINE-LEARNING RIG.............................1 1 A machine-learning odyssey 3 1.1 Machine-learning fundamentals 5 Parameters 7 ■ Learning and inference 8 1.2 Data representation and features 9 1.3 Distance metrics 15 1.4 Types of learning 17 Supervised learning 17 ■ Unsupervised learning 19 Reinforcement learning 19 1.5 TensorFlow 21 1.6 Overview of future chapters 22 1.7 Summary 24vii Licensed to Eduardo Guamán <guamane1992@gmail.com>
CONTENTSviii2 TensorFlow essentials 25 2.1 Ensuring that TensorFlow works 27 2.2 Representing tensors 28 2.3 Creating operators 32 2.4 Executing operators with sessions 34 Understanding code as a graph 35 ■ Setting session configurations 36 2.5 Writing code in Jupyter 38 2.6 Using variables 41 2.7 Saving and loading variables 43 2.8 Visualizing data using TensorBoard 44 Implementing a moving average 44 ■ Visualizing the moving average 46 2.9 Summary 49 PART 2 CORE LEARNING ALGORITHMS ...........................51 3 Linear regression and beyond 53 3.1 Formal notation 54 How do you know the regression algorithm is working? 57 3.2 Linear regression 59 3.3 Polynomial model 62 3.4 Regularization 65 3.5 Application of linear regression 69 3.6 Summary 70 4 A gentle introduction to classification 71 4.1 Formal notation 73 4.2 Measuring performance 75 Accuracy 75 ■ Precision and recall 76 ■ Receiver operating characteristic curve 77 4.3 Using linear regression for classification 78 4.4 Using logistic regression 83 Solving one-dimensional logistic regression 84 ■ Solving two-dimensional logistic regression 87Licensed to Eduardo Guamán <guamane1992@gmail.com>
CONTENTS ix4.5 Multiclass classifier 90 One-versus-all 91 ■ One-versus-one 92 Softmax regression 92 4.6 Application of classification 96 4.7 Summary 97 5 Automatically clustering data 99 5.1 Traversing files in TensorFlow 100 5.2 Extracting features from audio 102 5.3 K-means clustering 106 5.4 Audio segmentation 109 5.5 Clustering using a self-organizing map 112 5.6 Application of clustering 117 5.7 Summary 117 6 Hidden Markov models 119 6.1 Example of a not-so-interpretable model 121 6.2 Markov model 121 6.3 Hidden Markov model 124 6.4 Forward algorithm 125 6.5 Viterbi decoding 128 6.6 Uses of hidden Markov models 130 Modeling a video 130 ■ Modeling DNA 130 Modeling an image 130 6.7 Application of hidden Markov models 130 6.8 Summary 131 PART 3 THE NEURAL NETWORK PARADIGM ...................133 7 A peek into autoencoders 135 7.1 Neural networks 136 7.2 Autoencoders 140 7.3 Batch training 145 7.4 Working with images 146 7.5 Application of autoencoders 150 7.6 Summary 151Licensed to Eduardo Guamán <guamane1992@gmail.com>
CONTENTSx8 Reinforcement learning 153 8.1 Formal notions 155 Policy 156 ■ Utility 157 8.2 Applying reinforcement learning 158 8.3 Implementing reinforcement learning 160 8.4 Exploring other applications of reinforcement learning 167 8.5 Summary 168 9 Convolutional neural networks 169 9.1 Drawback of neural networks 170 9.2 Convolutional neural networks 171 9.3 Preparing the image 173 Generating filters 176 ■ Convolving using filters 178 Max pooling 181 9.4 Implementing a convolutional neural network in TensorFlow 182 Measuring performance 185 ■ Training the classifier 186 9.5 Tips and tricks to improve performance 187 9.6 Application of convolutional neural networks 188 9.7 Summary 188 10 Recurrent neural networks 189 10.1 Contextual information 190 10.2 Introduction to recurrent neural networks 190 10.3 Implementing a recurrent neural network 192 10.4 A predictive model for time-series data 195 10.5 Application of recurrent neural networks 198 10.6 Summary 199 11 Sequence-to-sequence models for chatbots 201 11.1 Building on classification and RNNs 202 11.2 Seq2seq architecture 205 11.3 Vector representation of symbols 210 11.4 Putting it all together 212Licensed to Eduardo Guamán <guamane1992@gmail.com>
CONTENTS xi11.5 Gathering dialogue data 220 11.6 Summary 222 12 Utility landscape 223 12.1 Preference model 226 12.2 Image embedding 231 12.3 Ranking images 234 12.4 Summary 239 12.5 What’s next? 239 appendix Installation 241 index 247Licensed to Eduardo Guamán <guamane1992@gmail.com>
Licensed to Eduardo Guamán <guamane1992@gmail.com>
preface Like many people of my generation, I’ve always been addicted to the latest online trends. Around 2005, I remember endlessly refreshing FARK, YTMND, and Delicious for entertainment and news. Now, I shuffle between Reddit and Hacker News, which led me to witness TensorFlow’s ceremonious debut on November 9, 2015. The post appeared at the top of the front page on Hacker News and received hundreds of com- ments—that energy overshadowed anything else on the website. At that time, machine-learning tools were already fragmented into a zoo of librar- ies; the ecosystem relied on experimental software packages from academic labs and proprietary solutions from industry giants. When Google revealed TensorFlow, the community’s responses were mixed. Despite Google’s history of retiring beloved ser- vices (such as Google Reader, iGoogle, Knol, and Google Wave), the company also had a history of nurturing open source projects (such as Android, Chromium, Go, and Protobuf). Bets had to be made right then and there about whether to adopt TensorFlow. Although many chose to wait until the library developed, a few dived right in. I sprinted through the official documentation, mastered the basics, and was ready to apply the technology to my doctoral research at UCLA. I accumulated notes diligently, having no idea that the pages I wrote for myself to navigate the TensorFlow documen- tation would develop into a book. Around that time, an acquisitions editor at Manning Publications contacted me for a second opinion on a new Haskell book—part of their due diligence procedure, because I’m the author of Haskell Data Analysis Cookbook (Packt Publishing, 2014). Thexiii Licensed to Eduardo Guamán <guamane1992@gmail.com>
PREFACExivjourney of writing the book you’re reading right now began with my reply: “On another note, have you heard about Google’s new machine-learning library called TensorFlow?” Machine Learning with TensorFlow started with a traditional table of contents, featur- ing subjects you might expect in any machine-learning book, but it evolved to cover topics that lacked online tutorials. For example, it’s difficult to find online TensorFlow implementations of hidden Markov models (HMMs) and reinforcement learning (RL). Each iteration of editing the book introduced more concepts like these that didn’t have sufficient existing sources. Online TensorFlow tutorials are often too brief or too advanced for a beginner who wants to explore the art of machine learning. The purpose of this book is to fill those gaps, and I believe it does exactly that. If you’re new to machine learning or TensorFlow, you’ll appreciate the book’s down-to-earth teaching style. Licensed to Eduardo Guamán <guamane1992@gmail.com>
acknowledgments The deep gratification of writing this book traces back to the support of my family: Suman (Mom), Umesh (Dad), and Natasha (Sis). Their happiness and pride are always contagious. Moral support throughout the months of writing came from my close college friends, the Awesomest Turntable DJs: Alex Katz, Anish Simhal, Jasdev Singh, John Gillen, Jonathon Blonchek, Kelvin Green, Shiv Sinha, and Vinay Dandekar. Thank you, Barbara Blumenthal, my best friend and more, for tying the galaxies, nebulas, and whales with your pink ribbons. You’ve been my escape, the cure to my writer’s block. I would like to acknowledge the tremendous feedback I’ve received from online communities: my posts on Reddit (r/artificial, r/machinelearning, r/Python, r/Tensor- Flow, and r/Programming) and Hacker News received fruitful attention. I thank those who posted on the official book forum and contributed to the GitHub repository. In addition, my thanks go to the amazing group of technical peer reviewers led by Aleksandar Dragosavljević: Nii Attoh-Okine, Thomas Ballinger, John Berryman, Gil Biraud, Mikaël Dautrey, Hamish Dickson, Miguel Eduardo, Peter Hampton, Michael Jensen, David Krief, Nat Luengnaruemitchai, Thomas Peklak, Mike Staufenberg, Ursin Stauss, Richard Tobias, William Wheeler, Brad Wiederholt, and Arthur Zubarev. Their contributions included catching technical mistakes, errors in terminology, and typos, and making topic suggestions. Each pass through the review process and each piece of feedback implemented through the forum topics shaped and molded the manuscript. xv Licensed to Eduardo Guamán <guamane1992@gmail.com>
ACKNOWLEDGMENTSxvi Special thanks go to Ken Fricklas, who served as the book’s senior technical editor; Jerry Gaines, the book’s technical development editor; and David Fombella Pombal, the book’s technical proofreader. They are the best technical editors I could have hoped for. Finally, I want to thank the people at Manning Publications who made this book possible: publisher Marjan Bace and everyone on the editorial and production teams, including Janet Vail, Tiffany Taylor, Sharon Wilkey, Katie Tennant, Dennis Dalinnik, and many others who worked behind the scenes. Of all the interactions with the many individuals at Manning, I extend my greatest gratitude to Toni Arritola, the book’s development editor. Her persistent guidance and education throughout the process opened the book to a much wider audience. Licensed to Eduardo Guamán <guamane1992@gmail.com>
about this book Whether you’re new to machine learning or just new to TensorFlow, this book will be your ultimate guide. You’ll need working knowledge of object-oriented programming in Python to understand some of the code listings, but other than that, this book cov- ers introductory machine learning from the basics. Roadmap The book is divided into three parts: ■ Part 1 starts by exploring what machine learning is and highlighting Tensor- Flow’s crucial role. Chapter 1 introduces the terminology and theory of machine learning, and chapter 2 tells you everything you need to know to begin using TensorFlow. ■ Part 2 covers fundamental algorithms that have withstood the test of time. Chapters 3–6 discuss regression, classification, clustering, and hidden Markov models, respectively. You’ll find these algorithms everywhere in the field of machine learning. ■ Part 3 unveils the true power of TensorFlow: neural networks. Chapters 7–12 introduce you to autoencoders, reinforcement learning, convolutional neural networks, recurrent neural networks, sequence-to-sequence models, and utility, respectively.xvii Licensed to Eduardo Guamán <guamane1992@gmail.com>
ABOUT THIS BOOKxviiiUnless you’re an experienced TensorFlow user with a fair amount of machine-learning experience under your belt, I highly recommend reading chapters 1 and 2 first. Other than that, feel free to skip around in the book as you wish. Source code The ideas in this book are timeless; and, thanks to the community, the code listings are, too. They’re available at the book’s website, www.manning.com/books/machine- learning-with-tensorflow; and the software will be kept up to date on the book’s official GitHub repository, https://github.com/BinRoot/TensorFlow-Book. You’re encour- aged to contribute to the repo by sending pull requests or submitting new issues through GitHub. Note to print book readers Some graphics in this book are best viewed in color. The eBook versions display the color graphics, so they should be referred to as you read. To get your free eBook in PDF, ePub, and Kindle formats, go to https://manning.com/books/machine-learn- ing-with-tensorflow to register your print book. Book forum Purchase of Machine Learning with TensorFlow includes free access to a private web forum run by Manning Publications where you can make comments about the book, ask technical questions, and receive help from the author and from other users. To access the forum, go to https://forums.manning.com/forums/machine-learning-with- tensorflow. You can also learn more about Manning’s forums and the rules of conduct at https://forums.manning.com/forums/about. Manning’s commitment to our readers is to provide a venue where a meaningful dialogue between individual readers and between readers and the author can take place. It is not a commitment to any specific amount of participation on the part of the author, whose contribution to the forum remains voluntary (and unpaid). We sug- gest you try asking him some challenging questions lest his interest stray! The forum and the archives of previous discussions will be accessible from the publisher’s website as long as the book is in print.Licensed to Eduardo Guamán <guamane1992@gmail.com>
about the author Nishant Shukla (http://shukla.io) is a doctorate researcher at UCLA, focusing on machine learning and computer vision techniques with robotics. He holds a BS in Computer Sci- ence and a BA in Mathematics from the University of Vir- ginia. There, he was a founding member of Hack.UVA (http:// hackuva.io), and he lectured for a widely attended course on Haskell (http://shuklan.com/haskell). Nishant has worked as a developer for Microsoft, Facebook, and Foursquare, and as a machine-learning engineer for SpaceX, and he is the author of Haskell Data Analysis Cookbook (http://haskelldata .com). In addition, he has published research papers on top- ics ranging from analytical chemistry to natural-language processing (http://mng.bz/e9sk). In his free time, he occasionally loses at Settlers of Catan and Gwent.xix Licensed to Eduardo Guamán <guamane1992@gmail.com>
Comments 0
Loading comments...
Reply to Comment
Edit Comment