Deep Learning with PythonFrancois Chollet
Deep Learning with Python introduces the field of deep learning using the Python language and the powerful Keras library. Written by Keras creator and Google AI researcher François Chollet, this book builds your understanding through intuitive explanations and practical examples.
Purchase of the print book includes a free eBook in PDF, Kindle, and ePub formats from Manning Publications.
About the Technology
Machine learning has made remarkable progress in recent years. We went from near-unusable speech and image recognition, to near-human accuracy. We went from machines that couldn't beat a serious Go player, to defeating a world champion. Behind this progress is deep learning—a combination of engineering advances, best practices, and theory that enables a wealth of previously impossible smart applications.
About the Book
Deep Learning with Python introduces the field of deep learning using the Python language and the powerful Keras library. Written by Keras creator and Google AI researcher François Chollet, this book builds your understanding through intuitive explanations and practical examples. You'll explore challenging concepts and practice with applications in computer vision, natural-language processing, and generative models. By the time you finish, you'll have the knowledge and hands-on skills to apply deep learning in your own projects.
- Deep learning from first principles
- Setting up your own deep-learning environment
- Image-classification models
- Deep learning for text and sequences
- Neural style transfer, text generation, and image generation
About the Reader
Readers need intermediate Python skills. No previous experience with Keras, TensorFlow, or machine learning is required.
About the Author
François Chollet works on deep learning at Google in Mountain View, CA. He is the creator of the Keras deep-learning library, as well as a contributor to the TensorFlow machine-learning framework. He also does deep-learning research, with a focus on computer vision and the application of machine learning to formal reasoning. His papers have been published at major conferences in the field, including the Conference on Computer Vision and Pattern Recognition (CVPR), the Conference and Workshop on Neural Information Processing Systems (NIPS), the International Conference on Learning Representations (ICLR), and others.
Table of Contents
- PART 1 - FUNDAMENTALS OF DEEP LEARNING
- What is deep learning?
- Before we begin: the mathematical building blocks of neural networks
- Getting started with neural networks
- Fundamentals of machine learning PART 2 - DEEP LEARNING IN PRACTICE
- Deep learning for computer vision
- Deep learning for text and sequences
- Advanced deep-learning best practices
- Generative deep learning
- appendix A - Installing Keras and its dependencies on Ubuntu
- appendix B - Running Jupyter notebooks on an EC2 GPU instance
You may be interested in
Most frequently terms
It shows you most of the deep learning concepts step by step and in most easiest way.
All in one book for deep learning.
Many hard theoretical concepts can be easily understood by just viewing their implementation
François Chollet MANNING Deep Learning with Python Licensed to <null> Licensed to <null> Deep Learning with Python FRANÇOIS CHOLLET MANNING SHELTER ISLAND Licensed to <null> 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: firstname.lastname@example.org ©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. 20 Baldwin Road PO Box 761 Shelter Island, NY 11964 Development editor: Technical development editor: Review editor: Project editor: Copyeditor: Proofreader: Technical proofreaders: Typesetter: Cover designer: ISBN 9781617294433 Printed in the United States of America 1 2 3 4 5 6 7 8 9 10 – EBM – 22 21 20 19 18 17 Licensed to <null> Toni Arritola Jerry Gaines Aleksandar Dragosavljević Tiffany Taylor Tiffany Taylor Katie Tennant Alex Ott and Richard Tobias Dottie Marsico Marija Tudor brief contents PART 1 FUNDAMENTALS OF DEEP LEARNING .................................. 1 1 2 ■ 3 4 ■ ■ ■ What is deep learning? 3 Before we begin: the mathematical building blocks of neural networks 25 Getting started with neural networks 56 Fundamentals of machine learning 93 PART 2 DEEP LEARNING IN PRACTICE ........................................ 117 5 6 7 8 9 ■ ■ ■ ■ ■ Deep learning for computer vision 119 Deep learning for text and sequences 178 Advanced deep-learning best practices 233 Generative deep learning 269 Conclusions 314 v Licensed to <null> Licensed to <null> contents preface xiii acknowledgments xv about this book xvi about the author xx about the cover xxi PART 1 1 FUNDAMENTALS OF DEEP LEARNING ...................1 What is deep learning? 1.1 3 Artificial intelligence, machine learning, and deep learning 4 Artificial intelligence 4 Machine learning 4 Learning representations from data 6 The “deep” in deep learning 8 Understanding how deep learning works, in three figures 9 What deep learning has achieved so far 11 Don’t believe the short-term hype 12 The promise of AI 13 ■ ■ ■ ■ ■ 1.2 Before deep learning: a brief history of machine learning 14 Probabilistic modeling 14 Early neural networks 14 Kernel methods 15 Decision trees, random forests, and gradient boosting machines 16 Back to neural networks 17 What makes deep learning different 17 The modern machine-learning landscape 18 ■ ■ ■ ■ vii Licensed to <null> viii CONTENTS 1.3 Why deep learning? Why now? 20 Hardware 20 Data 21 Algorithms 21 A new wave of investment 22 The democratization of deep learning 23 Will it last? 23 ■ ■ ■ ■ ■ 2 Before we begin: the mathematical building blocks of neural networks 25 2.1 2.2 A first look at a neural network 27 Data representations for neural networks 31 Scalars (0D tensors) 31 Vectors (1D tensors) 31 Matrices (2D tensors) 31 3D tensors and higherdimensional tensors 32 Key attributes 32 Manipulating tensors in Numpy 34 The notion of data batches 34 Real-world examples of data tensors 35 Vector data 35 Timeseries data or sequence data 35 Image data 36 Video data 37 ■ ■ ■ ■ ■ ■ ■ ■ 2.3 ■ The gears of neural networks: tensor operations 38 Element-wise operations 38 Broadcasting 39 Tensor dot 40 Tensor reshaping 42 Geometric interpretation of tensor operations 43 A geometric interpretation of deep learning 44 ■ ■ ■ ■ ■ 2.4 The engine of neural networks: gradient-based optimization 46 What’s a derivative? 47 Derivative of a tensor operation: the gradient 48 Stochastic gradient descent 48 Chaining derivatives: the Backpropagation algorithm 51 ■ ■ 2.5 2.6 3 Looking back at our first example Chapter summary 55 Getting started with neural networks 3.1 53 56 Anatomy of a neural network 58 Layers: the building blocks of deep learning 58 Models: networks of layers 59 Loss functions and optimizers: keys to configuring the learning process 60 ■ ■ 3.2 Introduction to Keras 61 Keras, TensorFlow, Theano, and CNTK with Keras: a quick overview 62 3.3 62 Setting up a deep-learning workstation ■ Developing 65 Jupyter notebooks: the preferred way to run deep-learning experiments 65 Getting Keras running: two options 66 ■ Licensed to <null> ix CONTENTS Running deep-learning jobs in the cloud: pros and cons What is the best GPU for deep learning? 66 3.4 66 Classifying movie reviews: a binary classification example 68 The IMDB dataset 68 Preparing the data 69 Building your network 70 Validating your approach 73 Using a trained network to generate predictions on new data 76 Further experiments 77 Wrapping up 77 ■ ■ ■ 3.5 ■ Classifying newswires: a multiclass classification example 78 The Reuters dataset 78 Preparing the data 79 Building your network 79 Validating your approach 80 Generating predictions on new data 83 A different way to handle the labels and the loss 83 The importance of having sufficiently large intermediate layers 83 Further experiments 84 Wrapping up 84 ■ ■ ■ ■ ■ ■ 3.6 Predicting house prices: a regression example 85 The Boston Housing Price dataset 85 Preparing the data 86 Building your network 86 Validating your approach using K-fold validation 87 Wrapping up ■ ■ ■ ■ 3.7 4 Chapter summary 92 Fundamentals of machine learning 4.1 93 Four branches of machine learning 94 Supervised learning 94 Unsupervised learning 94 Self-supervised learning 94 Reinforcement learning 95 ■ ■ 4.2 Evaluating machine-learning models Training, validation, and test sets keep in mind 100 4.3 97 Things to Data preprocessing, feature engineering, and feature learning 101 Data preprocessing for neural networks engineering 102 4.4 ■ 97 Overfitting and underfitting 101 ■ Feature 104 Reducing the network’s size 104 Adding weight regularization 107 Adding dropout 109 ■ ■ 4.5 The universal workflow of machine learning 111 Defining the problem and assembling a dataset 111 Choosing a measure of success 112 Deciding on an ■ Licensed to <null> 91 x CONTENTS evaluation protocol 112 Preparing your data 112 Developing a model that does better than a baseline 113 Scaling up: developing a model that overfits 114 Regularizing your model and tuning your hyperparameters ■ 4.6 PART 2 5 Chapter summary 114 116 DEEP LEARNING IN PRACTICE .........................117 Deep learning for computer vision 5.1 Introduction to convnets The convolution operation operation 127 5.2 119 120 122 ■ The max-pooling Training a convnet from scratch on a small dataset The relevance of deep learning for small-data problems 130 Downloading the data 131 Building your network 133 Data preprocessing 135 Using data augmentation 138 ■ ■ 5.3 Using a pretrained convnet Feature extraction up 159 5.4 143 ■ 143 Fine-tuning Visualizing what convnets learn 152 ■ Wrapping 160 Visualizing intermediate activations 160 Visualizing convnet filters 167 Visualizing heatmaps of class activation 172 ■ ■ 5.5 6 Chapter summary 177 Deep learning for text and sequences 6.1 Working with text data 178 180 One-hot encoding of words and characters 181 Using word embeddings 184 Putting it all together: from raw text to word embeddings 188 Wrapping up 195 ■ ■ ■ 6.2 Understanding recurrent neural networks 196 A recurrent layer in Keras 198 Understanding the LSTM and GRU layers 202 A concrete LSTM example in Keras 204 Wrapping up 206 ■ ■ ■ 6.3 Advanced use of recurrent neural networks 207 A temperature-forecasting problem 207 Preparing the data 210 A common-sense, non-machine-learning baseline 212 A basic machine-learning approach 213 A first recurrent baseline 215 Using recurrent dropout ■ ■ ■ ■ Licensed to <null> 130 xi CONTENTS to fight overfitting 216 Stacking recurrent layers 217 Using bidirectional RNNs 219 Going even further 222 Wrapping up 223 ■ ■ 6.4 Sequence processing with convnets 225 Understanding 1D convolution for sequence data 225 1D pooling for sequence data 226 Implementing a 1D convnet 226 Combining CNNs and RNNs to process long sequences 228 Wrapping up 231 ■ ■ ■ 6.5 7 Chapter summary 232 Advanced deep-learning best practices 7.1 233 Going beyond the Sequential model: the Keras functional API 234 Introduction to the functional API 236 Multi-input models 238 Multi-output models 240 Directed acyclic graphs of layers 242 Layer weight sharing 246 Models as layers 247 Wrapping up 248 ■ ■ ■ ■ ■ ■ 7.2 Inspecting and monitoring deep-learning models using Keras callbacks and TensorBoard 249 Using callbacks to act on a model during training 249 Introduction to TensorBoard: the TensorFlow visualization framework 252 Wrapping up 259 ■ 7.3 Getting the most out of your models 260 Advanced architecture patterns 260 Hyperparameter optimization 263 Model ensembling 264 Wrapping up 266 ■ ■ 7.4 8 Chapter summary ■ 268 Generative deep learning 8.1 269 Text generation with LSTM 271 A brief history of generative recurrent networks 271 How do you generate sequence data? 272 The importance of the sampling strategy 272 Implementing character-level LSTM text generation 274 Wrapping up 279 ■ ■ ■ ■ 8.2 DeepDream 280 Implementing DeepDream in Keras 8.3 Neural style transfer 281 ■ Wrapping up 287 The content loss 288 The style loss 288 Neural style transfer in Keras 289 Wrapping up 295 ■ ■ ■ Licensed to <null> 286 xii CONTENTS 8.4 Generating images with variational autoencoders 296 Sampling from latent spaces of images 296 Concept vectors for image editing 297 Variational autoencoders 298 Wrapping up 304 ■ ■ 8.5 Introduction to generative adversarial networks 305 A schematic GAN implementation 307 A bag of tricks 307 The generator 308 The discriminator 309 The adversarial network 310 How to train your DCGAN 310 Wrapping up 312 ■ ■ ■ ■ 8.6 9 Chapter summary Conclusions 9.1 ■ 313 314 Key concepts in review 315 Various approaches to AI 315 What makes deep learning special within the field of machine learning 315 How to think about deep learning 316 Key enabling technologies 317 The universal machine-learning workflow 318 Key network architectures 319 The space of possibilities 322 ■ ■ ■ ■ ■ 9.2 The limitations of deep learning 325 The risk of anthropomorphizing machine-learning models Local generalization vs. extreme generalization 327 Wrapping up 329 9.3 The future of deep learning 325 330 Models as programs 330 Beyond backpropagation and differentiable layers 332 Automated machine learning 332 Lifelong learning and modular subroutine reuse 333 The long-term vision 335 ■ ■ 9.4 Staying up to date in a fast-moving field 337 Practice on real-world problems using Kaggle 337 Read about the latest developments on arXiv 337 Explore the Keras ecosystem 338 9.5 appendix A appendix B Final words 339 Installing Keras and its dependencies on Ubuntu 340 Running Jupyter notebooks on an EC2 GPU instance 345 index 353 Licensed to <null> preface If you’ve picked up this book, you’re probably aware of the extraordinary progress that deep learning has represented for the field of artificial intelligence in the recent past. In a mere five years, we’ve gone from near-unusable image recognition and speech transcription, to superhuman performance on these tasks. The consequences of this sudden progress extend to almost every industry. But in order to begin deploying deep-learning technology to every problem that it could solve, we need to make it accessible to as many people as possible, including nonexperts—people who aren’t researchers or graduate students. For deep learning to reach its full potential, we need to radically democratize it. When I released the first version of the Keras deep-learning framework in March 2015, the democratization of AI wasn’t what I had in mind. I had been doing research in machine learning for several years, and had built Keras to help me with my own experiments. But throughout 2015 and 2016, tens of thousands of new people entered the field of deep learning; many of them picked up Keras because it was—and still is—the easiest framework to get started with. As I watched scores of newcomers use Keras in unexpected, powerful ways, I came to care deeply about the accessibility and democratization of AI. I realized that the further we spread these technologies, the more useful and valuable they become. Accessibility quickly became an explicit goal in the development of Keras, and over a few short years, the Keras developer community has made fantastic achievements on this front. We’ve put deep learning into the hands of tens of thousands of people, who in turn are using it to solve important problems we didn’t even know existed until recently. The book you’re holding is another step on the way to making deep learning available to as many people as possible. Keras had always needed a companion course to xiii Licensed to <null> xiv PREFACE simultaneously cover fundamentals of deep learning, Keras usage patterns, and deeplearning best practices. This book is my best effort to produce such a course. I wrote it with a focus on making the concepts behind deep learning, and their implementation, as approachable as possible. Doing so didn’t require me to dumb down anything—I strongly believe that there are no difficult ideas in deep learning. I hope you’ll find this book valuable and that it will enable you to begin building intelligent applications and solve the problems that matter to you. Licensed to <null> acknowledgments I’d like to thank the Keras community for making this book possible. Keras has grown to have hundreds of open source contributors and more than 200,000 users. Your contributions and feedback have turned Keras into what it is today. I’d also like to thank Google for backing the Keras project. It has been fantastic to see Keras adopted as TensorFlow’s high-level API. A smooth integration between Keras and TensorFlow greatly benefits both TensorFlow users and Keras users and makes deep learning accessible to most. I want to thank the people at Manning who made this book possible: publisher Marjan Bace and everyone on the editorial and production teams, including Christina Taylor, Janet Vail, Tiffany Taylor, Katie Tennant, Dottie Marsico, and many others who worked behind the scenes. Many thanks go to the technical peer reviewers led by Aleksandar Dragosavljević — Diego Acuña Rozas, Geoff Barto, David Blumenthal-Barby, Abel Brown, Clark Dorman, Clark Gaylord, Thomas Heiman, Wilson Mar, Sumit Pal, Vladimir Pasman, Gustavo Patino, Peter Rabinovitch, Alvin Raj, Claudio Rodriguez, Srdjan Santic, Richard Tobias, Martin Verzilli, William E. Wheeler, and Daniel Williams—and the forum contributors. 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. On the technical side, special thanks go to Jerry Gaines, who served as the book’s technical editor; and Alex Ott and Richard Tobias, who served as the book’s technical proofreaders. They’re the best technical editors I could have hoped for. Finally, I’d like to express my gratitude to my wife Maria for being extremely supportive throughout the development of Keras and the writing of this book. xv Licensed to <null> about this book This book was written for anyone who wishes to explore deep learning from scratch or broaden their understanding of deep learning. Whether you’re a practicing machine-learning engineer, a software developer, or a college student, you’ll find value in these pages. This book offers a practical, hands-on exploration of deep learning. It avoids mathematical notation, preferring instead to explain quantitative concepts via code snippets and to build practical intuition about the core ideas of machine learning and deep learning. You’ll learn from more than 30 code examples that include detailed commentary, practical recommendations, and simple high-level explanations of everything you need to know to start using deep learning to solve concrete problems. The code examples use the Python deep-learning framework Keras, with TensorFlow as a backend engine. Keras, one of the most popular and fastest-growing deeplearning frameworks, is widely recommended as the best tool to get started with deep learning. After reading this book, you’ll have a solid understand of what deep learning is, when it’s applicable, and what its limitations are. You’ll be familiar with the standard workflow for approaching and solving machine-learning problems, and you’ll know how to address commonly encountered issues. You’ll be able to use Keras to tackle real-world problems ranging from computer vision to natural-language processing: image classification, timeseries forecasting, sentiment analysis, image and text generation, and more. xvi Licensed to <null> ABOUT THIS BOOK xvii Who should read this book This book is written for people with Python programming experience who want to get started with machine learning and deep learning. But this book can also be valuable to many different types of readers: If you’re a data scientist familiar with machine learning, this book will provide you with a solid, practical introduction to deep learning, the fastest-growing and most significant subfield of machine learning. If you’re a deep-learning expert looking to get started with the Keras framework, you’ll find this book to be the best Keras crash course available. If you’re a graduate student studying deep learning in a formal setting, you’ll find this book to be a practical complement to your education, helping you build intuition around the behavior of deep neural networks and familiarizing you with key best practices. Even technically minded people who don’t code regularly will find this book useful as an introduction to both basic and advanced deep-learning concepts. In order to use Keras, you’ll need reasonable Python proficiency. Additionally, familiarity with the Numpy library will be helpful, although it isn’t required. You don’t need previous experience with machine learning or deep learning: this book covers from scratch all the necessary basics. You don’t need an advanced mathematics background, either—high school–level mathematics should suffice in order to follow along. Roadmap This book is structured in two parts. If you have no prior experience with machine learning, I strongly recommend that you complete part 1 before approaching part 2. We’ll start with simple examples, and as the book goes on, we’ll get increasingly close to state-of-the-art techniques. Part 1 is a high-level introduction to deep learning, providing context and definitions, and explaining all the notions required to get started with machine learning and neural networks: Chapter 1 presents essential context and background knowledge around AI, machine learning, and deep learning. Chapter 2 introduces fundamental concepts necessary in order to approach deep learning: tensors, tensor operations, gradient descent, and backpropagation. This chapter also features the book’s first example of a working neural network. Chapter 3 includes everything you need to get started with neural networks: an introduction to Keras, our deep-learning framework of choice; a guide for setting up your workstation; and three foundational code examples with detailed explanations. By the end of this chapter, you’ll be able to train simple neural Licensed to <null> xviii ABOUT THIS BOOK networks to handle classification and regression tasks, and you’ll have a solid idea of what’s happening in the background as you train them. Chapter 4 explores the canonical machine-learning workflow. You’ll also learn about common pitfalls and their solutions. Part 2 takes an in-depth dive into practical applications of deep learning in computer vision and natural-language processing. Many of the examples introduced in this part can be used as templates to solve problems you’ll encounter in the real-world practice of deep learning: Chapter 5 examines a range of practical computer-vision examples, with a focus on image classification. Chapter 6 gives you practice with techniques for processing sequence data, such as text and timeseries. Chapter 7 introduces advanced techniques for building state-of-the-art deeplearning models. Chapter 8 explains generative models: deep-learning models capable of creating images and text, with sometimes surprisingly artistic results. Chapter 9 is dedicated to consolidating what you’ve learned throughout the book, as well as opening perspectives on the limitations of deep learning and exploring its probable future. Software/hardware requirements All of this book’s code examples use the Keras deep-learning framework (https:// keras.io), which is open source and free to download. You’ll need access to a UNIX machine; it’s possible to use Windows, too, but I don’t recommend it. Appendix A walks you through the complete setup. I also recommend that you have a recent NVIDIA GPU on your machine, such as a TITAN X. This isn’t required, but it will make your experience better by allowing you to run the code examples several times faster. See section 3.3 for more information about setting up a deep-learning workstation. If you don’t have access to a local workstation with a recent NVIDIA GPU, you can use a cloud environment, instead. In particular, you can use Google Cloud instances (such as an n1-standard-8 instance with an NVIDIA Tesla K80 add-on) or Amazon Web Services (AWS) GPU instances (such as a p2.xlarge instance). Appendix B presents in detail one possible cloud workflow that runs an AWS instance via Jupyter notebooks, accessible in your browser. Source code All code examples in this book are available for download as Jupyter notebooks from the book’s website, www.manning.com/books/deep-learning-with-python, and on GitHub at https://github.com/fchollet/deep-learning-with-python-notebooks. Licensed to <null> ABOUT THIS BOOK xix Book forum Purchase of Deep Learning with Python 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/deep-learning-with-python. 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 isn’t a commitment to any specific amount of participation on the part of the author, whose contribution to the forum remains voluntary (and unpaid). We suggest 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 <null> about the author François Chollet works on deep learning at Google in Mountain View, CA. He is the creator of the Keras deep-learning library, as well as a contributor to the TensorFlow machinelearning framework. He also does deep-learning research, with a focus on computer vision and the application of machine learning to formal reasoning. His papers have been published at major conferences in the field, including the Conference on Computer Vision and Pattern Recognition (CVPR), the Conference and Workshop on Neural Information Processing Systems (NIPS), the International Conference on Learning Representations (ICLR), and others. xx Licensed to <null> about the cover The figure on the cover of Deep Learning with Python is captioned “Habit of a Persian Lady in 1568.” The illustration is taken from Thomas Jefferys’ A Collection of the Dresses of Different Nations, Ancient and Modern (four volumes), London, published between 1757 and 1772. The title page states that these are hand-colored copperplate engravings, heightened with gum arabic. Thomas Jefferys (1719–1771) was called “Geographer to King George III.” He was an English cartographer who was the leading map supplier of his day. He engraved and printed maps for government and other official bodies and produced a wide range of commercial maps and atlases, especially of North America. His work as a map maker sparked an interest in local dress customs of the lands he surveyed and mapped, which are brilliantly displayed in this collection. Fascination with faraway lands and travel for pleasure were relatively new phenomena in the late eighteenth century, and collections such as this one were popular, introducing both the tourist as well as the armchair traveler to the inhabitants of other countries. The diversity of the drawings in Jefferys’ volumes speaks vividly of the uniqueness and individuality of the world’s nations some 200 years ago. Dress codes have changed since then, and the diversity by region and country, so rich at the time, has faded away. It’s now often hard to tell the inhabitants of one continent from another. Perhaps, trying to view it optimistically, we’ve traded a cultural and visual diversity for a more varied personal life—or a more varied and interesting intellectual and technical life. At a time when it’s difficult to tell one computer book from another, Manning celebrates the inventiveness and initiative of the computer business with book covers based on the rich diversity of regional life of two centuries ago, brought back to life by Jefferys’ pictures. xxi Licensed to <null> Licensed to <null> Part 1 Fundamentals of deep learning C hapters 1–4 of this book will give you a foundational understanding of what deep learning is, what it can achieve, and how it works. It will also make you familiar with the canonical workflow for solving data problems using deep learning. If you aren’t already highly knowledgeable about deep learning, you should definitely begin by reading part 1 in full before moving on to the practical applications in part 2. Licensed to <null> Licensed to <null> What is deep learning? This chapter covers High-level definitions of fundamental concepts Timeline of the development of machine learning Key factors behind deep learning’s rising popularity and future potential In the past few years, artificial intelligence (AI) has been a subject of intense media hype. Machine learning, deep learning, and AI come up in countless articles, often outside of technology-minded publications. We’re promised a future of intelligent chatbots, self-driving cars, and virtual assistants—a future sometimes painted in a grim light and other times as utopian, where human jobs will be scarce and most economic activity will be handled by robots or AI agents. For a future or current practitioner of machine learning, it’s important to be able to recognize the signal in the noise so that you can tell world-changing developments from overhyped press releases. Our future is at stake, and it’s a future in which you have an active role to play: after reading this book, you’ll be one of those who develop the AI agents. So let’s tackle these questions: What has deep learning achieved so far? How significant is it? Where are we headed next? Should you believe the hype? This chapter provides essential context around artificial intelligence, machine learning, and deep learning. 3 Licensed to <null> 4 1.1 CHAPTER 1 What is deep learning? Artificial intelligence, machine learning, and deep learning First, we need to define clearly what we’re talking about when we mention AI. What are artificial intelligence, machine learning, and deep learning (see figure 1.1)? How do they relate to each other? Artificial intelligence Machine learning Deep learning Figure 1.1 Artificial intelligence, machine learning, and deep learning 1.1.1 Artificial intelligence Artificial intelligence was born in the 1950s, when a handful of pioneers from the nascent field of computer science started asking whether computers could be made to “think”—a question whose ramifications we’re still exploring today. A concise definition of the field would be as follows: the effort to automate intellectual tasks normally performed by humans. As such, AI is a general field that encompasses machine learning and deep learning, but that also includes many more approaches that don’t involve any learning. Early chess programs, for instance, only involved hardcoded rules crafted by programmers, and didn’t qualify as machine learning. For a fairly long time, many experts believed that human-level artificial intelligence could be achieved by having programmers handcraft a sufficiently large set of explicit rules for manipulating knowledge. This approach is known as symbolic AI, and it was the dominant paradigm in AI from the 1950s to the late 1980s. It reached its peak popularity during the expert systems boom of the 1980s. Although symbolic AI proved suitable to solve well-defined, logical problems, such as playing chess, it turned out to be intractable to figure out explicit rules for solving more complex, fuzzy problems, such as image classification, speech recognition, and language translation. A new approach arose to take symbolic AI’s place: machine learning. 1.1.2 Machine learning In Victorian England, Lady Ada Lovelace was a friend and collaborator of Charles Babbage, the inventor of the Analytical Engine: the first-known general-purpose, mechanical computer. Although visionary and far ahead of its time, the Analytical Licensed to <null> Artificial intelligence, machine learning, and deep learning 5 Engine wasn’t meant as a general-purpose computer when it was designed in the 1830s and 1840s, because the concept of general-purpose computation was yet to be invented. It was merely meant as a way to use mechanical operations to automate certain computations from the field of mathematical analysis—hence, the name Analytical Engine. In 1843, Ada Lovelace remarked on the invention, “The Analytical Engine has no pretensions whatever to originate anything. It can do whatever we know how to order it to perform.… Its province is to assist us in making available what we’re already acquainted with.” This remark was later quoted by AI pioneer Alan Turing as “Lady Lovelace’s objection” in his landmark 1950 paper “Computing Machinery and Intelligence,”1 which introduced the Turing test as well as key concepts that would come to shape AI. Turing was quoting Ada Lovelace while pondering whether general-purpose computers could be capable of learning and originality, and he came to the conclusion that they could. Machine learning arises from this question: could a computer go beyond “what we know how to order it to perform” and learn on its own how to perform a specified task? Could a computer surprise us? Rather than programmers crafting data-processing rules by hand, could a computer automatically learn these rules by looking at data? This question opens the door to a new programming paradigm. In classical programming, the paradigm of symbolic AI, humans input rules (a program) and data to be processed according to these rules, and out come answers (see figure 1.2). With machine learning, humans input data as well as the answers expected from the data, and out come the rules. These rules can then be applied to new data to produce original answers. Rules Data Data Answers Classical programming Machine learning Answers Rules Figure 1.2 Machine learning: a new programming paradigm A machine-learning system is trained rather than explicitly programmed. It’s presented with many examples relevant to a task, and it finds statistical structure in these examples that eventually allows the system to come up with rules for automating the task. For instance, if you wished to automate the task of tagging your vacation pictures, you could present a machine-learning system with many examples of pictures already tagged by humans, and the system would learn statistical rules for associating specific pictures to specific tags. 1 A. M. Turing, “Computing Machinery and Intelligence,” Mind 59, no. 236 (1950): 433-460. Licensed to <null> 6 CHAPTER 1 What is deep learning? Although machine learning only started to flourish in the 1990s, it has quickly become the most popular and most successful subfield of AI, a trend driven by the availability of faster hardware and larger datasets. Machine learning is tightly related to mathematical statistics, but it differs from statistics in several important ways. Unlike statistics, machine learning tends to deal with large, complex datasets (such as a dataset of millions of images, each consisting of tens of thousands of pixels) for which classical statistical analysis such as Bayesian analysis would be impractical. As a result, machine learning, and especially deep learning, exhibits comparatively little mathematical theory—maybe too little—and is engineering oriented. It’s a hands-on discipline in which ideas are proven empirically more often than theoretically. 1.1.3 Learning representations from data To define deep learning and understand the difference between deep learning and other machine-learning approaches, first we need some idea of what machinelearning algorithms do. I just stated that machine learning discovers rules to execute a data-processing task, given examples of what’s expected. So, to do machine learning, we need three things: Input data points—For instance, if the task is speech recognition, these data points could be sound files of people speaking. If the task is image tagging, they could be pictures. Examples of the expected output—In a speech-recognition task, these could be human-generated transcripts of sound files. In an image task, expected outputs could be tags such as “dog,” “cat,” and so on. A way to measure whether the algorithm is doing a good job—This is necessary in order to determine the distance between the algorithm’s current output and its expected output. The measurement is used as a feedback signal to adjust the way the algorithm works. This adjustment step is what we call learning. A machine-learning model transforms its input data into meaningful outputs, a process that is “learned” from exposure to known examples of inputs and outputs. Therefore, the central problem in machine learning and deep learning is to meaningfully transform data: in other words, to learn useful representations of the input data at hand—representations that get us closer to the expected output. Before we go any further: what’s a representation? At its core, it’s a different way to look at data—to represent or encode data. For instance, a color image can be encoded in the RGB format (red-green-blue) or in the HSV format (hue-saturation-value): these are two different representations of the same data. Some tasks that may be difficult with one representation can become easy with another. For example, the task “select all red pixels in the image” is simpler in the RG format, whereas “make the image less saturated” is simpler in the HSV format. Machine-learning models are all about finding appropriate representations for their input data—transformations of the data that make it more amenable to the task at hand, such as a classification task. Licensed to <null> 7 Artificial intelligence, machine learning, and deep learning Let’s make this concrete. Consider an x-axis, a y-axis, and some points represented by their coordinates in the (x, y) system, as shown in figure 1.3. As you can see, we have a few white points and a few black points. Let’s say we want to develop an algorithm that can take the coordinates (x, y) of a point and output whether that point is likely to be black or to be white. In this case, y x The inputs are the coordinates of our points. The expected outputs are the colors of our points. A way to measure whether our algorithm is doing a Figure 1.3 Some sample data good job could be, for instance, the percentage of points that are being correctly classified. What we need here is a new representation of our data that cleanly separates the white points from the black points. One transformation we could use, among many other possibilities, would be a coordinate change, illustrated in figure 1.4. 1: Raw data 2: Coordinate change y y 3: Better representation y x x Figure 1.4 x Coordinate change In this new coordinate system, the coordinates of our points can be said to be a new representation of our data. And it’s a good one! With this representation, the black/white classification problem can be expressed as a simple rule: “Black points are such that x > 0,” or “White points are such that x < 0.” This new representation basically solves the classification problem. In this case, we defined the coordinate change by hand. But if instead we tried systematically searching for different possible coordinate changes, and used as feedback the percentage of points being correctly classified, then we would be doing machine learning. Learning, in the context of machine learning, describes an automatic search process for better representations. All machine-learning algorithms consist of automatically finding such transformations that turn data into more-useful representations for a given task. These operations can be coordinate changes, as you just saw, or linear projections (which may destroy information), translations, nonlinear operations (such as “select all points such that x > 0”), and so on. Machine-learning algorithms aren’t usually creative in Licensed to <null> 8 CHAPTER 1 What is deep learning? finding these transformations; they’re merely searching through a predefined set of operations, called a hypothesis space. So that’s what machine learning is, technically: searching for useful representations of some input data, within a predefined space of possibilities, using guidance from a feedback signal. This simple idea allows for solving a remarkably broad range of intellectual tasks, from speech recognition to autonomous car driving. Now that you understand what we mean by learning, let’s take a look at what makes deep learning special. 1.1.4 The “deep” in deep learning Deep learning is a specific subfield of machine learning: a new take on learning representations from data that puts an emphasis on learning successive layers of increasingly meaningful representations. The deep in deep learning isn’t a reference to any kind of deeper understanding achieved by the approach; rather, it stands for this idea of successive layers of representations. How many layers contribute to a model of the data is called the depth of the model. Other appropriate names for the field could have been layered representations learning and hierarchical representations learning. Modern deep learning often involves tens or even hundreds of successive layers of representations— and they’re all learned automatically from exposure to training data. Meanwhile, other approaches to machine learning tend to focus on learning only one or two layers of representations of the data; hence, they’re sometimes called shallow learning. In deep learning, these layered representations are (almost always) learned via models called neural networks, structured in literal layers stacked on top of each other. The term neural network is a reference to neurobiology, but although some of the central concepts in deep learning were developed in part by drawing inspiration from our understanding of the brain, deep-learning models are not models of the brain. There’s no evidence that the brain implements anything like the learning mechanisms used in modern deep-learning models. You may come across pop-science articles proclaiming that deep learning works like the brain or was modeled after the brain, but that isn’t the case. It would be confusing and counterproductive for newcomers to the field to think of deep learning as being in any way related to neurobiology; you don’t need that shroud of “just like our minds” mystique and mystery, and you may as well forget anything you may have read about hypothetical links between deep learning and biology. For our purposes, deep learning is a mathematical framework for learning representations from data. Licensed to <null> 9 Artificial intelligence, machine learning, and deep learning What do the representations learned by a deep-learning algorithm look like? Let’s examine how a network several layers deep (see figure 1.5) transforms an image of a digit in order to recognize what digit it is. Layer 1 Layer 2 Layer 3 Layer 4 Original input 0 1 2 3 4 5 6 7 8 9 Final output Figure 1.5 A deep neural network for digit classification As you can see in figure 1.6, the network transforms the digit image into representations that are increasingly different from the original image and increasingly informative about the final result. You can think of a deep network as a multistage information-distillation operation, where information goes through successive filters and comes out increasingly purified (that is, useful with regard to some task). Layer 1 representations Layer 2 representations Layer 3 representations Layer 4 representations (final output) 0 1 2 3 4 5 6 7 8 9 Original input Layer 1 Figure 1.6 Layer 2 Layer 3 Layer 4 Deep representations learned by a digit-classification model So that’s what deep learning is, technically: a multistage way to learn data representations. It’s a simple idea—but, as it turns out, very simple mechanisms, sufficiently scaled, can end up looking like magic. 1.1.5 Understanding how deep learning works, in three figures At this point, you know that machine learning is about mapping inputs (such as images) to targets (such as the label “cat”), which is done by observing many examples of input and targets. You also know that deep neural networks do this input-to-target Licensed to <null> 10 CHAPTER 1 What is deep learning? mapping via a deep sequence of simple data transformations (layers) and that these data transformations are learned by exposure to examples. Now let’s look at how this learning happens, concretely. The specification of what a layer does to its input data is stored in the layer’s weights, which in essence are a bunch of numbers. In technical terms, we’d say that the transformation implemented by a layer is parameterized by its weights (see figure 1.7). (Weights are also sometimes called the parameters of a layer.) In this context, learning means finding a set of values for the weights of all layers in a network, such that the network will correctly map example inputs to their associated targets. But here’s the thing: a deep neural network can contain tens of millions of parameters. Finding the correct value for all of them may seem like a daunting task, especially given that modifying the value of one parameter will affect the behavior of all the others! Input X Goal: finding the right values for these weights Weights Layer (data transformation) Weights Layer (data transformation) Predictions Y' Figure 1.7 A neural network is parameterized by its weights. To control something, first you need to be able to observe it. To control the output of a neural network, you need to be able to measure how far this output is from what you expected. This is the job of the loss function of the network, also called the objective function. The loss function takes the predictions of the network and the true target (what you wanted the network to output) and computes a distance score, capturing how well the network has done on this specific example (see figure 1.8). Input X Weights Layer (data transformation) Weights Layer (data transformation) Predictions Y' True targets Y Loss function Loss score Figure 1.8 A loss function measures the quality of the network’s output. Licensed to <null> Artificial intelligence, machine learning, and deep learning 11 The fundamental trick in deep learning is to use this score as a feedback signal to adjust the value of the weights a little, in a direction that will lower the loss score for the current example (see figure 1.9). This adjustment is the job of the optimizer, which implements what’s called the Backpropagation algorithm: the central algorithm in deep learning. The next chapter explains in more detail how backpropagation works. Input X Weights Layer (data transformation) Weights Layer (data transformation) Weight update Optimizer Predictions Y' True targets Y Loss function Loss score Figure 1.9 The loss score is used as a feedback signal to adjust the weights. Initially, the weights of the network are assigned random values, so the network merely implements a series of random transformations. Naturally, its output is far from what it should ideally be, and the loss score is accordingly very high. But with every example the network processes, the weights are adjusted a little in the correct direction, and the loss score decreases. This is the training loop, which, repeated a sufficient number of times (typically tens of iterations over thousands of examples), yields weight values that minimize the loss function. A network with a minimal loss is one for which the outputs are as close as they can be to the targets: a trained network. Once again, it’s a simple mechanism that, once scaled, ends up looking like magic. 1.1.6 What deep learning has achieved so far Although deep learning is a fairly old subfield of machine learning, it only rose to prominence in the early 2010s. In the few years since, it has achieved nothing short of a revolution in the field, with remarkable results on perceptual problems such as seeing and hearing—problems involving skills that seem natural and intuitive to humans but have long been elusive for machines. In particular, deep learning has achieved the following breakthroughs, all in historically difficult areas of machine learning: Near-human-level image classification Near-human-level speech recognition Near-human-level handwriting transcription Improved machine translation Licensed to <null> 12 CHAPTER 1 What is deep learning? Improved text-to-speech conversion Digital assistants such as Google Now and Amazon Alexa Near-human-level autonomous driving Improved ad targeting, as used by Google, Baidu, and Bing Improved search results on the web Ability to answer natural-language questions Superhuman Go playing We’re still exploring the full extent of what deep learning can do. We’ve started applying it to a wide variety of problems outside of machine perception and natural-language understanding, such as formal reasoning. If successful, this may herald an age where deep learning assists humans in science, software development, and more. 1.1.7 Don’t believe the short-term hype Although deep learning has led to remarkable achievements in recent years, expectations for what the field will be able to achieve in the next decade tend to run much higher than what will likely be possible. Although some world-changing applications like autonomous cars are already within reach, many more are likely to remain elusive for a long time, such as believable dialogue systems, human-level machine translation across arbitrary languages, and human-level natural-language understanding. In particular, talk of human-level general intelligence shouldn’t be taken too seriously. The risk with high expectations for the short term is that, as technology fails to deliver, research investment will dry up, slowing progress for a long time. This has happened before. Twice in the past, AI went through a cycle of intense optimism followed by disappointment and skepticism, with a dearth of funding as a result. It started with symbolic AI in the 1960s. In those early days, projections about AI were flying high. One of the best-known pioneers and proponents of the symbolic AI approach was Marvin Minsky, who claimed in 1967, “Within a generation … the problem of creating ‘artificial intelligence’ will substantially be solved.” Three years later, in 1970, he made a more precisely quantified prediction: “In from three to eight years we will have a machine with the general intelligence of an average human being.” In 2016, such an achievement still appears to be far in the future—so far that we have no way to predict how long it will take—but in the 1960s and early 1970s, several experts believed it to be right around the corner (as do many people today). A few years later, as these high expectations failed to materialize, researchers and government funds turned away from the field, marking the start of the first AI winter (a reference to a nuclear winter, because this was shortly after the height of the Cold War). It wouldn’t be the last one. In the 1980s, a new take on symbolic AI, expert systems, started gathering steam among large companies. A few initial success stories triggered a wave of investment, with corporations around the world starting their own in-house AI departments to develop expert systems. Around 1985, companies were spending over $1 billion each year on the technology; but by the early 1990s, these systems had proven expensive to maintain, difficult to scale, and limited in scope, and interest died down. Thus began the second AI winter. Licensed to <null> Artificial intelligence, machine learning, and deep learning 13 We may be currently witnessing the third cycle of AI hype and disappointment— and we’re still in the phase of intense optimism. It’s best to moderate our expectations for the short term and make sure people less familiar with the technical side of the field have a clear idea of what deep learning can and can’t deliver. 1.1.8 The promise of AI Although we may have unrealistic short-term expectations for AI, the long-term picture is looking bright. We’re only getting started in applying deep learning to many important problems for which it could prove transformative, from medical diagnoses to digital assistants. AI research has been moving forward amazingly quickly in the past five years, in large part due to a level of funding never before seen in the short history of AI, but so far relatively little of this progress has made its way into the products and processes that form our world. Most of the research findings of deep learning aren’t yet applied, or at least not applied to the full range of problems they can solve across all industries. Your doctor doesn’t yet use AI, and neither does your accountant. You probably don’t use AI technologies in your day-to-day life. Of course, you can ask your smartphone simple questions and get reasonable answers, you can get fairly useful product recommendations on Amazon.com, and you can search for “birthday” on Google Photos and instantly find those pictures of your daughter’s birthday party from last month. That’s a far cry from where such technologies used to stand. But such tools are still only accessories to our daily lives. AI has yet to transition to being central to the way we work, think, and live. Right now, it may seem hard to believe that AI could have a large impact on our world, because it isn’t yet widely deployed—much as, back in 1995, it would have been difficult to believe in the future impact of the internet. Back then, most people didn’t see how the internet was relevant to them and how it was going to change their lives. The same is true for deep learning and AI today. But make no mistake: AI is coming. In a notso-distant future, AI will be your assistant, even your friend; it will answer your questions, help educate your kids, and watch over your health. It will deliver your groceries to your door and drive you from point A to point B. It will be your interface to an increasingly complex and information-intensive world. And, even more important, AI will help humanity as a whole move forward, by assisting human scientists in new breakthrough discoveries across all scientific fields, from genomics to mathematics. On the way, we may face a few setbacks and maybe a new AI winter—in much the same way the internet industry was overhyped in 1998–1999 and suffered from a crash that dried up investment throughout the early 2000s. But we’ll get there eventually. AI will end up being applied to nearly every process that makes up our society and our daily lives, much like the internet is today. Don’t believe the short-term hype, but do believe in the long-term vision. It may take a while for AI to be deployed to its true potential—a potential the full extent of which no one has yet dared to dream—but AI is coming, and it will transform our world in a fantastic way. Licensed to <null> 14 1.2 CHAPTER 1 What is deep learning? Before deep learning: a brief history of machine learning Deep learning has reached a level of public attention and industry investment never before seen in the history of AI, but it isn’t the first successful form of machine learning. It’s safe to say that most of the machine-learning algorithms used in the industry today aren’t deep-learning algorithms. Deep learning isn’t always the right tool for the job—sometimes there isn’t enough data for deep learning to be applicable, and sometimes the problem is better solved by a different algorithm. If deep learning is your first contact with machine learning, then you may find yourself in a situation where all you have is the deep-learning hammer, and every machine-learning problem starts to look like a nail. The only way not to fall into this trap is to be familiar with other approaches and practice them when appropriate. A detailed discussion of classical machine-learning approaches is outside of the scope of this book, but we’ll briefly go over them and describe the historical context in which they were developed. This will allow us to place deep learning in the broader context of machine learning and better understand where deep learning comes from and why it matters. 1.2.1 Probabilistic modeling Probabilistic modeling is the application of the principles of statistics to data analysis. It was one of the earliest forms of machine learning, and it’s still widely used to this day. One of the best-known algorithms in this category is the Naive Bayes algorithm. Naive Bayes is a type of machine-learning classifier based on applying Bayes’ theorem while assuming that the features in the input data are all independent (a strong, or “naive” assumption, which is where the name comes from). This form of data analysis predates computers and was applied by hand decades before its first computer implementation (most likely dating back to the 1950s). Bayes’ theorem and the foundations of statistics date back to the eighteenth century, and these are all you need to start using Naive Bayes classifiers. A closely related model is the logistic regression (logreg for short), which is sometimes considered to be the “hello world” of modern machine learning. Don’t be misled by its name—logreg is a classification algorithm rather than a regression algorithm. Much like Naive Bayes, logreg predates computing by a long time, yet it’s still useful to this day, thanks to its simple and versatile nature. It’s often the first thing a data scientist will try on a dataset to get a feel for the classification task at hand. 1.2.2 Early neural networks Early iterations of neural networks have been completely supplanted by the modern variants covered in these pages, but it’s helpful to be aware of how deep learning originated. Although the core ideas of neural networks were investigated in toy forms as early as the 1950s, the approach took decades to get started. For a long time, the missing piece was an efficient way to train large neural networks. This changed in the mid-1980s, Licensed to <null> Before deep learning: a brief history of machine learning 15 when multiple people independently rediscovered the Backpropagation algorithm— a way to train chains of parametric operations using gradient-descent optimization (later in the book, we’ll precisely define these concepts)—and started applying it to neural networks. The first successful practical application of neural nets came in 1989 from Bell Labs, when Yann LeCun combined the earlier ideas of convolutional neural networks and backpropagation, and applied them to the problem of classifying handwritten digits. The resulting network, dubbed LeNet, was used by the United States Postal Service in the 1990s to automate the reading of ZIP codes on mail envelopes. 1.2.3 Kernel methods As neural networks started to gain some respect among researchers in the 1990s, thanks to this first success, a new approach to machine learning rose to fame and quickly sent neural nets back to oblivion: kernel methods. Kernel methods are a group of classification algorithms, the best known of which is the support vector machine (SVM). The modern formulation of an SVM was developed by Vladimir Vapnik and Corinna Cortes in the early 1990s at Bell Labs and published in 1995,2 although an older linear formulation was published by Vapnik and Alexey Chervonenkis as early as 1963.3 SVMs aim at solving classification problems by finding good decision boundaries (see figure 1.10) between two sets of points belonging to two different categories. A decision boundary can be thought of as a line or surface separating your training data into two spaces corresponding to two categories. To classify new data points, you just need to check which side of the decision Figure 1.10 A decision boundary boundary they fall on. SVMs proceed to find these boundaries in two steps: 1 2 The data is mapped to a new high-dimensional representation where the decision boundary can be expressed as a hyperplane (if the data was twodimensional, as in figure 1.10, a hyperplane would be a straight line). A good decision boundary (a separation hyperplane) is computed by trying to maximize the distance between the hyperplane and the closest data points from each class, a step called maximizing the margin. This allows the boundary to generalize well to new samples outside of the training dataset. The technique of mapping data to a high-dimensional representation where a classification problem becomes simpler may look good on paper, but in practice it’s often computationally intractable. That’s where the kernel trick comes in (the key idea that kernel methods are named after). Here’s the gist of it: to find good decision 2 3 Vladimir Vapnik and Corinna Cortes, “Support-Vector Networks,” Machine Learning 20, no. 3 (1995): 273–297. Vladimir Vapnik and Alexey Chervonenkis, “A Note on One Class of Perceptrons,” Automation and Remote Control 25 (1964). Licensed to <null> 16 CHAPTER 1 What is deep learning? hyperplanes in the new representation space, you don’t have to explicitly compute the coordinates of your points in the new space; you just need to compute the distance between pairs of points in that space, which can be done efficiently using a kernel function. A kernel function is a computationally tractable operation that maps any two points in your initial space to the distance between these points in your target representation space, completely bypassing the explicit computation of the new representation. Kernel functions are typically crafted by hand rather than learned from data—in the case of an SVM, only the separation hyperplane is learned. At the time they were developed, SVMs exhibited state-of-the-art performance on simple classification problems and were one of the few machine-learning methods backed by extensive theory and amenable to serious mathematical analysis, making them well understood and easily interpretable. Because of these useful properties, SVMs became extremely popular in the field for a long time. But SVMs proved hard to scale to large datasets and didn’t provide good results for perceptual problems such as image classification. Because an SVM is a shallow method, applying an SVM to perceptual problems requires first extracting useful representations manually (a step called feature engineering), which is difficult and brittle. 1.2.4 Decision trees, random forests, and gradient boosting machines Decision trees are flowchart-like structures that let you classify input data points or predict output values given inputs (see figure 1.11). They’re easy to visualize and interpret. Decisions trees learned from data began to receive significant research interest in the 2000s, and by 2010 they were often preferred to kernel methods. Input data Question Question Category Category Question Category Category Figure 1.11 A decision tree: the parameters that are learned are the questions about the data. A question could be, for instance, “Is coefficient 2 in the data greater than 3.5?” In particular, the Random Forest algorithm introduced a robust, practical take on decision-tree learning that involves building a large number of specialized decision trees and then ensembling their outputs. Random forests are applicable to a wide range of problems—you could say that they’re almost always the second-best algorithm for any shallow machine-learning task. When the popular machine-learning competition website Kaggle (http://kaggle.com) got started in 2010, random forests quickly became a favorite on the platform—until 2014, when gradient boosting machines took over. A gradient boosting machine, much like a random forest, is a machine-learning technique based on ensembling weak prediction models, generally decision trees. It Licensed to <null> Before deep learning: a brief history of machine learning 17 uses gradient boosting, a way to improve any machine-learning model by iteratively training new models that specialize in addressing the weak points of the previous models. Applied to decision trees, the use of the gradient boosting technique results in models that strictly outperform random forests most of the time, while having similar properties. It may be one of the best, if not the best, algorithm for dealing with nonperceptual data today. Alongside deep learning, it’s one of the most commonly used techniques in Kaggle competitions. 1.2.5 Back to neural networks Around 2010, although neural networks were almost completely shunned by the scientific community at large, a number of people still working on neural networks started to make important breakthroughs: the groups of Geoffrey Hinton at the University of Toronto, Yoshua Bengio at the University of Montreal, Yann LeCun at New York University, and IDSIA in Switzerland. In 2011, Dan Ciresan from IDSIA began to win academic image-classification competitions with GPU-trained deep neural networks—the first practical success of modern deep learning. But the watershed moment came in 2012, with the entry of Hinton’s group in the yearly large-scale image-classification challenge ImageNet. The ImageNet challenge was notoriously difficult at the time, consisting of classifying highresolution color images into 1,000 different categories after training on 1.4 million images. In 2011, the top-five accuracy of the winning model, based on classical approaches to computer vision, was only 74.3%. Then, in 2012, a team led by Alex Krizhevsky and advised by Geoffrey Hinton was able to achieve a top-five accuracy of 83.6%—a significant breakthrough. The competition has been dominated by deep convolutional neural networks every year since. By 2015, the winner reached an accuracy of 96.4%, and the classification task on ImageNet was considered to be a completely solved problem. Since 2012, deep convolutional neural networks (convnets) have become the go-to algorithm for all computer vision tasks; more generally, they work on all perceptual tasks. At major computer vision conferences in 2015 and 2016, it was nearly impossible to find presentations that didn’t involve convnets in some form. At the same time, deep learning has also found applications in many other types of problems, such as natural-language processing. It has completely replaced SVMs and decision trees in a wide range of applications. For instance, for several years, the European Organization for Nuclear Research, CERN, used decision tree–based methods for analysis of particle data from the ATLAS detector at the Large Hadron Collider (LHC); but CERN eventually switched to Keras-based deep neural networks due to their higher performance and ease of training on large datasets. 1.2.6 What makes deep learning different The primary reason deep learning took off so quickly is that it offered better performance on many problems. But that’s not the only reason. Deep learning also makes Licensed to <null> 18 CHAPTER 1 What is deep learning? problem-solving much easier, because it completely automates what used to be the most crucial step in a machine-learning workflow: feature engineering. Previous machine-learning techniques—shallow learning—only involved transforming the input data into one or two successive representation spaces, usually via simple transformations such as high-dimensional non-linear projections (SVMs) or decision trees. But the refined representations required by complex problems generally can’t be attained by such techniques. As such, humans had to go to great lengths to make the initial input data more amenable to processing by these methods: they had to manually engineer good layers of representations for their data. This is called feature engineering. Deep learning, on the other hand, completely automates this step: with deep learning, you learn all features in one pass rather than having to engineer them yourself. This has greatly simplified machine-learning workflows, often replacing sophisticated multistage pipelines with a single, simple, end-to-end deep-learning model. You may ask, if the crux of the issue is to have multiple successive layers of representations, could shallow methods be applied repeatedly to emulate the effects of deep learning? In practice, there are fast-diminishing returns to successive applications of shallow-learning methods, because the optimal first representation layer in a threelayer model isn’t the optimal first layer in a one-layer or two-layer model. What is transformative about deep learning is that it allows a model to learn all layers of representation jointly, at the same time, rather than in succession (greedily, as it’s called). With joint feature learning, whenever the model adjusts one of its internal features, all other features that depend on it automatically adapt to the change, without requiring human intervention. Everything is supervised by a single feedback signal: every change in the model serves the end goal. This is much more powerful than greedily stacking shallow models, because it allows for complex, abstract representations to be learned by breaking them down into long series of intermediate spaces (layers); each space is only a simple transformation away from the previous one. These are the two essential characteristics of how deep learning learns from data: the incremental, layer-by-layer way in which increasingly complex representations are developed, and the fact that these intermediate incremental representations are learned jointly, each layer being updated to follow both the representational needs of the layer above and the needs of the layer below. Together, these two properties have made deep learning vastly more successful than previous approaches to machine learning. 1.2.7 The modern machine-learning landscape A great way to get a sense of the current landscape of machine-learning algorithms and tools is to look at machine-learning competitions on Kaggle. Due to its highly competitive environment (some contests have thousands of entrants and milliondollar prizes) and to the wide variety of machine-learning problems covered, Kaggle offers a realistic way to assess what works and what doesn’t. So, what kind of algorithm is reliably winning competitions? What tools do top entrants use? Licensed to <null> Before deep learning: a brief history of machine learning 19 In 2016 and 2017, Kaggle was dominated by two approaches: gradient boosting machines and deep learning. Specifically, gradient boosting is used for problems where structured data is available, whereas deep learning is used for perceptual problems such as image classification. Practitioners of the former almost always use the excellent XGBoost library, which offers support for the two most popular languages of data science: Python and R. Meanwhile, most of the Kaggle entrants using deep learning use the Keras library, due to its ease of use, flexibility, and support of Python. These are the two techniques you should be the most familiar with in order to be successful in applied machine learning today: gradient boosting machines, for shallowlearning problems; and deep learning, for perceptual problems. In technical terms, this means you’ll need to be familiar with XGBoost and Keras—the two libraries that currently dominate Kaggle competitions. With this book in hand, you’re already one big step closer. Licensed to <null> 20 1.3 CHAPTER 1 What is deep learning? Why deep learning? Why now? The two key ideas of deep learning for computer vision—convolutional neural networks and backpropagation—were already well understood in 1989. The Long ShortTerm Memory (LSTM) algorithm, which is fundamental to deep learning for timeseries, was developed in 1997 and has barely changed since. So why did deep learning only take off after 2012? What changed in these two decades? In general, three technical forces are driving advances in machine learning: Hardware Datasets and benchmarks Algorithmic advances Because the field is guided by experimental findings rather than by theory, algorithmic advances only become possible when appropriate data and hardware are available to try new ideas (or scale up old ideas, as is often the case). Machine learning isn’t mathematics or physics, where major advances can be done with a pen and a piece of paper. It’s an engineering science. The real bottlenecks throughout the 1990s and 2000s were data and hardware. But here’s what happened during that time: the internet took off, and high-performance graphics chips were developed for the needs of the gaming market. 1.3.1 Hardware Between 1990 and 2010, off-the-shelf CPUs became faster by a factor of approximately 5,000. As a result, nowadays it’s possible to run small deep-learning models on your laptop, whereas this would have been intractable 25 years ago. But typical deep-learning models used in computer vision or speech recognition require orders of magnitude more computational power than what your laptop can deliver. Throughout the 2000s, companies like NVIDIA and AMD have been investing billions of dollars in developing fast, massively parallel chips (graphical processing units [GPUs]) to power the graphics of increasingly photorealistic video games— cheap, single-purpose supercomputers designed to render complex 3D scenes on your screen in real time. This investment came to benefit the scientific community when, in 2007, NVIDIA launched CUDA (https://developer.nvidia.com/about-cuda), a programming interface for its line of GPUs. A small number of GPUs started replacing massive clusters of CPUs in various highly parallelizable applications, beginning with physics modeling. Deep neural networks, consisting mostly of many small matrix multiplications, are also highly parallelizable; and around 2011, some researchers began to write CUDA implementations of neural nets—Dan Ciresan4 and Alex Krizhevsky5 were among the first. 4 5 See “Flexible, High Performance Convolutional Neural Networks for Image Classification,” Proceedings of the 22nd International Joint Conference on Artificial Intelligence (2011), www.ijcai.org/Proceedings/11/Papers/ 210.pdf. See “ImageNet Classification with Deep Convolutional Neural Networks,” Advances in Neural Information Processing Systems 25 (2012), http://mng.bz/2286. Licensed to <null> Why deep learning? Why now? 21 What happened is that the gaming market subsidized supercomputing for the next generation of artificial intelligence applications. Sometimes, big things begin as games. Today, the NVIDIA TITAN X, a gaming GPU that cost $1,000 at the end of 2015, can deliver a peak of 6.6 TFLOPS in single precision: 6.6 trillion float32 operations per second. That’s about 350 times more than what you can get out of a modern laptop. On a TITAN X, it takes only a couple of days to train an ImageNet model of the sort that would have won the ILSVRC competition a few years ago. Meanwhile, large companies train deep-learning models on clusters of hundreds of GPUs of a type developed specifically for the needs of deep learning, such as the NVIDIA Tesla K80. The sheer computational power of such clusters is something that would never have been possible without modern GPUs. What’s more, the deep-learning industry is starting to go beyond GPUs and is investing in increasingly specialized, efficient chips for deep learning. In 2016, at its annual I/O convention, Google revealed its tensor processing unit (TPU) project: a new chip design developed from the ground up to run deep neural networks, which is reportedly 10 times faster and far more energy efficient than top-of-the-line GPUs. 1.3.2 Data AI is sometimes heralded as the new industrial revolution. If deep learning is the steam engine of this revolution, then data is its coal: the raw material that powers our intelligent machines, without which nothing would be possible. When it comes to data, in addition to the exponential progress in storage hardware over the past 20 years (following Moore’s law), the game changer has been the rise of the internet, making it feasible to collect and distribute very large datasets for machine learning. Today, large companies work with image datasets, video datasets, and natural-language datasets that couldn’t have been collected without the internet. User-generated image tags on Flickr, for instance, have been a treasure trove of data for computer vision. So are YouTube videos. And Wikipedia is a key dataset for natural-language processing. If there’s one dataset that has been a catalyst for the rise of deep learning, it’s the ImageNet dataset, consisting of 1.4 million images that have been hand annotated with 1,000 image categories (1 category per image). But what makes ImageNet special isn’t just its large size, but also the yearly competition associated with it.6 As Kaggle has been demonstrating since 2010, public competitions are an excellent way to motivate researchers and engineers to push the envelope. Having common benchmarks that researchers compete to beat has greatly helped the recent rise of deep learning. 1.3.3 Algorithms In addition to hardware and data, until the late 2000s, we were missing a reliable way to train very deep neural networks. As a result, neural networks were still fairly shallow, 6 The ImageNet Large Scale Visual Recognition Challenge (ILSVRC), www.image-net.org/challenges/LSVRC. Licensed to <null> 22 CHAPTER 1 What is deep learning? using only one or two layers of representations; thus, they weren’t able to shine against more-refined shallow methods such as SVMs and random forests. The key issue was that of gradient propagation through deep stacks of layers. The feedback signal used to train neural networks would fade away as the number of layers increased. This changed around 2009–2010 with the advent of several simple but important algorithmic improvements that allowed for better gradient propagation: Better activation functions for neural layers Better weight-initialization schemes, starting with layer-wise pretraining, which was quickly abandoned Better optimization schemes, such as RMSProp and Adam Only when these improvements began to allow for training models with 10 or more layers did deep learning start to shine. Finally, in 2014, 2015, and 2016, even more advanced ways to help gradient propagation were discovered, such as batch normalization, residual connections, and depthwise separable convolutions. Today we can train from scratch models that are thousands of layers deep. 1.3.4 A new wave of investment As deep learning became the new state of the art for computer vision in 2012–2013, and eventually for all perceptual tasks, industry leaders took note. What followed was a gradual wave of industry investment far beyond anything previously seen in the history of AI. In 2011, right before deep learning took the spotlight, the total venture capital investment in AI was around $19 million, which went almost entirely to practical applications of shallow machine-learning approaches. By 2014, it had risen to a staggering $394 million. Dozens of startups launched in these three years, trying to capitalize on the deep-learning hype. Meanwhile, large tech companies such as Google, Facebook, Baidu, and Microsoft have invested in internal research departments in amounts that would most likely dwarf the flow of venture-capital money. Only a few numbers have surfaced: In 2013, Google acquired the deep-learning startup DeepMind for a reported $500 million—the largest acquisition of an AI company in history. In 2014, Baidu started a deep-learning research center in Silicon Valley, investing $300 million in the project. The deep-learning hardware startup Nervana Systems was acquired by Intel in 2016 for over $400 million. Machine learning—in particular, deep learning—has become central to the product strategy of these tech giants. In late 2015, Google CEO Sundar Pichai stated, “Machine learning is a core, transformative way by which we’re rethinking how we’re doing everything. We’re thoughtfully applying it across all our products, be it search, ads, YouTube, or Play. And we’re in early days, but you’ll see us—in a systematic way— apply machine learning in all these areas.”7 7 Sundar Pichai, Alphabet earnings call, Oct. 22, 2015. Licensed to <null> Why deep learning? Why now? 23 As a result of this wave of investment, the number of people working on deep learning went in just five years from a few hundred to tens of thousands, and research progress has reached a frenetic pace. There are currently no signs that this trend will slow any time soon. 1.3.5 The democratization of deep learning One of the key factors driving this inflow of new faces in deep learning has been the democratization of the toolsets used in the field. In the early days, doing deep learning required significant C++ and CUDA expertise, which few people possessed. Nowadays, basic Python scripting skills suffice to do advanced deep-learning research. This has been driven most notably by the development of Theano and then TensorFlow—two symbolic tensor-manipulation frameworks for Python that support autodifferentiation, greatly simplifying the implementation of new models—and by the rise of user-friendly libraries such as Keras, which makes deep learning as easy as manipulating LEGO bricks. After its release in early 2015, Keras quickly became the go-to deep-learning solution for large numbers of new startups, graduate students, and researchers pivoting into the field. 1.3.6 Will it last? Is there anything special about deep neural networks that makes them the “right” approach for companies to be investing in and for researchers to flock to? Or is deep learning just a fad that may not last? Will we still be using deep neural networks in 20 years? Deep learning has several properties that justify its status as an AI revolution, and it’s here to stay. We may not be using neural networks two decades from now, but whatever we use will directly inherit from modern deep learning and its core concepts. These important properties can be broadly sorted into three categories: Simplicity—Deep learning removes the need for feature engineering, replacing complex, brittle, engineering-heavy pipelines with simple, end-to-end trainable models that are typically built using only five or six different tensor operations. Scalability—Deep learning is highly amenable to parallelization on GPUs or TPUs, so it can take full advantage of Moore’s law. In addition, deep-learning models are trained by iterating over small batches of data, allowing them to be trained on datasets of arbitrary size. (The only bottleneck is the amount of parallel computational power available, which, thanks to Moore’s law, is a fastmoving barrier.) Versatility and reusability—Unlike many prior machine-learning approaches, deep-learning models can be trained on additional data without restarting from scratch, making them viable for continuous online learning—an important property for very large production models. Furthermore, trained deep-learning models are repurposable and thus reusable: for instance, it’s possible to take a deep-learning model trained for image classification and drop it into a videoprocessing pipeline. This allows us to reinvest previous work into increasingly Licensed to <null> 24 CHAPTER 1 What is deep learning? complex and powerful models. This also makes deep learning applicable to fairly small datasets. Deep learning has only been in the spotlight for a few years, and we haven’t yet established the full scope of what it can do. With every passing month, we learn about new use cases and engineering improvements that lift previous limitations. Following a scientific revolution, progress generally follows a sigmoid curve: it starts with a period of fast progress, which gradually stabilizes as researchers hit hard limitations, and then further improvements become incremental. Deep learning in 2017 seems to be in the first half of that sigmoid, with much more progress to come in the next few years. Licensed to <null> Before we begin: the mathematical building blocks of neural networks This chapter covers A first example of a neural network Tensors and tensor operations How neural networks learn via backpropagation and gradient descent Understanding deep learning requires familiarity with many simple mathematical concepts: tensors, tensor operations, differentiation, gradient descent, and so on. Our goal in this chapter will be to build your intuition about these notions without getting overly technical. In particular, we’ll steer away from mathematical notation, which can be off-putting for those without any mathematics background and isn’t strictly necessary to explain things well. To add some context for tensors and gradient descent, we’ll begin the chapter with a practical example of a neural network. Then we’ll go over every new concept 25 Licensed to <null> 26 CHAPTER 2 Before we begin: the mathematical building blocks of neural networks that’s been introduced, point by point. Keep in mind that these concepts will be essential for you to understand the practical examples that will come in the following chapters! After reading this chapter, you’ll have an intuitive understanding of how neural networks work, and you’ll be able to move on to practical applications—which will start with chapter 3. Licensed to <null> A first look at a neural network 2.1 27 A first look at a neural network Let’s look at a concrete example of a neural network that uses the Python library Keras to learn to classify handwritten digits. Unless you already have experience with Keras or similar libraries, you won’t understand everything about this first example right away. You probably haven’t even installed Keras yet; that’s fine. In the next chapter, we’ll review each element in the example and explain them in detail. So don’t worry if some steps seem arbitrary or look like magic to you! We’ve got to start somewhere. The problem we’re trying to solve here is to classify grayscale images of handwritten digits (28 × 28 pixels) into their 10 categories (0 through 9). We’ll use the MNIST dataset, a classic in the machine-learning community, which has been around almost as long as the field itself and has been intensively studied. It’s a set of 60,000 training images, plus 10,000 test images, assembled by the National Institute of Standards and Technology (the NIST in MNIST) in the 1980s. You can think of “solving” MNIST as the “Hello World” of deep learning—it’s what you do to verify that your algorithms are working as expected. As you become a machine-learning practitioner, you’ll see MNIST come up over and over again, in scientific papers, blog posts, and so on. You can see some MNIST samples in figure 2.1. Note on classes and labels In machine learning, a category in a classification problem is called a class. Data points are called samples. The class associated with a specific sample is called a label. Figure 2.1 MNIST sample digits You don’t need to try to reproduce this example on your machine just now. If you wish to, you’ll first need to set up Keras, which is covered in section 3.3. The MNIST dataset comes preloaded in Keras, in the form of a set of four Numpy arrays. Listing 2.1 Loading the MNIST dataset in Keras from keras.datasets import mnist (train_images, train_labels), (test_images, test_labels) = mnist.load_data() train_images and train_labels form the training set, the data that the model will learn from. The model will then be tested on the test set, test_images and test_labels. Licensed to <null> 28 CHAPTER 2 Before we begin: the mathematical building blocks of neural networks The images are encoded as Numpy arrays, and the labels are an array of digits, ranging from 0 to 9. The images and labels have a one-to-one correspondence. Let’s look at the training data: >>> train_images.shape (60000, 28, 28) >>> len(train_labels) 60000 >>> train_labels array([5, 0, 4, ..., 5, 6, 8], dtype=uint8) And here’s the test data: >>> test_images.shape (10000, 28, 28) >>> len(test_labels) 10000 >>> test_labels array([7, 2, 1, ..., 4, 5, 6], dtype=uint8) The workflow will be as follows: First, we’ll feed the neural network the training data, train_images and train_labels. The network will then learn to associate images and labels. Finally, we’ll ask the network to produce predictions for test_images, and we’ll verify whether these predictions match the labels from test_labels. Let’s build the network—again, remember that you aren’t expected to understand everything about this example yet. Listing 2.2 The network architecture from keras import models from keras import layers network = models.Sequential() network.add(layers.Dense(512, activation='relu', input_shape=(28 * 28,))) network.add(layers.Dense(10, activation='softmax')) The core building block of neural networks is the layer, a data-processing module that you can think of as a filter for data. Some data goes in, and it comes out in a more useful form. Specifically, layers extract representations out of the data fed into them—hopefully, representations that are more meaningful for the problem at hand. Most of deep learning consists of chaining together simple layers that will implement a form of progressive data distillation. A deep-learning model is like a sieve for data processing, made of a succession of increasingly refined data filters—the layers. Here, our network consists of a sequence of two Dense layers, which are densely connected (also called fully connected) neural layers. The second (and last) layer is a 10-way softmax layer, which means it will return an array of 10 probability scores (summing to 1). Each score will be the probability that the current digit image belongs to one of our 10 digit classes. Licensed to <null> A first look at a neural network 29 To make the network ready for training, we need to pick three more things, as part of the compilation step: A loss function—How the network will be able to measure its performance on the training data, and thus how it will be able to steer itself in the right direction. An optimizer—The mechanism through which the network will update itself based on the data it sees and its loss function. Metrics to monitor during training and testing—Here, we’ll only care about accuracy (the fraction of the images that were correctly classified). The exact purpose of the loss function and the optimizer will be made clear throughout the next two chapters. Listing 2.3 The compilation step network.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy']) Before training, we’ll preprocess the data by reshaping it into the shape the network expects and scaling it so that all values are in the [0, 1] interval. Previously, our training images, for instance, were stored in an array of shape (60000, 28, 28) of type uint8 with values in the [0, 255] interval. We transform it into a float32 array of shape (60000, 28 * 28) with values between 0 and 1. Listing 2.4 Preparing the image data train_images = train_images.reshape((60000, 28 * 28)) train_images = train_images.astype('float32') / 255 test_images = test_images.reshape((10000, 28 * 28)) test_images = test_images.astype('float32') / 255 We also need to categorically encode the labels, a step that’s explained in chapter 3. Listing 2.5 Preparing the labels from keras.utils import to_categorical train_labels = to_categorical(train_labels) test_labels = to_categorical(test_labels) We’re now ready to train the network, which in Keras is done via a call to the network’s fit method—we fit the model to its training data: >>> network.fit(train_images, train_labels, epochs=5, batch_size=128) Epoch 1/5 60000/60000 [==============================] - 9s - loss: 0.2524 - acc: 0.9273 Epoch 2/5 51328/60000 [========================>.....] - ETA: 1s - loss: 0.1035 - acc: 0.9692 Licensed to <null> 30 CHAPTER 2 Before we begin: the mathematical building blocks of neural networks Two quantities are displayed during training: the loss of the network over the training data, and the accuracy of the network over the training data. We quickly reach an accuracy of 0.989 (98.9%) on the training data. Now let’s check that the model performs well on the test set, too: >>> test_loss, test_acc = network.evaluate(test_images, test_labels) >>> print('test_acc:', test_acc) test_acc: 0.9785 The test-set accuracy turns out to be 97.8%—that’s quite a bit lower than the training set accuracy. This gap between training accuracy and test accuracy is an example of overfitting: the fact that machine-learning models tend to perform worse on new data than on their training data. Overfitting is a central topic in chapter 3. This concludes our first example—you just saw how you can build and train a neural network to classify handwritten digits in less than 20 lines of Python code. In the next chapter, I’ll go into detail about every moving piece we just previewed and clarify what’s going on behind the scenes. You’ll learn about tensors, the data-storing objects going into the network; tensor operations, which layers are made of; and gradient descent, which allows your network to learn from its training examples. Licensed to <null> Data representations for neural networks 2.2 31 Data representations for neural networks In the previous example, we started from data stored in multidimensional Numpy arrays, also called tensors. In general, all current machine-learning systems use tensors as their basic data structure. Tensors are fundamental to the field—so fundamental that Google’s TensorFlow was named after them. So what’s a tensor? At its core, a tensor is a container for data—almost always numerical data. So, it’s a container for numbers. You may be already familiar with matrices, which are 2D tensors: tensors are a generalization of matrices to an arbitrary number of dimensions (note that in the context of tensors, a dimension is often called an axis). 2.2.1 Scalars (0D tensors) A tensor that contains only one number is called a scalar (or scalar tensor, or 0-dimensional tensor, or 0D tensor). In Numpy, a float32 or float64 number is a scalar tensor (or scalar array). You can display the number of axes of a Numpy tensor via the ndim attribute; a scalar tensor has 0 axes (ndim == 0). The number of axes of a tensor is also called its rank. Here’s a Numpy scalar: >>> import numpy as np >>> x = np.array(12) >>> x array(12) >>> x.ndim 0 2.2.2 Vectors (1D tensors) An array of numbers is called a vector, or 1D tensor. A 1D tensor is said to have exactly one axis. Following is a Numpy vector: >>> x = np.array([12, 3, 6, 14]) >>> x array([12, 3, 6, 14]) >>> x.ndim 1 This vector has five entries and so is called a 5-dimensional vector. Don’t confuse a 5D vector with a 5D tensor! A 5D vector has only one axis and has five dimensions along its axis, whereas a 5D tensor has five axes (and may have any number of dimensions along each axis). Dimensionality can denote either the number of entries along a specific axis (as in the case of our 5D vector) or the number of axes in a tensor (such as a 5D tensor), which can be confusing at times. In the latter case, it’s technically more correct to talk about a tensor of rank 5 (the rank of a tensor being the number of axes), but the ambiguous notation 5D tensor is common regardless. 2.2.3 Matrices (2D tensors) An array of vectors is a matrix, or 2D tensor. A matrix has two axes (often referred to rows and columns). You can visually interpret a matrix as a rectangular grid of numbers. This is a Numpy matrix: Licensed to <null> 32 CHAPTER 2 Before we begin: the mathematical building blocks of neural networks >>> x = np.array([[5, 78, 2, 34, 0], [6, 79, 3, 35, 1], [7, 80, 4, 36, 2]]) >>> x.ndim 2 The entries from the first axis are called the rows, and the entries from the second axis are called the columns. In the previous example, [5, 78, 2, 34, 0] is the first row of x, and [5, 6, 7] is the first column. 2.2.4 3D tensors and higher-dimensional tensors If you pack such matrices in a new array, you obtain a 3D tensor, which you can visually interpret as a cube of numbers. Following is a Numpy 3D tensor: >>> x = np.array([[[5, [6, [7, [[5, [6, [7, [[5, [6, [7, >>> x.ndim 3 78, 79, 80, 78, 79, 80, 78, 79, 80, 2, 3, 4, 2, 3, 4, 2, 3, 4, 34, 35, 36, 34, 35, 36, 34, 35, 36, 0], 1], 2]], 0], 1], 2]], 0], 1], 2]]]) By packing 3D tensors in an array, you can create a 4D tensor, and so on. In deep learning, you’ll generally manipulate tensors that are 0D to 4D, although you may go up to 5D if you process video data. 2.2.5 Key attributes A tensor is defined by three key attributes: Number of axes (rank)—For instance, a 3D tensor has three axes, and a matrix has two axes. This is also called the tensor’s ndim in Python libraries such as Numpy. Shape—This is a tuple of integers that describes how many dimensions the ten- sor has along each axis. For instance, the previous matrix example has shape (3, 5), and the 3D tensor example has shape (3, 3, 5). A vector has a shape with a single element, such as (5,), whereas a scalar has an empty shape, (). Data type (usually called dtype in Python libraries)—This is the type of the data contained in the tensor; for instance, a tensor’s type could be float32, uint8, float64, and so on. On rare occasions, you may see a char tensor. Note that string tensors don’t exist in Numpy (or in most other libraries), because tensors live in preallocated, contiguous memory segments: and strings, being variable length, would preclude the use of this implementation. Licensed to <null> Data representations for neural networks 33 To make this more concrete, let’s look back at the data we processed in the MNIST example. First, we load the MNIST dataset: from keras.datasets import mnist (train_images, train_labels), (test_images, test_labels) = mnist.load_data() Next, we display the number of axes of the tensor train_images , the ndim attribute: >>> print(train_images.ndim) 3 Here’s its shape: >>> print(train_images.shape) (60000, 28, 28) And this is its data type, the dtype attribute: >>> print(train_images.dtype) uint8 So what we have here is a 3D tensor of 8-bit integers. More precisely, it’s an array of 60,000 matrices of 28 × 8 integers. Each such matrix is a grayscale image, with coefficients between 0 and 255. Let’s display the fourth digit in this 3D tensor, using the library Matplotlib (part of the standard scientific Python suite); see figure 2.2. Listing 2.6 Displaying the fourth digit digit = train_images import matplotlib.pyplot as plt plt.imshow(digit, cmap=plt.cm.binary) plt.show() Figure 2.2 The fourth sample in our dataset Licensed to <null> 34 2.2.6 CHAPTER 2 Before we begin: the mathematical building blocks of neural networks Manipulating tensors in Numpy In the previous example, we selected a specific digit alongside the first axis using the syntax train_images[i]. Selecting specific elements in a tensor is called tensor slicing. Let’s look at the tensor-slicing operations you can do on Numpy arrays. The following example selects digits #10 to #100 (#100 isn’t included) and puts them in an array of shape (90, 28, 28): >>> my_slice = train_images[10:100] >>> print(my_slice.shape) (90, 28, 28) It’s equivalent to this more detailed notation, which specifies a start index and stop index for the slice along each tensor axis. Note that : is equivalent to selecting the entire axis: Equivalent to the previous example >>> my_slice = train_images[10:100, :, :] >>> my_slice.shape Also equivalent to the (90, 28, 28) previous example >>> my_slice = train_images[10:100, 0:28, 0:28] >>> my_slice.shape (90, 28, 28) In general, you may select between any two indices along each tensor axis. For instance, in order to select 14 × 14 pixels in the bottom-right corner of all images, you do this: my_slice = train_images[:, 14:, 14:] It’s also possible to use negative indices. Much like negative indices in Python lists, they indicate a position relative to the end of the current axis. In order to crop the images to patches of 14 × 14 pixels centered in the middle, you do this: my_slice = train_images[:, 7:-7, 7:-7] 2.2.7 The notion of data batches In general, the first axis (axis 0, because indexing starts at 0) in all data tensors you’ll come across in deep learning will be the samples axis (sometimes called the samples dimension). In the MNIST example, samples are images of digits. In addition, deep-learning models don’t process an entire dataset at once; rather, they break the data into small batches. Concretely, here’s one batch of our MNIST digits, with batch size of 128: batch = train_images[:128] And here’s the next batch: batch = train_images[128:256] And the n th batch: batch = train_images[128 * n:128 * (n + 1)] Licensed to <null> Data representations for neural networks 35 When considering such a batch tensor, the first axis (axis 0) is called the batch axis or batch dimension. This is a term you’ll frequently encounter when using Keras and other deep-learning libraries. 2.2.8 Real-world examples of data tensors Let’s make data tensors more concrete with a few examples similar to what you’ll encounter later. The data you’ll manipulate will almost always fall into one of the following categories: Vector data—2D tensors of shape (samples, features) Timeseries data or sequence data—3D tensors of shape (samples, timesteps, features) Images—4D tensors of shape (samples, height, width, channels) or (samples, channels, height, width) Video—5D tensors of shape (samples, frames, height, width, channels) or (samples, frames, channels, height, width) 2.2.9 Vector data This is the most common case. In such a dataset, each single data point can be encoded as a vector, and thus a batch of data will be encoded as a 2D tensor (that is, an array of vectors), where the first axis is the samples axis and the second axis is the features axis. Let’s take a look at two examples: An actuarial dataset of people, where we consider each person’s age, ZIP code, and income. Each person can be characterized as a vector of 3 values, and thus an entire dataset of 100,000 people can be stored in a 2D tensor of shape (100000, 3). A dataset of text documents, where we represent each document by the counts of how many times each word appears in it (out of a dictionary of 20,000 common words). Each document can be encoded as a vector of 20,000 values (one count per word in the dictionary), and thus an entire dataset of 500 documents can be stored in a tensor of shape (500, 20000). 2.2.10 Timeseries data or sequence data Whenever time matters in your data (or the notion of sequence order), it makes sense to store it in a 3D tensor with an explicit time axis. Each sample can be encoded as a sequence of vectors (a 2D tensor), and thus a batch of data will be encoded as a 3D tensor (see figure 2.3). Features Samples Timesteps Figure 2.3 Licensed to <null> A 3D timeseries data tensor 36 CHAPTER 2 Before we begin: the mathematical build