Main
Deep Learning from Scratch: Building with Python from First Principles
Deep Learning from Scratch: Building with Python from First Principles
Seth Weidman
With the resurgence of neural networks in the 2010s, deep learning has become essential for machine learning practitioners and even many software engineers. This book provides a comprehensive introduction for data scientists and software engineers with machine learning experience. You’ll start with deep learning basics and move quickly to the details of important advanced architectures, implementing everything from scratch along the way.
Author Seth Weidman shows you how neural networks work using a first principles approach. You’ll learn how to apply multilayer neural networks, convolutional neural networks, and recurrent neural networks from the ground up. With a thorough understanding of how neural networks work mathematically, computationally, and conceptually, you’ll be set up for success on all future deep learning projects.
This book provides:
• Extremely clear and thorough mental models—accompanied by working code examples and mathematical explanations—for understanding neural networks
• Methods for implementing multilayer neural networks from scratch, using an easytounderstand objectoriented framework
• Working implementations and clearcut explanations of convolutional and recurrent neural networks
• Implementation of these neural network concepts using the popular PyTorch framework
Author Seth Weidman shows you how neural networks work using a first principles approach. You’ll learn how to apply multilayer neural networks, convolutional neural networks, and recurrent neural networks from the ground up. With a thorough understanding of how neural networks work mathematically, computationally, and conceptually, you’ll be set up for success on all future deep learning projects.
This book provides:
• Extremely clear and thorough mental models—accompanied by working code examples and mathematical explanations—for understanding neural networks
• Methods for implementing multilayer neural networks from scratch, using an easytounderstand objectoriented framework
• Working implementations and clearcut explanations of convolutional and recurrent neural networks
• Implementation of these neural network concepts using the popular PyTorch framework
Categories:
Computers\\Cybernetics: Artificial Intelligence
Year:
2019
Edition:
1
Publisher:
O’Reilly Media
Language:
english
Pages:
252
ISBN 10:
1492041416
ISBN 13:
9781492041412
File:
EPUB, 4.91 MB
Download (epub, 4.91 MB)
 Checking other formats...
 Convert to FB2
 Convert to PDF
 Convert to MOBI
 Convert to TXT
 Convert to RTF
 Converted file can differ from the original. If possible, download the file in its original format.
 Please login to your account first

Need help? Please read our short guide how to send a book to Kindle.
The file will be sent to your email address. It may take up to 15 minutes before you receive it.
The file will be sent to your Kindle account. It may takes up to 15 minutes before you received it.
Please note you need to add our NEW email km@bookmail.org to approved email addresses. Read more.
Please note you need to add our NEW email km@bookmail.org to approved email addresses. Read more.
You may be interested in
Most frequently terms
neural^{392}
input^{288}
ndarray^{272}
output^{252}
neural networks^{214}
layers^{185}
gradient^{177}
neural network^{176}
matrix^{146}
inputs^{144}
def^{144}
weights^{130}
gradients^{125}
compute^{120}
linear^{111}
derivative^{102}
convolutional^{102}
pytorch^{95}
activation^{94}
derivatives^{86}
epochs^{84}
validation^{83}
diagram^{77}
sigmoid^{76}
convolution^{72}
optimizer^{72}
parameters^{72}
neurons^{71}
predictions^{68}
param^{68}
linear regression^{67}
batch^{66}
validation loss^{61}
vector^{61}
update^{60}
dropout^{60}
representation^{59}
array^{57}
dimensional^{55}
math^{54}
trainer^{51}
outputs^{51}
parameter^{51}
rnns^{49}
tensor^{49}
implement^{46}
computing^{46}
ndarrays^{46}
prediction^{43}
rnn^{43}
softmax^{42}
convolutions^{42}
neuralnetwork^{42}
int^{41}
batches^{40}
nonlinear^{40}
nested^{40}
implementing^{39}
You can write a book review and share your experiences. Other readers will always be interested in your opinion of the books you've read. Whether you've loved the book or not, if you give your honest and detailed thoughts then people will find new books that are right for them.
1

2

Deep Learning from Scratch Building with Python from First Principles Seth Weidman Deep Learning from Scratch by Seth Weidman Copyright © 2019 Seth Weidman. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://oreilly.com). For more information, contact our corporate/institutional sales department: 8009989938 or corporate@oreilly.com. Development Editor: Melissa Potter Acquisitions Editors: Jon Hassell and Mike Loukides Production Editor: Katherine Tozer Copyeditor: Arthur Johnson Proofreader: Rachel Monaghan Indexer: Judith McConville Interior Designer: David Futato Cover Designer: Karen Montgomery Illustrator: Rebecca Demarest September 2019: First Edition Revision History for the First Edition 20190906: First Release See http://oreilly.com/catalog/errata.csp?isbn=9781492041412 for release details. The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. Deep Learning from Scratch, the cover image, and related trade dress are trademarks of O’Reilly Media, Inc. The views expressed in this work are those of the author, and do not represent the publisher’s views. While the publisher and the author have used good faith efforts to ensure that the information and instructions contained in this work are accurate, the publisher and the author disclaim all responsibility for errors or omissions, including without limitation responsibility for damages resulting from the use of or reliance on this work. Use of the information and instructions contained in this work is at your own risk. If any code samples or other technology this work contains or describes is subject to open source licenses or the intellectual property rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/or rights. 9781492041412 [LSI] Preface If you’ve tried to learn about neural networks and deep learning, you’ve probably encountered an abundance of resources, from blog posts to MOOCs (massive open online courses, such as those offered on Coursera and Udacity) of varying quality and even some books—I know I did when I started exploring the subject a few years ago. However, if you’re reading this preface, it’s likely that each explanation of neural networks that you’ve come across is lacking in some way. I found the same thing when I started learning: the various explanations were like blind men describing different parts of an elephant, but none describing the whole thing. That is what led me to write this book. These existing resources on neural networks mostly fall into two categories. Some are conceptual and mathematical, containing both the drawings one typically finds in explanations of neural networks, of circles connected by lines with arrows on the ends, as well as extensive mathematical explanations of what is going on so you can “understand the theory.” A prototypical example of this is the very good book Deep Learning by Ian Goodfellow et al. (MIT Press). Other resources have dense blocks of code that, if run, appear to show a loss value decreasing over time and thus a neural network “learning.” For instance, the following example from the PyTorch documentation does indeed define and train a simple neural network on randomly generated data: # N is batch size; D_in is input dimension; # H is hidden dimension; D_out is output dimension. N, D_in, H, D_out = 64, 1000, 100, 10 # Create random input and output data x = torch.randn(N, D_in, device=device, dtype=dtype) y = torch.randn(N, D_out, device=device, dtype=dtype) # Randomly initialize weights w1 = torch.randn(D_in, H, device=device, dtype=dtype) w2 = torch.randn(H, D_out, device=device, dtype=dtype) learning_rate = 1e6 for t in range(500): # Forward pass: compute predicted y h = x.mm(w1) h_relu = h.clamp(min=0) y_pred = h_relu.mm(w2) # Compute and print loss loss = (y_pred  y).pow(2).sum().item() print(t, loss) # Backprop to compute gradients of w1 and w2 with respect to loss grad_y_pred = 2.0 * (y_pred  y) grad_w2 = h_relu.t().mm(grad_y_pred) grad_h_relu = grad_y_pred.mm(w2.t()) grad_h = grad_h_relu.clone() grad_h[h < 0] = 0 grad_w1 = x.t().mm(grad_h) # Update weights using gradient descent w1 = learning_rate * grad_w1 w2 = learning_rate * grad_w2 Explanations like this, of course, don’t give much insight into “what is really going on”: the underlying mathematical principles, the individual neural network components contained here and how they work together, and so on.1 What would a good explanation of neural networks contain? For an answer, it is instructive to look at how other computer science concepts are explained: if you want to learn about sorting algorithms, for example, there are textbooks that will contain: An explanation of the algorithm, in plain English A visual explanation of how the algorithm works, of the kind that you would draw on a whiteboard during a coding interview Some mathematical explanation of “why the algorithm works”2 Pseudocode implementing the algorithm One rarely—or never—finds these elements of an explanation of neural networks side by side, even though it seems obvious to me that a proper explanation of neural networks should be done this way; this book is an attempt to fill that gap. Understanding Neural Networks Requires Multiple Mental Models I am not a researcher, and I do not have a Ph.D. I have, however, taught data science professionally: I taught a couple of data science bootcamps with a company called Metis, and then I traveled around the world for a year with Metis doing one to fiveday workshops for companies in many different industries in which I explained machine learning and basic software engineering concepts to their employees. I’ve always loved teaching and have always been fascinated by the question of how best to explain technical concepts, most recently focusing on concepts in machine learning and statistics. With neural networks, I’ve found the most challenging part is conveying the correct “mental model” for what a neural network is, especially since understanding neural networks fully requires not just one but several mental models, all of which illuminate different (but still essential) aspects of how neural networks work. To illustrate this: the following four sentences are all correct answers to the question “What is a neural network?”: A neural network is a mathematical function that takes in inputs and produces outputs. A neural network is a computational graph through which multidimensional arrays flow. A neural network is made up of layers, each of which can be thought of as having a number of “neurons.” A neural network is a universal function approximator that can in theory represent the solution to any supervised learning problem. Indeed, many of you reading this have probably heard one or more of these before, and may have a reasonable understanding of what they mean and what their implications are for how neural networks work. To fully understand them, however, we’ll have to understand all of them and show how they are connected—how is the fact that a neural network can be represented as a computational graph connected to the notion of “layers,” for example? Furthermore, to make all of this precise, we’ll implement all of these concepts from scratch, in Python, and stitch them together to make working neural networks that you can train on your laptop. Nevertheless, despite the fact that we’ll spend a substantial amount of time on implementation details, the purpose of implementing these models in Python is to solidify and make precise our understanding of the concepts; it is not to write as concise or performant of a neural network library as possible. My goal is that after you’ve read this book, you’ll have such a solid understanding of all of these mental models (and their implications for how neural networks should be implemented) that learning related concepts or doing further projects in the field will be much easier. Chapter Outlines The first three chapters are the most important ones and could themselves form a standalone book. In Chapter 1 I’ll show how mathematical functions can be represented as a series of operations linked together to form a computational graph, and show how this representation lets us compute the derivatives of these functions’ outputs with respect to their inputs using the chain rule from calculus. At the end of this chapter, I’ll introduce a very important operation, the matrix multiplication, and show how it can fit into a mathematical function represented in this way while still allowing us to compute the derivatives we’ll end up needing for deep learning. In Chapter 2 we’ll directly use the building blocks we created in Chapter 1 to build and train models to solve a realworld problem: specifically, we’ll use them to build both linear regression and neural network models to predict housing prices on a realworld dataset. I’ll show that the neural network performs better than the linear regression and try to give some intuition for why. The “first principles” approach to building the models in this chapter should give you a very good idea of how neural networks work, but will also show the limited capability of the stepbystep, purely firstprinciplesbased approach to defining deep learning models; this will motivate Chapter 3. In Chapter 3 we’ll take the building blocks from the firstprinciplesbased approach of the first two chapters and use them to build the “higher level” components that make up all deep learning models: Layers, Models, Optimizers, and so on. We’ll end this chapter by training a deep learning model, defined from scratch, on the same dataset from Chapter 2 and showing that it performs better than our simple neural network. As it turns out, there are few theoretical guarantees that a neural network with a given architecture will actually find a good solution on a given dataset when trained using the standard training techniques we’ll use in this book. In Chapter 4 we’ll cover the most important “training tricks” that generally increase the probability that a neural network will find a good solution, and, wherever possible, give some mathematical intuition as to why they work. In Chapter 5 I cover the fundamental ideas behind convolutional neural networks (CNNs), a kind of neural network architecture specialized for understanding images. There are many explanations of CNNs out there, so I’ll focus on explaining the absolute essentials of CNNs and how they differ from regular neural networks: specifically, how CNNs result in each layer of neurons being organized into “feature maps,” and how two of these layers (each made up of multiple feature maps) are connected together via convolutional filters. In addition, just as we coded the regular layers in a neural network from scratch, we’ll code convolutional layers from scratch to reinforce our understanding of how they work. Throughout the first five chapters, we’ll build up a miniature neural network library that defines neural networks as a series of Layers—which are themselves made up of a series of Operations—that send inputs forward and gradients backward. This is not how most neural networks are implemented in practice; instead, they use a technique called automatic differentiation. I’ll give a quick illustration of automatic differentiation at the beginning of Chapter 6 and use it to motivate the main subject of the chapter: recurrent neural networks (RNNs), the neural network architecture typically used for understanding data in which the data points appear sequentially, such as time series data or natural language data. I’ll explain the workings of “vanilla RNNs” and of two variants: GRUs and LSTMs (and of course implement all three from scratch); throughout, I’ll be careful to distinguish between the elements that are shared across all of these RNN variants and the specific ways in which these variants differ. Finally, in Chapter 7, I’ll show how everything we did from scratch in Chapters 1–6 can be implemented using the highperformance, open source neural network library PyTorch. Learning a framework like this is essential for progressing your learning about neural networks; but diving in and learning a framework without first having a solid understanding of how and why neural networks work would severely limit your learning in the long term. The goal of the progression of chapters in this book is to give you the power to write extremely highperformance neural networks (by teaching you PyTorch) while still setting you up for longterm learning and success (by teaching you the fundamentals before you learn PyTorch). We’ll conclude with a quick illustration of how neural networks can be used for unsupervised learning. My goal here was to write the book that I wish had existed when I started to learn the subject a few years ago. I hope you will find this book helpful. Onward! Conventions Used in This Book The following typographical conventions are used in this book: Italic Indicates new terms, URLs, email addresses, filenames, and file extensions. Constant width Used for program listings, as well as within paragraphs to refer to program elements such as variable or function names, databases, data types, environment variables, statements, and keywords. Constant width bold Shows commands or other text that should be typed literally by the user. Constant width italic Used for text that should be replaced with usersupplied values or by values determined by context and for comments in code examples. The Pythagorean Theorem is . Note This element signifies a general note. Using Code Examples Supplemental material (code examples, exercises, etc.) is available for download at the book’s GitHub repository. This book is here to help you get your job done. In general, if example code is offered with this book, you may use it in your programs and documentation. You do not need to contact us for permission unless you’re reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a CDROM of examples from O’Reilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your product’s documentation does require permission. We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: “Deep Learning from Scratch by Seth Weidman (O’Reilly). Copyright 2019 Seth Weidman, 9781492041412.” If you feel your use of code examples falls outside fair use or the permission given above, feel free to contact us at permissions@oreilly.com. O’Reilly Online Learning Note For almost 40 years, O’Reilly Media has provided technology and business training, knowledge, and insight to help companies succeed. Our unique network of experts and innovators share their knowledge and expertise through books, articles, conferences, and our online learning platform. O’Reilly’s online learning platform gives you ondemand access to live training courses, indepth learning paths, interactive coding environments, and a vast collection of text and video from O’Reilly and 200+ other publishers. For more information, please visit http://oreilly.com. How to Contact Us Please address comments and questions concerning this book to the publisher: O’Reilly Media, Inc. 1005 Gravenstein Highway North Sebastopol, CA 95472 8009989938 (in the United States or Canada) 7078290515 (international or local) 7078290104 (fax) We have a web page for this book, where we list errata, examples, and any additional information. You can access this page at https://oreil.ly/dlfromscratch. Email bookquestions@oreilly.com to comment or ask technical questions about this book. For more information about our books, courses, conferences, and news, see our website at http://www.oreilly.com. Find us on Facebook: http://facebook.com/oreilly Follow us on Twitter: http://twitter.com/oreillymedia Watch us on YouTube: http://www.youtube.com/oreillymedia Acknowledgments I’d like to thank my editor, Melissa Potter, along with the team at O’Reilly, who were meticulous with their feedback and responsive to my questions throughout the process. I’d like to give a special thanks to several people whose work to make technical concepts in machine learning accessible to a wider audience has directly influenced me, and a couple of whom I’ve been lucky enough to have gotten to know personally: in a randomly generated order, these people are Brandon Rohrer, Joel Grus, Jeremy Watt, and Andrew Trask. I’d like to thank my boss at Metis and my director at Facebook, who were unreasonably supportive of my carving out time to work on this project. I’d like to give a special thank you and acknowledgment to Mat Leonard, who was my coauthor for a brief period of time before we decided to go our separate ways. Mat helped organize the code for the minilibrary associated with the book—lincoln—and gave me very helpful feedback on some extremely unpolished versions of the first two chapters, writing his own versions of large sections of these chapters in the process. Finally, I’d like to thank my friends Eva and John, both of whom directly encouraged and inspired me to take the plunge and actually start writing. I’d also like to thank my many friends in San Francisco who tolerated my general preoccupation and worry about the book as well as my lack of availability to hang out for many months, and who were unwaveringly supportive when I needed them to be. 1 To be fair, this example was intended as an illustration of the PyTorch library for those who already understand neural networks, not as an instructive tutorial. Still, many tutorials follow this style, showing only the code along with some brief explanations. 2 Specifically, in the case of sorting algorithms, why the algorithm terminates with a properly sorted list. Chapter 1. Foundations Don’t memorize these formulas. If you understand the concepts, you can invent your own notation. John Cochrane, Investments Notes 2006 The aim of this chapter is to explain some foundational mental models that are essential for understanding how neural networks work. Specifically, we’ll cover nested mathematical functions and their derivatives. We’ll work our way up from the simplest possible building blocks to show that we can build complicated functions made up of a “chain” of constituent functions and, even when one of these functions is a matrix multiplication that takes in multiple inputs, compute the derivative of the functions’ outputs with respect to their inputs. Understanding how this process works will be essential to understanding neural networks, which we technically won’t begin to cover until Chapter 2. As we’re getting our bearings around these foundational building blocks of neural networks, we’ll systematically describe each concept we introduce from three perspectives: Math, in the form of an equation or equations Code, with as little extra syntax as possible (making Python an ideal choice) A diagram explaining what is going on, of the kind you would draw on a whiteboard during a coding interview As mentioned in the preface, one of the challenges of understanding neural networks is that it requires multiple mental models. We’ll get a sense of that in this chapter: each of these three perspectives excludes certain essential features of the concepts we’ll cover, and only when taken together do they provide a full picture of both how and why nested mathematical functions work the way they do. In fact, I take the uniquely strong view that any attempt to explain the building blocks of neural networks that excludes one of these three perspectives is incomplete. With that out of the way, it’s time to take our first steps. We’re going to start with some extremely simple building blocks to illustrate how we can understand different concepts in terms of these three perspectives. Our first building block will be a simple but critical concept: the function. Functions What is a function, and how do we describe it? As with neural nets, there are several ways to describe functions, none of which individually paints a complete picture. Rather than trying to give a pithy onesentence description, let’s simply walk through the three mental models one by one, playing the role of the blind men feeling different parts of the elephant. Math Here are two examples of functions, described in mathematical notation: f1(x) = x2 f2(x) = max(x, 0) This notation says that the functions, which we arbitrarily call f1 and f2, take in a number x as input and transform it into either x2 (in the first case) or max(x, 0) (in the second case). Diagrams One way of depicting functions is to: Draw an xy plane (where x refers to the horizontal axis and y refers to the vertical axis). Plot a bunch of points, where the xcoordinates of the points are (usually evenly spaced) inputs of the function over some range, and the ycoordinates are the outputs of the function over that range. Connect these plotted points. This was first done by the French philosopher René Descartes, and it is extremely useful in many areas of mathematics, in particular calculus. Figure 11 shows the plot of these two functions. Figure 11. Two continuous, mostly differentiable functions However, there is another way to depict functions that isn’t as useful when learning calculus but that will be very useful for us when thinking about deep learning models. We can think of functions as boxes that take in numbers as input and produce numbers as output, like minifactories that have their own internal rules for what happens to the input. Figure 12 shows both these functions described as general rules and how they operate on specific inputs. Figure 12. Another way of looking at these functions Code Finally, we can describe these functions using code. Before we do, we should say a bit about the Python library on top of which we’ll be writing our functions: NumPy. Code caveat #1: NumPy NumPy is a widely used Python library for fast numeric computation, the internals of which are mostly written in C. Simply put: the data we deal with in neural networks will always be held in a multidimensional array that is almost always either one, two, three, or fourdimensional, but especially two or threedimensional. The ndarray class from the NumPy library allows us to operate on these arrays in ways that are both (a) intuitive and (b) fast. To take the simplest possible example: if we were storing our data in Python lists (or lists of lists), adding or multiplying the lists elementwise using normal syntax wouldn’t work, whereas it does work for ndarrays: print("Python list operations:") a = [1,2,3] b = [4,5,6] print("a+b:", a+b) try: print(a*b) except TypeError: print("a*b has no meaning for Python lists") print() print("numpy array operations:") a = np.array([1,2,3]) b = np.array([4,5,6]) print("a+b:", a+b) print("a*b:", a*b) Python list operations: a+b: [1, 2, 3, 4, 5, 6] a*b has no meaning for Python lists numpy array operations: a+b: [5 7 9] a*b: [ 4 10 18] ndarrays also have several features you’d expect from an ndimensional array; each ndarray has n axes, indexed from 0, so that the first axis is 0, the second is 1, and so on. In particular, since we deal with 2D ndarrays often, we can think of axis = 0 as the rows and axis = 1 as the columns—see Figure 13. Figure 13. A 2D NumPy array, with axis = 0 as the rows and axis = 1 as the columns NumPy’s ndarrays also support applying functions along these axes in intuitive ways. For example, summing along axis 0 (the rows for a 2D array) essentially “collapses the array” along that axis, returning an array with one less dimension than the original array; for a 2D array, this is equivalent to summing each column: print('a:') print(a) print('a.sum(axis=0):', a.sum(axis=0)) print('a.sum(axis=1):', a.sum(axis=1)) a: [[1 2] [3 4]] a.sum(axis=0): [4 6] a.sum(axis=1): [3 7] Finally, NumPy ndarrays support adding a 1D array to the last axis; for a 2D array a with R rows and C columns, this means we can add a 1D array b of length C and NumPy will do the addition in the intuitive way, adding the elements to each row of a:1 a = np.array([[1,2,3], [4,5,6]]) b = np.array([10,20,30]) print("a+b:\n", a+b) a+b: [[11 22 33] [14 25 36]] Code caveat #2: Typechecked functions As I’ve mentioned, the primary goal of the code we write in this book is to make the concepts I’m explaining precise and clear. This will get more challenging as the book goes on, as we’ll be writing functions with many arguments as part of complicated classes. To combat this, we’ll use functions with type signatures throughout; for example, in Chapter 3, we’ll initialize our neural networks as follows: def __init__(self, layers: List[Layer], loss: Loss, learning_rate: float = 0.01) > None: This type signature alone gives you some idea of what the class is used for. By contrast, consider the following type signature that we could use to define an operation: def operation(x1, x2): This type signature by itself gives you no hint as to what is going on; only by printing out each object’s type, seeing what operations get performed on each object, or guessing based on the names x1 and x2 could we understand what is going on in this function. I can instead define a function with a type signature as follows: def operation(x1: ndarray, x2: ndarray) > ndarray: You know right away that this is a function that takes in two ndarrays, probably combines them in some way, and outputs the result of that combination. Because of the increased clarity they provide, we’ll use typechecked functions throughout this book. Basic functions in NumPy With these preliminaries in mind, let’s write up the functions we defined earlier in NumPy: def square(x: ndarray) > ndarray: ''' Square each element in the input ndarray. ''' return np.power(x, 2) def leaky_relu(x: ndarray) > ndarray: ''' Apply "Leaky ReLU" function to each element in ndarray. ''' return np.maximum(0.2 * x, x) Note One of NumPy’s quirks is that many functions can be applied to ndarrays either by writing np.function_name(ndarray) or by writing ndarray.function_name. For example, the preceding relu function could be written as: x.clip(min=0). We’ll try to be consistent and use the np.function_name(ndarray) convention throughout—in particular, we’ll avoid tricks such as ndarray.T for transposing a twodimensional ndarray, instead writing np.transpose(ndarray, (1, 0)). If you can wrap your mind around the fact that math, a diagram, and code are three different ways of representing the same underlying concept, then you are well on your way to displaying the kind of flexible thinking you’ll need to truly understand deep learning. Derivatives Derivatives, like functions, are an extremely important concept for understanding deep learning that many of you are probably familiar with. Also like functions, they can be depicted in multiple ways. We’ll start by simply saying at a high level that the derivative of a function at a point is the “rate of change” of the output of the function with respect to its input at that point. Let’s now walk through the same three perspectives on derivatives that we covered for functions to gain a better mental model for how derivatives work. Math First, we’ll get mathematically precise: we can describe this number—how much the output of f changes as we change its input at a particular value a of the input—as a limit: This limit can be approximated numerically by setting a very small value for Δ, such as 0.001, so we can compute the derivative as: While accurate, this is only one part of a full mental model of derivatives. Let’s look at them from another perspective: a diagram. Diagrams First, the familiar way: if we simply draw a tangent line to the Cartesian representation of the function f, the derivative of f at a point a is just the slope of this line at a. As with the mathematical descriptions in the prior subsection, there are two ways we can actually calculate the slope of this line. The first would be to use calculus to actually calculate the limit. The second would be to just take the slope of the line connecting f at a – 0.001 and a + 0.001. The latter method is depicted in Figure 14 and should be familiar to anyone who has taken calculus. Figure 14. Derivatives as slopes As we saw in the prior section, another way of thinking of functions is as minifactories. Now think of the inputs to those factories being connected to the outputs by a string. The derivative is equal to the answer to this question: if we pull up on the input to the function a by some very small amount—or, to account for the fact that the function may be asymmetric at a, pull down on a by some small amount—by what multiple of this small amount will the output change, given the inner workings of the factory? This is depicted in Figure 15. Figure 15. Another way of visualizing derivatives This second representation will turn out to be more important than the first one for understanding deep learning. Code Finally, we can code up the approximation to the derivative that we saw previously: from typing import Callable def deriv(func: Callable[[ndarray], ndarray], input_: ndarray, delta: float = 0.001) > ndarray: ''' Evaluates the derivative of a function "func" at every element in the "input_" array. ''' return (func(input_ + delta)  func(input_  delta)) / (2 * delta) Note When we say that “something is a function of something else”—for example, that P is a function of E (letters chosen randomly on purpose), what we mean is that there is some function f such that f(E) = P—or equivalently, there is a function f that takes in E objects and produces P objects. We might also think of this as meaning that P is defined as whatever results when we apply the function f to E: And we would code this up as: def f(input_: ndarray) > ndarray: # Some transformation(s) return output P = f(E) Nested Functions Now we’ll cover a concept that will turn out to be fundamental to understanding neural networks: functions can be “nested” to form “composite” functions. What exactly do I mean by “nested”? I mean that if we have two functions that by mathematical convention we call f1 and f2, the output of one of the functions becomes the input to the next one, so that we can “string them together.” Diagram The most natural way to represent a nested function is with the “minifactory” or “box” representation (the second representation from “Functions”). As Figure 16 shows, an input goes into the first function, gets transformed, and comes out; then it goes into the second function and gets transformed again, and we get our final output. Figure 16. Nested functions, naturally Math We should also include the less intuitive mathematical representation: This is less intuitive because of the quirk that nested functions are read “from the outside in” but the operations are in fact performed “from the inside out.” For example, though is read “f 2 of f 1 of x,” what it really means is to “first apply f1 to x, and then apply f2 to the result of applying f1 to x.” Code Finally, in keeping with my promise to explain every concept from three perspectives, we’ll code this up. First, we’ll define a data type for nested functions: from typing import List # A Function takes in an ndarray as an argument and produces an ndarray Array_Function = Callable[[ndarray], ndarray] # A Chain is a list of functions Chain = List[Array_Function] Then we’ll define how data goes through a chain, first of length 2: def chain_length_2(chain: Chain, a: ndarray) > ndarray: ''' Evaluates two functions in a row, in a "Chain". ''' assert len(chain) == 2, \ "Length of input 'chain' should be 2" f1 = chain[0] f2 = chain[1] return f2(f1(x)) Another Diagram Depicting the nested function using the box representation shows us that this composite function is really just a single function. Thus, we can represent this function as simply f1 f2, as shown in Figure 17. Figure 17. Another way to think of nested functions Moreover, a theorem from calculus tells us that a composite function made up of “mostly differentiable” functions is itself mostly differentiable! Thus, we can think of f1f2 as just another function that we can compute derivatives of—and computing derivatives of composite functions will turn out to be essential for training deep learning models. However, we need a formula to be able to compute this composite function’s derivative in terms of the derivatives of its constituent functions. That’s what we’ll cover next. The Chain Rule The chain rule is a mathematical theorem that lets us compute derivatives of composite functions. Deep learning models are, mathematically, composite functions, and reasoning about their derivatives is essential to training them, as we’ll see in the next couple of chapters. Math Mathematically, the theorem states—in a rather nonintuitive form—that, for a given value x, where u is simply a dummy variable representing the input to a function. Note When describing the derivative of a function f with one input and output, we can denote the function that represents the derivative of this function as . We could use a different dummy variable in place of u—it doesn’t matter, just as f(x) = x2 and f(y) = y2 mean the same thing. On the other hand, later on we’ll deal with functions that take in multiple inputs, say, both x and y. Once we get there, it will make sense to write and have it mean something different than . This is why in the preceding formula we denote all the derivatives with a u on the bottom: both f1 and f2 are functions that take in one input and produce one output, and in such cases (of functions with one input and one output) we’ll use u in the derivative notation. Diagram The preceding formula does not give much intuition into the chain rule. For that, the box representation is much more helpful. Let’s reason through what the derivative “should” be in the simple case of f1 f2. Figure 18. An illustration of the chain rule Intuitively, using the diagram in Figure 18, the derivative of the composite function should be a sort of product of the derivatives of its constituent functions. Let’s say we feed the value 5 into the first function, and let’s say further that computing the derivative of the first function at u = 5 gives us a value of 3—that is, . Let’s say that we then take the value of the function that comes out of the first box—let’s suppose it is 1, so that f1(5) = 1—and compute the derivative of the second function f2 at this value: that is, . We find that this value is –2. If we think about these functions as being literally strung together, then if changing the input to box two by 1 unit yields a change of –2 units in the output of box two, changing the input to box two by 3 units should change the output to box two by –2 × 3 = –6 units. This is why in the formula for the chain rule, the final result is ultimately a product: times . So by considering the diagram and the math, we can reason through what the derivative of the output of a nested function with respect to its input ought to be, using the chain rule. What might the code instructions for the computation of this derivative look like? Code Let’s code this up and show that computing derivatives in this way does in fact yield results that “look correct.” We’ll use the square function from “Basic functions in NumPy” along with sigmoid, another function that ends up being important in deep learning: def sigmoid(x: ndarray) > ndarray: ''' Apply the sigmoid function to each element in the input ndarray. ''' return 1 / (1 + np.exp(x)) And now we code up the chain rule: def chain_deriv_2(chain: Chain, input_range: ndarray) > ndarray: ''' Uses the chain rule to compute the derivative of two nested functions: (f2(f1(x))' = f2'(f1(x)) * f1'(x) ''' assert len(chain) == 2, \ "This function requires 'Chain' objects of length 2" assert input_range.ndim == 1, \ "Function requires a 1 dimensional ndarray as input_range" f1 = chain[0] f2 = chain[1] # df1/dx f1_of_x = f1(input_range) # df1/du df1dx = deriv(f1, input_range) # df2/du(f1(x)) df2du = deriv(f2, f1(input_range)) # Multiplying these quantities together at each point return df1dx * df2du Figure 19 plots the results and shows that the chain rule works: PLOT_RANGE = np.arange(3, 3, 0.01) chain_1 = [square, sigmoid] chain_2 = [sigmoid, square] plot_chain(chain_1, PLOT_RANGE) plot_chain_deriv(chain_1, PLOT_RANGE) plot_chain(chain_2, PLOT_RANGE) plot_chain_deriv(chain_2, PLOT_RANGE) Figure 19. The chain rule works, part 1 The chain rule seems to be working. When the functions are upwardsloping, the derivative is positive; when they are flat, the derivative is zero; and when they are downwardsloping, the derivative is negative. So we can in fact compute, both mathematically and via code, the derivatives of nested or “composite” functions such as f1 f2, as long as the individual functions are themselves mostly differentiable. It will turn out that deep learning models are, mathematically, long chains of these mostly differentiable functions; spending time going manually through a slightly longer example in detail will help build your intuition about what is going on and how it can generalize to more complex models. A Slightly Longer Example Let’s closely examine a slightly longer chain: if we have three mostly differentiable functions—f1, f2, and f3—how would we go about computing the derivative of f1 f2 f3? We “should” be able to do it, since from the calculus theorem mentioned previously, we know that the composite of any finite number of “mostly differentiable” functions is differentiable. Math Mathematically, the result turns out to be the following expression: The underlying logic as to why the formula works for chains of length 2, , also applies here—as does the lack of intuition from looking at the formula alone! Diagram The best way to (literally) see why this formula makes sense is via another box diagram, as shown in Figure 110. Figure 110. The “box model” for computing the derivative of three nested functions Using similar reasoning to the prior section: if we imagine the input to f1 f2 f3 (call it a) being connected to the output (call it b) by a string, then changing a by a small amount Δ will result in a change in f1(a) of times Δ, which will result in a change to (the next step along in the chain) of times Δ, and so on for the third step, when we get to the final change equal to the full formula for the preceding chain rule times Δ. Spend a bit of time going through this explanation and the earlier diagram—but not too much time, since we’ll develop even more intuition for this when we code it up. Code How might we translate such a formula into code instructions for computing the derivative, given the constituent functions? Interestingly, already in this simple example we see the beginnings of what will become the forward and backward passes of a neural network: def chain_deriv_3(chain: Chain, input_range: ndarray) > ndarray: ''' Uses the chain rule to compute the derivative of three nested functions: (f3(f2(f1)))' = f3'(f2(f1(x))) * f2'(f1(x)) * f1'(x) ''' assert len(chain) == 3, \ "This function requires 'Chain' objects to have length 3" f1 = chain[0] f2 = chain[1] f3 = chain[2] # f1(x) f1_of_x = f1(input_range) # f2(f1(x)) f2_of_x = f2(f1_of_x) # df3du df3du = deriv(f3, f2_of_x) # df2du df2du = deriv(f2, f1_of_x) # df1dx df1dx = deriv(f1, input_range) # Multiplying these quantities together at each point return df1dx * df2du * df3du Something interesting took place here—to compute the chain rule for this nested function, we made two “passes” over it: First, we went “forward” through it, computing the quantities f1_of_x and f2_of_x along the way. We can call this (and think of it as) “the forward pass.” Then, we “went backward” through the function, using the quantities that we computed on the forward pass to compute the quantities that make up the derivative. Finally, we multiplied three of these quantities together to get our derivative. Now, let’s show that this works, using the three simple functions we’ve defined so far: sigmoid, square, and leaky_relu. PLOT_RANGE = np.range(3, 3, 0.01) plot_chain([leaky_relu, sigmoid, square], PLOT_RANGE) plot_chain_deriv([leaky_relu, sigmoid, square], PLOT_RANGE) Figure 111 shows the result. Figure 111. The chain rule works, even with triply nested functions Again, comparing the plots of the derivatives to the slopes of the original functions, we see that the chain rule is indeed computing the derivatives properly. Let’s now apply our understanding to composite functions with multiple inputs, a class of functions that follows the same principles we already established and is ultimately more applicable to deep learning. Functions with Multiple Inputs By this point, we have a conceptual understanding of how functions can be strung together to form composite functions. We also have a sense of how to represent these functions as series of boxes that inputs go into and outputs come out of. Finally, we’ve walked through how to compute the derivatives of these functions so that we understand these derivatives both mathematically and as quantities computed via a stepbystep process with a “forward” and “backward” component. Oftentimes, the functions we deal with in deep learning don’t have just one input. Instead, they have several inputs that at certain steps are added together, multiplied, or otherwise combined. As we’ll see, computing the derivatives of the outputs of these functions with respect to their inputs is still no problem: let’s consider a very simple scenario with multiple inputs, where two inputs are added together and then fed through another function. Math For this example, it is actually useful to start by looking at the math. If our inputs are x and y, then we could think of the function as occurring in two steps. In Step 1, x and y are fed through a function that adds them together. We’ll denote that function as α (we’ll use Greek letters to refer to function names throughout) and the output of the function as a. Formally, this is simply: Step 2 would be to feed a through some function σ (σ can be any continuous function, such as sigmoid, or the square function, or even a function whose name doesn’t start with s). We’ll denote the output of this function as s: We could, equivalently, denote the entire function as f and write: This is more mathematically concise, but it obscures the fact that this is really two operations happening sequentially. To illustrate that, we need the diagram in the next section. Diagram Now that we’re at the stage where we’re examining functions with multiple inputs, let’s pause to define a concept we’ve been dancing around: the diagrams with circles and arrows connecting them that represent the mathematical “order of operations” can be thought of as computational graphs. For example, Figure 112 shows a computational graph for the function f we just described. Figure 112. Function with multiple inputs Here we see the two inputs going into α and coming out as a and then being fed through σ. Code Coding this up is very straightforward; note, however, that we have to add one extra assertion: def multiple_inputs_add(x: ndarray, y: ndarray, sigma: Array_Function) > float: ''' Function with multiple inputs and addition, forward pass. ''' assert x.shape == y.shape a = x + y return sigma(a) Unlike the functions we saw earlier in this chapter, this function does not simply operate “elementwise” on each element of its input ndarrays. Whenever we deal with an operation that takes multiple ndarrays as inputs, we have to check their shapes to ensure they meet whatever conditions are required by that operation. Here, for a simple operation such as addition, all we need to check is that the shapes are identical so that the addition can happen elementwise. Derivatives of Functions with Multiple Inputs It shouldn’t seem surprising that we can compute the derivative of the output of such a function with respect to both of its inputs. Diagram Conceptually, we simply do the same thing we did in the case of functions with one input: compute the derivative of each constituent function “going backward” through the computational graph and then multiply the results together to get the total derivative. This is shown in Figure 113. Figure 113. Going backward through the computational graph of a function with multiple inputs Math The chain rule applies to these functions in the same way it applied to the functions in the prior sections. Since this is a nested function, with , we have: And of course would be identical. Now note that: since for every unit increase in x, a increases by one unit, no matter the value of x (the same holds for y). Given this, we can code up how we might compute the derivative of such a function. Code def multiple_inputs_add_backward(x: ndarray, y: ndarray, sigma: Array_Function) > float: ''' Computes the derivative of this simple function with respect to both inputs. ''' # Compute "forward pass" a = x + y # Compute derivatives dsda = deriv(sigma, a) dadx, dady = 1, 1 return dsda * dadx, dsda * dady A straightforward exercise for the reader is to modify this for the case where x and y are multiplied instead of added. Next, we’ll examine a more complicated example that more closely mimics what happens in deep learning: a similar function to the previous example, but with two vector inputs. Functions with Multiple Vector Inputs In deep learning, we deal with functions whose inputs are vectors or matrices. Not only can these objects be added, multiplied, and so on, but they can also combined via a dot product or a matrix multiplication. In the rest of this chapter, I’ll show how the mathematics of the chain rule and the logic of computing the derivatives of these functions using a forward and backward pass can still apply. These techniques will end up being central to understanding why deep learning works. In deep learning, our goal will be to fit a model to some data. More precisely, this means that we want to find a mathematical function that maps observations from the data—which will be inputs to the function—to some desired predictions from the data—which will be the outputs of the function—in as optimal a way as possible. It turns out these observations will be encoded in matrices, typically with row as an observation and each column as a numeric feature for that observation. We’ll cover this in more detail in the next chapter; for now, being able to reason about the derivatives of complex functions involving dot products and matrix multiplications will be essential. Let’s start by defining precisely what I mean, mathematically. Math A typical way to represent a single data point, or “observation,” in a neural network is as a row with n features, where each feature is simply a number x1, x2, and so on, up to xn: A canonical example to keep in mind here is predicting housing prices, which we’ll build a neural network from scratch to do in the next chapter; in this example, x1, x2, and so on are numerical features of a house, such as its square footage or its proximity to schools. Creating New Features from Existing Features Perhaps the single most common operation in neural networks is to form a “weighted sum” of these features, where the weighted sum could emphasize certain features and deemphasize others and thus be thought of as a new feature that itself is just a combination of old features. A concise way to express this mathematically is as a dot product of this observation, with some set of “weights” of the same length as the features, w1, w2, and so on, up to wn. Let’s explore this concept from the three perspectives we’ve used thus far in this chapter. Math To be mathematically precise, if: then we could define the output of this operation as: Note that this operation is a special case of a matrix multiplication that just happens to be a dot product because X has one row and W has only one column. Next, let’s look at a few ways we could depict this with a diagram. Diagram A simple way of depicting this operation is shown in Figure 114. Figure 114. Diagram of a vector dot product This diagram depicts an operation that takes in two inputs, both of which can be ndarrays, and produces one output ndarray. But this is really a massive shorthand for many operations that are happening on many inputs. We could instead highlight the individual operations and inputs, as shown in Figures 115 and 116. Figure 115. Another diagram of a matrix multiplication Figure 116. A third diagram of a matrix multiplication The key point is that the dot product (or matrix multiplication) is a concise way to represent many individual operations; in addition, as we’ll start to see in the next section, using this operation makes our derivative calculations on the backward pass extremely concise as well. Code Finally, in code this operation is simply: def matmul_forward(X: ndarray, W: ndarray) > ndarray: ''' Computes the forward pass of a matrix multiplication. ''' assert X.shape[1] == W.shape[0], \ ''' For matrix multiplication, the number of columns in the first array should match the number of rows in the second; instead the number of columns in the first array is {0} and the number of rows in the second array is {1}. '''.format(X.shape[1], W.shape[0]) # matrix multiplication N = np.dot(X, W) return N where we have a new assertion that ensures that the matrix multiplication will work. (This is necessary since this is our first operation that doesn’t merely deal with ndarrays that are the same size and perform an operation elementwise—our output is now actually a different size than our input.) Derivatives of Functions with Multiple Vector Inputs For functions that simply take one input as a number and produce one output, like f(x) = x2 or f(x) = sigmoid(x), computing the derivative is straightforward: we simply apply rules from calculus. For vector functions, it isn’t immediately obvious what the derivative is: if we write a dot product as , as in the prior section, the question naturally arises—what would and be? Diagram Conceptually, we just want to do something like in Figure 117. Figure 117. Backward pass of a matrix multiplication, conceptually Calculating these derivatives was easy when we were just dealing with addition and multiplication, as in the prior examples. But how can we do the analogous thing with matrix multiplication? To define that precisely, we’ll have to turn to the math. Math First, how would we even define “the derivative with respect to a matrix”? Recalling that the matrix syntax is just shorthand for a bunch of numbers arranged in a particular form, “the derivative with respect to a matrix” really means “the derivative with respect to each element of the matrix.” Since X is a row, a natural way to define it is: However, the output of ν is just a number: . And looking at this, we can see that if, for example, changes by ϵ units, then N will change by units—and the same logic applies to the other xi elements. Thus: And so: This is a surprising and elegant result that turns out to be a key piece of the puzzle to understanding both why deep learning works and how it can be implemented so cleanly. Using similar reasoning, we can see that: Code Here, reasoning mathematically about what the answer “should” be was the hard part. The easy part is coding up the result: def matmul_backward_first(X: ndarray, W: ndarray) > ndarray: ''' Computes the backward pass of a matrix multiplication with respect to the first argument. ''' # backward pass dNdX = np.transpose(W, (1, 0)) return dNdX The dNdX quantity computed here represents the partial derivative of each element of X with respect to the sum of the output N. There is a special name for this quantity that we’ll use throughout the book: we’ll call it the gradient of X with respect to X. The idea is that for an individual element of X—say, x3—the corresponding element in dNdx (dNdX[2], to be specific) is the partial derivative of the output of the vector dot product N with respect to x3. The term “gradient” as we’ll use it in this book simply refers to a multidimensional analogue of the partial derivative; specifically, it is an array of partial derivatives of the output of a function with respect to each element of the input to that function. Vector Functions and Their Derivatives: One Step Further Deep learning models, of course, involve more than one operation: they include long chains of operations, some of which are vector functions like the one covered in the last section, and some of which simply apply a function elementwise to the ndarray they receive as input. Therefore, we’ll now look at computing the derivative of a composite function that includes both kinds of functions. Let’s suppose our function takes in the vectors X and W, performs the dot product described in the prior section—which we’ll denote as —and then feeds the vectors through a function σ. We’ll express the same objective as before, but in new language: we want to compute the gradients of the output of this new function with respect to X and W. Again, starting in the next chapter, we’ll see in precise detail how this is connected to what neural networks do, but for now we just want to build up the idea that we can compute gradients for computational graphs of arbitrary complexity. Diagram The diagram for this function, shown in Figure 118, is the same as in Figure 117, with the σ function simply added onto the end. Figure 118. Same graph as before, but with another function tacked onto the end Math Mathematically, this is straightforward as well: Code Finally, we can code this function up as: def matrix_forward_extra(X: ndarray, W: ndarray, sigma: Array_Function) > ndarray: ''' Computes the forward pass of a function involving matrix multiplication, one extra function. ''' assert X.shape[1] == W.shape[0] # matrix multiplication N = np.dot(X, W) # feeding the output of the matrix multiplication through sigma S = sigma(N) return S Vector Functions and Their Derivatives: The Backward Pass The backward pass is similarly just a straightforward extension of the prior example. Math Since f(X, W) is a nested function—specifically, f(X, W) = σ(ν(X, W))—its derivative with respect to, for example, X should conceptually be: But the first part of this is simply: which is well defined since σ is just a continuous function whose derivative we can evaluate at any point, and here we are just evaluating it at . Furthermore, we reasoned in the prior example that . Therefore: which, as in the preceding example, results in a vector of the same shape as X, since the final answer is a number, , times a vector of the same shape as X in WT. Diagram The diagram for the backward pass of this function, shown in Figure 119, is similar to that of the prior example and even higher level than the math; we just have to add one more multiplication based on the derivative of the σ function evaluated at the result of the matrix multiplication. Figure 119. Graph with a matrix multiplication: the backward pass Code Finally, coding up the backward pass is straightforward as well: def matrix_function_backward_1(X: ndarray, W: ndarray, sigma: Array_Function) > ndarray: ''' Computes the derivative of our matrix function with respect to the first element. ''' assert X.shape[1] == W.shape[0] # matrix multiplication N = np.dot(X, W) # feeding the output of the matrix multiplication through sigma S = sigma(N) # backward calculation dSdN = deriv(sigma, N) # dNdX dNdX = np.transpose(W, (1, 0)) # multiply them together; since dNdX is 1x1 here, order doesn't matter return np.dot(dSdN, dNdX) Notice that we see the same dynamic here that we saw in the earlier example with the three nested functions: we compute quantities on the forward pass (here, just N) that we then use during the backward pass. Is this right? How can we tell if these derivatives we’re computing are correct? A simple test is to perturb the input a little bit and observe the resulting change in output. For example, X in this case is: print(X) [[ 0.4723 0.6151 1.7262]] If we increase x3 by 0.01, from 1.726 to 1.716, we should see an increase in the value produced by the forward function of the gradient of the output with respect to x3 × 0.01. Figure 120 shows this. Figure 120. Gradient checking: an illustration Using the matrix_function_backward_1 function, we can see that the gradient is 0.1121: print(matrix_function_backward_1(X, W, sigmoid)) [[ 0.0852 0.0557 0.1121]] To test whether this gradient is correct, we should see, after incrementing x3 by 0.01, a corresponding decrease in the output of the function by about 0.01 × 0.1121 = 0.001121; if we saw an decrease by more or less than this amount, or an increase, for example, we would know that our reasoning about the chain rule was off. What we see when we do this calculation,2 however, is that increasing x3 by a small amount does indeed decrease the value of the output of the function by 0.01 × 0.1121—which means the derivatives we’re computing are correct! To close out this chapter, we’ll cover an example that builds on everything we’ve done so far and directly applies to the models we’ll build in the next chapter: a computational graph that starts by multiplying a pair of twodimensional matrices together. Computational Graph with Two 2D Matrix Inputs In deep learning, and in machine learning more generally, we deal with operations that take as input two 2D arrays, one of which represents a batch of data X and the other of which represents the weights W. In the next chapter, we’ll dive deep into why this makes sense in a modeling context, but in this chapter we’ll just focus on the mechanics and the math behind this operation. Specifically. we’ll walk through a simple example in detail and show that even when multiplications of 2D matrices are involved, rather than just dot products of 1D vectors, the reasoning we’ve been using throughout this chapter still makes mathematical sense and is in fact extremely easy to code. As before, the math needed to derive these results gets…not difficult, but messy. Nevertheless, the result is quite clean. And, of course, we’ll break it down step by step and always connect it back to both code and diagrams. Math Let’s suppose that: and: This could correspond to a dataset in which each observation has three features, and the three rows could correspond to three different observations for which we want to make predictions. Now we’ll define the following straightforward operations to these matrices: Multiply these matrices together. As before, we’ll denote the function that does this as ν(X, W) and the output as N, so that N = ν(X, W). Feed result through some differentiable function σ, and define (S = σ(N). As before, the question now is: what are the gradients of the output S with respect to X and W? Can we simply use the chain rule again? Why or why not? If you think about this for a bit, you may realize that something is different from the previous examples that we’ve looked at: S is now a matrix, not simply a number. And what, after all, does the gradient of one matrix with respect to another matrix mean? This leads us to a subtle but important idea: we may perform whatever series of operations on multidimensional arrays we want, but for the notion of a “gradient” with respect to some output to be well defined, we need to sum (or otherwise aggregate into a single number) the final array in the sequence so that the notion of “how much will changing each element of X affect the output” will even make sense. So we’ll tack onto the end a third function, Lambda, that simply takes the elements of S and sums them up. Let’s make this mathematically concrete. First, let’s multiply X and W: where we denote row i and column j in the resulting matrix as for convenience. Next, we’ll feed this result through σ, which just means applying σ to every element of the matrix : Finally, we can simply sum up these elements: Now we are back in a pure calculus setting: we have a number, L, and we want to figure out the gradient of L with respect to X and W; that is, we want to know how much changing each element of these input matrices (x11, w21, and so on) would change L. We can write this as: And now we understand mathematically the problem we are up against. Let’s pause the math for a second and catch up with our diagram and code. Diagram Conceptually, what we are doing here is similar to what we’ve done in the previous examples with a computational graph with multiple inputs; thus, Figure 121 should look familiar. Figure 121. Graph of a function with a complicated forward pass We are simply sending inputs forward as before. We claim that even in this more complicated scenario, we should be able to calculate the gradients we need using the chain rule. Code We can code this up as: def matrix_function_forward_sum(X: ndarray, W: ndarray, sigma: Array_Function) > float: ''' Computing the result of the forward pass of this function with input ndarrays X and W and function sigma. ''' assert X.shape[1] == W.shape[0] # matrix multiplication N = np.dot(X, W) # feeding the output of the matrix multiplication through sigma S = sigma(N) # sum all the elements L = np.sum(S) return L The Fun Part: The Backward Pass Now we want to “perform the backward pass” for this function, showing how, even when a matrix multiplication is involved, we can end up calculating the gradient of N with respect to each of the elements of our input ndarrays.3 With this final step figured out, starting to train real machine learning models in Chapter 2 will be straightforward. First, let’s remind ourselves what we are doing, conceptually. Diagram Again, what we’re doing is similar to what we’ve done in the prior examples from this chapter; Figure 122 should look as familiar as Figure 121 did. Figure 122. Backward pass through our complicated function We simply need to calculate the partial derivative of each constituent function and evaluate it at its input, multiplying the results together to get the final derivative. Let’s consider each of these partial derivatives in turn; the only way through it is through the math. Math Let’s first note that we could compute this directly. The value L is indeed a function of x11, x12, and so on, all the way up to x33. However, that seems complicated. Wasn’t the whole point of the chain rule that we can break down the derivatives of complicated functions into simple pieces, compute each of those pieces, and then just multiply the results? Indeed, that fact was what made it so easy to code these things up: we just went step by step through the forward pass, saving the results as we went, and then we used those results to evaluate all the necessary derivatives for the backward pass. I’ll show that this approach only kind of works when there are matrices involved. Let’s dive in. We can write L as . If this were a regular function, we would just write the chain rule: Then we would compute each of the three partial derivatives in turn. This is exactly what we did before in the function of three nested functions, for which we computed the derivative using the chain rule, and Figure 122 suggests that approach should work for this function as well. The first derivative is the most straightforward and thus makes the best warmup. We want to know how much L (the output of Λ) will increase if each element of S increases. Since L is the sum of all the elements of S, this derivative is simply: since increasing any element of S by, say, 0.46 units would increase Λ by 0.46 units. Next, we have . This is simply the derivative of whatever function σ is, evaluated at the elements in N. In the "XW" syntax we’ve used previously, this is again simple to compute: Note that at this point we can say for certain that we can multiply these two derivatives together elementwise and compute : Now, however, we are stuck. The next thing we want, based on the diagram and applying the chain rule, is . Recall, however, that N, the output of ν, was just the result of a matrix multiplication of X with W. Thus we want some notion of how much increasing each element of X (a 3 × 3 matrix) will increase each element of N (a 3 × 2 matrix). If you’re having trouble wrapping your mind around such a notion, that’s the point—it isn’t clear at all how we’d define this, or whether it would even be useful if we did. Why is this a problem now? Before, we were in the fortunate situation of X and W being transposes of each other in terms of shape. That being the case, we could show that and . Is there something analogous we can say here? The “?” More specifically, here’s where we’re stuck. We need to figure out what goes in the “?”: The answer It turns out that because of the way the multiplication works out, what fills the “?” is simply WT, as in the simpler example with the vector dot product that we just saw! The way to verify this is to compute the partial derivative of L with respect to each element of X directly; when we do so,4 the resulting matrix does indeed (remarkably) factor out into: where the first multiplication is elementwise, and the second one is a matrix multiplication. This means that even if the operations in our computational graph involve multiplying matrices with multiple rows and columns, and even if the shapes of the outputs of those operations are different than those of the inputs, we can still include these operations in our computational graph and backpropagate through them using “chain rule” logic. This is a critical result, without which training deep learning models would be much more cumbersome, as you’ll appreciate further after the next chapter. Code Let’s encapsulate what we just derived using code, and hopefully solidify our understanding in the process: def matrix_function_backward_sum_1(X: ndarray, W: ndarray, sigma: Array_Function) > ndarray: ''' Compute derivative of matrix function with a sum with respect to the first matrix input. ''' assert X.shape[1] == W.shape[0] # matrix multiplication N = np.dot(X, W) # feeding the output of the matrix multiplication through sigma S = sigma(N) # sum all the elements L = np.sum(S) # note: I'll refer to the derivatives by their quantities here, # unlike the math, where we referred to their function names # dLdS  just 1s dLdS = np.ones_like(S) # dSdN dSdN = deriv(sigma, N) # dLdN dLdN = dLdS * dSdN # dNdX dNdX = np.transpose(W, (1, 0)) # dLdX dLdX = np.dot(dSdN, dNdX) return dLdX Now let’s verify that everything worked: np.random.seed(190204) X = np.random.randn(3, 3) W = np.random.randn(3, 2) print("X:") print(X) print("L:") print(round(matrix_function_forward_sum(X, W, sigmoid), 4)) print() print("dLdX:") print(matrix_function_backward_sum_1(X, W , sigmoid)) X: [[1.5775 0.6664 0.6391] [0.5615 0.7373 1.4231] [1.4435 0.3913 0.1539]] L: 2.3755 dLdX: [[ 0.2489 0.3748 0.0112] [ 0.126 0.2781 0.1395] [ 0.2299 0.3662 0.0225]] As in the previous example, since dLdX represents the gradient of X with respect to L, this means that, for instance, the topleft element indicates that . Thus, if the matrix math for this example was correct, then increasing x11 by 0.001 should increase L by 0.01 × 0.2489. Indeed, we see that this is what happens: X1 = X.copy() X1[0, 0] += 0.001 print(round( (matrix_function_forward_sum(X1, W, sigmoid)  \ matrix_function_forward_sum(X, W, sigmoid)) / 0.001, 4)) 0.2489 Looks like the gradients were computed correctly! Describing these gradients visually To bring this back to what we noted at the beginning of the chapter, we fed the element in question, x11, through a function with many operations: there was a matrix multiplication—which was really shorthand for combining the nine inputs in the matrix X with the six inputs in the matrix W to create six outputs—the sigmoid function, and then the sum. Nevertheless, we can also think of this as a single function called, say, ", “as depicted in Figure 123. Figure 123. Another way of describing the nested function: as one function, “WNSL” Since each function is differentiable, the whole thing is just a single differentiable function, with x11 as an input; thus, the gradient is simply the answer to the question, what is ? To visualize this, we can simply plot how L changes as x11 changes. Looking at the initial value of x11, we see that it is 1.5775: print("X:") print(X) X: [[1.5775 0.6664 0.6391] [0.5615 0.7373 1.4231] [1.4435 0.3913 0.1539]] If we plot the value of L that results from feeding X and W into the computational graph defined previously—or, to represent it differently, from feeding X and W into the function called in the preceding code—changing nothing except the value for x11 (or X[0, 0]), the resulting plot looks like Figure 124.5 Figure 124. L versus x11, holding other values of X and W constant Indeed, eyeballing this relationship in the case of x11, it looks like the distance this function increases along the Laxis is roughly 0.5 (from just over 2.1 to just over 2.6), and we know that we are showing a change of 2 along the x11axis, which would make the slope roughly —which is exactly what we just calculated! So our complicated matrix math does in fact seem to have resulted in us correctly computing the partial derivative L with respect to each element of X. Furthermore, the gradient of L with respect to W could be computed similarly. Note The expression for the gradient of L with respect to W would be XT. However, because of the order in which the XT expression factors out of the derivative for L, XT would be on the left side of the expression for the gradient of L with respect to W: In code, therefore, while we would have dNdW = np.transpose(X, (1, 0)), the next step would be: dLdW = np.dot(dNdW, dSdN) instead of dLdX = np.dot(dSdN, dNdX) as before. Conclusion After this chapter, you should have confidence that you can understand complicated nested mathematical functions and reason out how they work by conceptualizing them as a series of boxes, each one representing a single constituent function, connected by strings. Specifically, you can write code to compute the derivatives of the outputs of such functions with respect to any of the inputs, even when there are matrix multiplications involving twodimensional ndarrays involved, and understand the math behind why these derivative computations are correct. These foundational concepts are exactly what we’ll need to start building and training neural networks in the next chapter, and to build and train deep learning models from scratch in the chapters after that. Onward! 1 This will allow us to easily add a bias to our matrix multiplication later on. 2 Throughout I’ll provide links to relevant supplementary material on a GitHub repo that contains the code for the book, including for this chapter. 3 In the following section we’ll focus on computing the gradient of N with respect to X, but the gradient with respect to W could be reasoned through similarly. 4 We do this in “Matrix Chain Rule”. 5 The full function can be found on the book’s website; it is simply a subset of the matrix function backward sum function shown on the previous page. Chapter 2. Fundamentals In Chapter 1, I described the major conceptual building block for understanding deep learning: nested, continuous, differentiable functions. I showed how to represent these functions as computational graphs, with each node in a graph representing a single, simple function. In particular, I demonstrated that such a representation showed easily how to calculate the derivative of the output of the nested function with respect to its input: we simply take the derivatives of all the constituent functions, evaluate these derivatives at the input that these functions received, and then multiply all of the results together; this will result in a correct derivative for the nested function because of the chain rule. I illustrated that this does in fact work with some simple examples, with functions that took NumPy’s ndarrays as inputs and produced ndarrays as outputs. I showed that this method of computing derivatives works even when the function takes in multiple ndarrays as inputs and combines them via a matrix multiplication operation, which, unlike the other operations we saw, changes the shape of its inputs. Specifically, if one input to this operation—call the input X—is a B × N ndarray, and another input to this operation, W, is an N × M ndarray, then its output P is a B × M ndarray. While it isn’t clear what the derivative of such an operation would be, I showed that when a matrix multiplication ν(X, W) is included as a “constituent operation” in a nested function, we can still use a simple expression in place of its derivative to compute the derivatives of its inputs: specifically, the role of can be filled by XT, and the role of can be played by WT. In this chapter, we’ll start translating these concepts into realworld applications, Specifically, we will: Express linear regression in terms of these building blocks Show that the reasoning around derivatives that we did in Chapter 1 allows us to train this linear regression model Extend this model (still using our building blocks) to a onelayer neural network Then, in Chapter 3, it will be straightforward to use these same building blocks to build deep learning models. Before we dive into all this, though, let’s give an overview of supervised learning, the subset of machine learning that we’ll focus on as we see how to use neural networks to solve problems. Supervised Learning Overview At a high level, machine learning can be described as building algorithms that can uncover or “learn” relationships in data; supervised learning can be described as the subset of machine learning dedicated to finding relationships between characteristics of the data that have already been measured.1 In this chapter, we’ll deal with a typical supervised learning problem that you might encounter in the real world: finding the relationship between characteristics of a house and the value of the house. Clearly, there is some relationship between characteristics such as the number of rooms, the square footage, or the proximity to schools and how desirable a house is to live in or own. At a high level, the aim of supervised learning is to uncover these relationships, given that we’ve already measured these characteristics. By “measure,” I mean that each characteristic has been defined precisely and represented as a number. Many characteristics of a house, such as the number of bedrooms, the square footage, and so on, naturally lend themselves to being represented as numbers, but if we had other, different kinds of information, such as natural language descriptions of the house’s neighborhood from TripAdvisor, this part of the problem would be much less straightforward, and doing the translation of this lessstructured data into numbers in a reasonable way could make or break our ability to uncover relationships. In addition, for any concept that is ambiguously defined, such as the value of a house, we simply have to pick a single number to describe it; here, an obvious choice is to use the price of the house.2 Once we’ve translated our “characteristics” into numbers, we have to decide what structure to use to represent these numbers. One that is nearly universal across machine learning and turns out to make computations easy is to represent each set of numbers for a single observation—for example, a single house—as a row of data, and then stack these rows on top of each other to form “batches” of data that will get fed into our models as twodimensional ndarrays. Our models will then return predictions as output ndarrays with each prediction in a row, similarly stacked on top of each other, with one prediction for each observation in the batch. Now for some definitions: we say that the length of each row in this ndarray is the number of features of our data. In general, a single characteristic can map to many features, a classic example being a characteristic that describes our data as belonging to one of several categories, such as being a red brick house, a tan brick house, or a slate house;3 in this specific case we might describe this single characteristic with three features. The process of mapping what we informally think of as characteristics of our observations into features is called feature engineering. I won’t spend much time discussing this process in this book; indeed, in this chapter we’ll deal with a problem in which we have 13 characteristics of each observation, and we simply represent each characteristic with a single numeric feature. I said that the goal of supervised learning is ultimately to uncover relationships between characteristics of data. In practice, we do this by choosing one characteristic that we want to predict from the others; we call this characteristic our target. The choice of which characteristic to use as the target is completely arbitrary and depends on the problem you are trying to solve. For example, if your goal is just to describe the relationship between the prices of houses and the number of rooms they have, you could do this by training a model with the prices of houses as the target and the number of rooms as a feature, or vice versa; either way, the resulting model will indeed contain a description of the relationship between these two characteristics, allowing you to say, for example, a higher number of rooms in a house is associated with higher prices. On the other hand, if your goal is to predict the prices of houses for which no price information is available, you have to choose the price as your target, so that you can ultimately feed the other information into your model once it is trained. Figure 21 shows this hierarchy of descriptions of supervised learning, from the highestlevel description of finding relationships in data, to the lowest level of quantifying those relationships by training models to uncover numerical representations between the features and the target. Figure 21. Supervised learning overview As mentioned, we’ll spend almost all our time on the level highlighted at the bottom of Figure 21; nevertheless, in many problems, getting the parts at the top correct—collecting the right data, defining the problem you are trying to solve, and doing feature engineering—is much harder than the actual modeling. Still, since this book is focused on modeling—specifically, on understanding how deep learning models work—let’s return to that subject. Supervised Learning Models Now we know at a high level what supervised learning models are trying to do—and as I alluded to earlier in the chapter, such models are just nested, mathematical functions. We spent the last chapter seeing how to represent such functions in terms of diagrams, math, and code, so now I can state the goal of supervised learning more precisely in terms of both math and code (I’ll show plenty of diagrams later): the goal is to find (a mathematical function) / (a function that takes an ndarray as input and produces an ndarray as output) that can (map characteristics of observations to the target) / (given an input ndarray containing the features we created, produce an output ndarray whose values are “close to” the ndarray containing the target). Specifically, our data will be represented in a matrix X with n rows, each of which represents an observation with k features, all of which are numbers. Each row observation will be a vector, as in , and these observations will be stacked on top of one another to form a batch. For example, a batch of size 3 would look like: For each batch of observations, we will have a corresponding batch of targets, each element of which is the target number for the corresponding observation. We can represent these in a onedimensional vector: In terms of these arrays, our goal with supervised learning will be to use the tools I described in the last chapter to build a function that can take as input batches of observations with the structure of Xbatch and produce vectors of values pi—which we’ll interpret as “predictions”—that (for data in our particular dataset X, at least) are “close to the target values” yi for some reasonable measure of closeness. Finally, we are ready to make all of this concrete and start building our first model for a realworld dataset. We’ll start with a straightforward model—linear regression—and show how to express it in terms of the building blocks from the prior chapter. Linear Regression Linear regression is often shown as: This representation describes mathematically our belief that the numeric value of each target is a linear combination of the k features of X, plus the β0 term to adjust the “baseline” value of the prediction (specifically, the prediction that will be made when the value of all of the features is 0). This, of course, doesn’t give us much insight into how we would code this up so that we could “train” such a model. To do that, we have to translate this model into the language of the functions we saw in Chapter 1; the best place to start is with a diagram. Linear Regression: A Diagram How can we represent linear regression as a computational graph? We could break it down all the way to the individual elements, with each xi being multiplied by another element wi and then the results being added together, as in Figure 22. Figure 22. The operations of a linear regression shown at the level of individual multiplications and additions But again, as we saw in Chapter 1, if we can represent these operations as just a matrix multiplication, we’ll be able to write the function more concisely while still being able to correctly calculate the derivative of the output with respect to the input, which will allow us to train the model. How can we do this? First, let’s handle the simpler scenario in which we don’t have an intercept term (β0 shown previously). Note that we can represent the output of a linear regression model as the dot product of each observation vector with another vector of parameters that we’ll call W: Our prediction would then simply be: So, we can represent “generating the predictions” for a linear regression using a single operation: the dot product. Furthermore, when we want to make predictions using linear regression with a batch of observations, we can use another, single operation: the matrix multiplication. If we have a batch of size 3, for example: then performing the matrix multiplication of this batch Xbatch with W gives a vector of predictions for the batch, as desired: So generating predictions for a batch of observations in a linear regression can be done with a matrix multiplication. Next, I’ll show how to use this fact, along with the reasoning about derivatives from the prior chapter, to train this model. “Training” this model What does it mean to “train” a model? At a high level, models4 take in data, combine them with parameters in some way, and produce predictions. For example, the linear regression model shown earlier takes in data X and parameters W and produces the predictions pbatch using a matrix multiplication: To train our model, however, we need another crucial piece of information: whether or not these predictions are good. To learn this, we bring in the vector of targets ybatch associated with the batch of observations Xbatch fed into the function, and we compute a single number that is a function of ybatch and pbatch and that represents the model’s “penalty” for making the predictions that it did. A reasonable choice is mean squared error, which is simply the average squared value that our model’s predictions “missed” by: Getting to this number, which we can call L, is key: once we have it, we can use all the techniques we saw in Chapter 1 to compute the gradient of this number with respect to each element of W. Then we can use these derivatives to update each element of W in the direction that would cause L to decrease. Repeating this procedure many times, we hope, will “train” our model; in this chapter, we’ll see that this can indeed work in practice. To see clearly how to compute these gradients, we’ll complete the process of representing linear regression as a computational graph. Linear Regression: A More Helpful Diagram (and the Math) Figure 23 shows how to represent linear regression in terms of the diagrams from the last chapter. Figure 23. The linear regression equations expressed as a computational graph—the dark blue letters are the data inputs to the function, and the light blue W denotes the weights Finally, to reinforce that we’re still representing a nested mathematical function with this diagram, we could represent the loss value L that we ultimately compute as: Adding in the Intercept Representing models as diagrams shows us conceptually how we can add an intercept to the model. We simply add an extra step at the end that involves adding a “bias,” as shown in Figure 24. Figure 24. The computational graph of linear regression, with the addition of a bias term at the end Here, though, we should reason mathematically about what is going on before moving on to the code; with the bias added, each element of our model’s prediction pi will be the dot product described earlier with the quantity b added to it: Note that because the intercept in linear regression should be just a single number rather than being different for each observation, the same number should get added to each observation of the input to the bias operation that is passed in; we’ll discuss what this means for computing the derivatives in a later section of this chapter. Linear Regression: The Code We’ll now tie things together and code up the function that makes predictions and computes losses given batches of observations Xbatch and their corresponding targets ybatch. Recall that computing derivatives for nested functions using the chain rule involves two sets of steps: first, we perform a “forward pass,” passing the input successively forward through a series of operations and saving the quantities computed as we go; then we use those quantities to compute the appropriate derivatives during the backward pass. The following code does this, saving the quantities computed on the forward pass in a dictionary; furthermore, to differentiate between the quantities computed on the forward pass and the parameters themselves (which we’ll also need for the backward pass), our function will expect to receive a dictionary containing the parameters: def forward_linear_regression(X_batch: ndarray, y_batch: ndarray, weights: Dict[str, ndarray]) > Tuple[float, Dict[str, ndarray]]: ''' Forward pass for the stepbystep linear regression. ''' # assert batch sizes of X and y are equal assert X_batch.shape[0] == y_batch.shape[0] # assert that matrix multiplication can work assert X_batch.shape[1] == weights['W'].shape[0] # assert that B is simply a 1x1 ndarray assert weights['B'].shape[0] == weights['B'].shape[1] == 1 # compute the operations on the forward pass N = np.dot(X_batch, weights['W']) P = N + weights['B'] loss = np.mean(np.power(y_batch  P, 2)) # save the information computed on the forward pass forward_info: Dict[str, ndarray] = {} forward_info['X'] = X_batch forward_info['N'] = N forward_info['P'] = P forward_info['y'] = y_batch return loss, forward_info Now we have all the pieces in place to start “training” this model. Next, we’ll cover exactly what this means and how we’ll do it. Training the Model We are now going to use all the tools we learned in the last chapter to compute for every wi in W, as well as . How? Well, since the “forward pass” of this function was passing the input through a series of nested functions, the backward pass will simply involve computing the partial derivatives of each function, evaluating those derivatives at the functions’ inputs, and multiplying them together—and even though a matrix multiplication is involved, we’ll be able to handle this using the reasoning we covered in the last chapter. Calculating the Gradients: A Diagram Conceptually, we want something like what is depicted in Figure 25. Figure 25. The backward pass through the linear regression computational graph We simply step backward, computing the derivative of each constituent function and evaluating those derivatives at the inputs that those functions received on the forward pass, and then multiplying these derivatives together at the end. This is straightforward enough, so let’s get into the details. Calculating the Gradients: The Math (and Some Code) From Figure 25, we can see that the derivative product that we ultimately want to compute is: There are three components here; let’s compute each of them in turn. First up: . Since for each element in Y and P: We’re jumping ahead of ourselves a bit, but note that coding this up would simply be: dLdP = 2 * (Y  P) Next, we have an expression involving matrices: . But since α is just addition, the same logic that we reasoned through with numbers in the prior chapter applies here: increasing any element of N by one unit will increase by one unit. Thus, is just a matrix of +1+s, of the same shape as N. Coding this expression, therefore, would simply be: dPdN = np.ones_like(N) Finally, we have . As we discussed in detail in the last chapter, when computing derivatives of nested functions where one of the constituent functions is a matrix multiplication, we can act as if: which in code is simply: dNdW = np.transpose(X, (1, 0)) We’ll do the same for the intercept term; since we are just adding it, the partial derivative of the intercept term with respect to the output is simply 1: dPdB = np.ones_like(weights['B']) The last step is to simply multiply these together, making sure we use the correct order for the matrix multiplications involving dNdW and dNdX based on what we reasoned through at the end of the last chapter. Calculating the Gradients: The (Full) Code Recall that our goal is to take everything computed on or inputed into the forward pass—which, from the diagram in Figure 25, will include X, W, N, B, P, and y—and compute and . The following code does that, receiving W and B as inputs in a dictionary called weights and the rest of the quantities in a dictionary called forward_info: def loss_gradients(forward_info: Dict[str, ndarray], weights: Dict[str, ndarray]) > Dict[str, ndarray]: ''' Compute dLdW and dLdB for the stepbystep linear regression model. ''' batch_size = forward_info['X'].shape[0] dLdP = 2 * (forward_info['y']  forward_info['P']) dPdN = np.ones_like(forward_info['N']) dPdB = np.ones_like(weights['B']) dLdN = dLdP * dPdN dNdW = np.transpose(forward_info['X'], (1, 0)) # need to use matrix multiplication here, # with dNdW on the left (see note at the end of last chapter) dLdW = np.dot(dNdW, dLdN) # need to sum along dimension representing the batch size # (see note near the end of this chapter) dLdB = (dLdP * dPdB).sum(axis=0) loss_gradients: Dict[str, ndarray] = {} loss_gradients['W'] = dLdW loss_gradients['B'] = dLdB return loss_gradients As you can see, we simply compute the derivatives with respect to each operation and successively multiply them together, taking care that we do the matrix multiplication in the right order.5 As we’ll see shortly, this actually works—and after the intuition we built up around the chain rule in the last chapter, this shouldn’t be too surprising. Note An implementation detail about those loss gradients: we’re storing them as a dictionary, with the names of the weights as keys and the amounts that increasing the weights affect the losses as values. The weights dictionary is structured the same way. Therefore, we’ll iterate through the weights in our model in the following way: for key in weights.keys(): weights[key] = learning_rate * loss_grads[key] There is nothing special about storing them in this way; if we stored them differently, we would simply iterate through them and refer to them differently. Using These Gradients to Train the Model Now we’ll simply run the following procedure over and over again: Select a batch of data. Run the forward pass of the model. Run the backward pass of the model using the info computed on the forward pass. Use the gradients computed on the backward pass to update the weights. The Jupyter Notebook for this chapter of the book includes a train function that codes this up. It isn’t too interesting; it simply implements the preceding steps and adds a few sensible things such as shuffling the data to ensure that it is fed through in a random order. The key lines, which get repeated inside of a for loop, are these: forward_info, loss = forward_loss(X_batch, y_batch, weights) loss_grads = loss_gradients(forward_info, weights) for key in weights.keys(): # 'weights' and 'loss_grads' have the same keys weights[key] = learning_rate * loss_grads[key] Then we run the train function for a certain number of epochs, or cycles through the entire training dataset, as follows: train_info = train(X_train, y_train, learning_rate = 0.001, batch_size=23, return_weights=True, seed=80718) The train function returns train_info, a Tuple, one element of which is the parameters or weights that represent what the model has learned. Note The terms “parameters” and “weights” are used interchangeably throughout deep learning, so we will use them interchangeably in this book. Assessing Our Model: Training Set Versus Testing Set To understand whether our model uncovered relationships in our data, we have to introduce some terms and ways of thinking from statistics. We think of any dataset received as being a sample from a population. Our goal is always to find a model that uncovers relationships in the population, despite us seeing only a sample. There is always a danger that we build a model that picks up relationships that exist in the sample but not in the population. For example, it might be the case in our sample that yellow slate houses with three bathrooms are relatively inexpensive, and a complicated neural network model we build could pick up on this relationship even though it may not exist in the population. This is a problem known as overfitting. How can we detect whether a model structure we use is likely to have this problem? The solution is to split our sample into a training set and a testing set. We use the training data to train the model (that is, to iteratively update the weights), and then we evaluate the model on the testing set to estimate its performance. The full logic here is that if our model was able to successfully pick up on relationships that generalize from the training set to the rest of the sample (our whole dataset), then it is likely that the same “model structure” will generalize from our sample—which, again, is our entire dataset—to the population, which is what we want. Assessing Our Model: The Code With that understanding, let’s evaluate our model on the testing set. First, we’ll write a function to generate predictions by truncating the forward_pass function we saw previously: def predict(X: ndarray, weights: Dict[str, ndarray]): ''' Generate predictions from the stepbystep linear regression model. ''' N = np.dot(X, weights['W']) return N + weights['B'] Then we simply use the weights returned earlier from the train function and write: preds = predict(X_test, weights) # weights = train_info[0] How good are these predictions? Keep in mind that at this point we haven’t validated our seemingly strange approach of defining models as a series of operations, and training them by iteratively adjusting the parameters involved using the partial derivatives of the loss calculated with respect to the parameters using the chain rule; thus, we should be pleased if this approach works at all. The first thing we can do to see whether our model worked is to make a plot with the model’s predictions on the xaxis and the actual values on the yaxis. If every point fell exactly on the 45degree line, the model would be perfect. Figure 26 shows a plot of our model’s predicted and actual values. Figure 26. Predicted versus actual values for our custom linear regression model Our plot looks pretty good, but let’s quantify how good the model is. There are a couple of common ways to do that: Calculate the mean distance, in absolute value, between our model’s predictions and the actual values, a metric called mean absolute error: def mae(preds: ndarray, actuals: ndarray): ''' Compute mean absolute error. ''' return np.mean(np.abs(preds  actuals)) Calculate the mean squared distance between our model’s predictions and the actual values, a metric known as root mean squared error: def rmse(preds: ndarray, actuals: ndarray): ''' Compute root mean squared error. ''' return np.sqrt(np.mean(np.power(preds  actuals, 2))) The values for this particular model are: Mean absolute error: 3.5643 Root mean squared error: 5.0508 Root mean squared error is a particularly common metric since it is on the same scale as the target. If we divide this number by the mean value of the target, we can get a measure of how far off a prediction is, on average, from its actual value. Since the mean value of y_test is 22.0776, we see that this model’s predictions of house prices are off by 5.0508 / 22.0776 ≅ 22.9% on average. So are these numbers any good? In the Jupyter Notebook containing the code for this chapter, I show that performing a linear regression on this dataset using the most popular Python library for machine learning, SciKit Learn, results in a mean absolute error and root mean squared error of 3.5666 and 5.0482, respectively, which are virtually identical to what we calculated in our “firstprinciplesbased” linear regression previously. This should give you confidence that the approach we’ve been taking so far in this book is in fact a valid approach for reasoning about and training models! Both later in this chapter, and in the next chapter we’ll extend this approach to neural networks and deep learning models. Analyzing the Most Important Feature Before beginning modeling, we scaled each feature of our data to have mean 0 and standard deviation 1; this has computational advantages that we’ll discuss in more detail in Chapter 4. A benefit of doing this that is specific to linear regression is that we can interpret the absolute values of the coefficients as corresponding to the importance of the different features to the model; a larger coefficient means that the feature is more important. Here are the coefficients: np.round(weights['W'].reshape(1), 4) array([1.0084, 0.7097, 0.2731, 0.7161, 2.2163, 2.3737, 0.7156, 2.6609, 2.629 , 1.8113, 2.3347, 0.8541, 4.2003]) The fact that the last coefficient is largest means that the last feature in the dataset is the most important one. In Figure 27, we plot this feature against our target. Figure 27. Most important feature versus target in custom linear regression We see that this feature is indeed strongly correlated with the target: as this feature increases, the value of the target decreases, and vice versa. However, this relationship is not linear. The expected amount that the target changes as the feature changes from –2 to –1 is not the same amount that it changes as the feature changes from 1 to 2. We’ll come back to this later. In Figure 28, we overlay onto this plot the relationship between this feature and the model predictions. We’ll generate this by feeding the following data through our trained model: The values of all features set equal to their mean The values of the most important feature linearly interpolated over 40 steps from –1.5 to 3.5, which is roughly the range of this scaled feature in our data Figure 28. Most important feature versus target and predictions in custom linear regression This figure shows (literally) a limitation of linear regression: despite the fact that there is a visually clear and “modelable” nonlinear relationship between this feature and the target, our model is