Calculus - Math for Machine Learning
Key Takeaways
The video covers the core ideas from calculus that are necessary for machine learning, including linear approximations, derivatives, and gradients, with a focus on vector- and matrix-valued functions, and introduces tools like autograd and Jupyter Notebooks for automatic gradient calculation and interactive learning.
Full Transcript
[Music] just to remind you of our overall structure we're figuring out what math has to do with machine learning and even though all programming involves math at some level machine learning is a particular kind of programming it's programming by optimization rather than telling a computer exactly what we want it to do we tell the computer here's what it means to get better at this task it's very numerical rather than like programming where you just write stuff and so we really need math for this type of programming in order to understand that optimization process so we already talked about the objects that were being optimized the first way that optimization and machine learning intersect when we talked about linear algebra there we talked about arrays which sort of represent our data and our models these are the things that go into our optimization process our parameters we'll see are you often thought of as arrays the things that determine how our model behaves the equivalent of our code effectively and so all of that fell under the aegis of linear algebra now we're going to talk about how we optimize with calculus calculus helps us make tiny changes in order to iteratively slowly improve the behavior of our computer programs with machine learning we still won't cover what it is that we're optimizing so we're really leaving this pretty we're leaving the meat of it for the third session where we talk about probability and statistics because it turns out what we want to do is reduce surprise or reduce uncertainty with our models but today we will focus instead on how we optimize with calculus so let's dive in so the takeaways for today are that calculus is approximation with linear maps and that calculus helps us incrementally optimize the first point indicates a connection to what we talked about in linear algebra which we're going to really chew on today then one point which we will hopefully but not definitely get to is that calculus has been automated calculus can be done automatically in computers nowadays vector calculus is at the center of machine learning so i think most folks will probably have taken a class on calculus with a single variable derivatives of functions with a single variable vector calculus does the same thing but instead it's with vectors with arrays with matrices as the arguments to the functions and so some examples of where vector calculus shows up you may have if you've done linear regression before and you've got an exact solution those exact solutions are derived using calculus so the actual calculation isn't done using calculus but the derivation uses calculus if you've ever calculated eigenvectors and eigenvalues that is typically done with if you want to calculate them iteratively it's done with a method that is justified using calculus and so the explanation for why this works uses calculus then the algorithms of gradient descent and back propagation where the former is a generic term and the latter is a specific term for neural networks these algorithms are used to optimize machine learning algorithms and they are calculus based and so the usual thing you'll hear is that vector calculus combines linear algebra and calculus where linear algebra is algebra for solving equations of vectors and matrices instead of just numbers and calculus is a collection of methods for studying rates of change and areas but we've already seen that the former is not necessarily the best way to think about linear algebra for machine learning the linear algebra is instead the study of functions that can be represented by arrays aka linear maps as we talked about last time this time we'll talk about a different way to think about calculus calculus at least derivative calculus which is our primary concern is the study of methods for approximating functions with linear maps so rather than the function being exactly the same as something linear we're going to approximate the function with something linear and we're going to take a slightly different tac than the usual way of covering it of defining it we're going to use a slightly different definition of the derivative at least the end result is the same but the definition is different and in mathematics the way we write our definitions is really important even if the final object is equivalent and so basically what we're going to do is we're going to define the derivative using linear maps and that's going to make vector calculus a lot easier when it comes time and the three benefits are that we're going to end up with a single style for thinking about gradients aka derivatives of single variable vector and matrix functions we're going to drop indices from all of our calculations so it's not going to be this big thicket of i's and j's and k's as sometimes happens when you do vector calculus and we'll also get a chance to use something called the little o notation instead of limits which has connections to the big o notation from computer science and i think is just a little bit more intuitive and easier to work with so let's talk about how this works for single variable functions these are the functions you would come across in a first calculus sequence what's called the calculus a b and b c in the american schooling system the standard definition of the derivative emphasizes limits it says that the derivative is the limit of a ratio and that ratio is between change in output and change in input we take a function we're trying to find the derivative of that function and what we do is we look at the difference between how it behaves at one point how it behaves at another point and we divide by how far away those points are and then we make that change in input smaller we move the two points we're querying closer together we imagine making that infinitely small the mathematics of limits gives us an answer to what this ratio limits to and that is what we call the derivative and there are some useful things about thinking that way but i want to emphasize a slightly different way of thinking about it which is we're going to instead think in terms of approximation so the derivative is going to show up as a function that helps us approximate other functions we're still thinking about okay what if i imagine input at one point versus input at another point but instead we're saying if the value at another point epsilon away from my query point x is equal to the original point so this is setting our sort of baseline plus some function of x we can be whatever we want times epsilon so now we're multiplying these two things together and then plus something smaller so just for now just think of little o of epsilon is meaning this thing is really small at least it is smaller than epsilon then we call that function f prime the derivative so one way to think about this is that if we can always approximate the behavior of f near x so f at x plus epsilon for various different points of epsilon we can approximate that with a line passing through f of x with slope f prime of x then we call the function that gives us the slope the derivative now this right here is the definition of a line right we have an intercept here we have a slope here and then we have a variable here epsilon is a variable now this is often b for bias m i don't know why that's the slope thing and x so y equals mx plus b is the way that equation is often written this is the equation for a line and it doesn't give us the exact value of f of x plus epsilon that would be too good that would be too easy in general it gives us something that's off by a little bit and the amount it's off by is given by this little o of epsilon term which could be lots of different functions of epsilon it could be epsilon squared it could be e to the minus epsilon it could be anything that's really small when epsilon is really really small so the function we are using to approximate f near a point is linear right f prime of x times epsilon this is even though f prime of x can be anything the way it interacts with epsilon the way it interacts with our variable is multiplicative and so this function we're using to approximate f near the point x is linear and therefore can always be represented by an array right now it's an array with one element so this isn't that interesting right and the way we use the array is we're basically doing a dot product we're taking one number here one number here multiplying them together and adding up the results we get one number you know this maybe doesn't seem that useful of the use of our linear algebra analogy yet but watch this space for the next example i think it's maybe useful to have a picture i have this function this curve here i actually just drew this in google slides so i don't know how to calculate values of this function at other points but i know that this is its value at one point and the question is okay how do i figure out what its value is at another point so i can't query this function at all so one way to figure out what is value at another point is to approximate it and how are we going to approximate it there are lots of different ways that we could approximate a function just tons but the way we're going to approximate it is by using local like around a particular point linear straight line approximations so it won't give us exact answers they'll give us approximations but they'll be pretty good if you look at this gold line here that represents our approximation that actually stays pretty close to the gray line across this region here the approximation's pretty valid across the region where i'm bringing my pointer there we have our point in gray f of x that's where we start it's also the zero point of our approximation when epsilon is zero that's what our guess is f x equals f of x great that's a good sign and then as we move away we use the slope of this line times epsilon to get our new point so then our approximation is the point i'm indicating with my bright red indicator there my little laser pointer and that value is not exactly right the actual answer is this bigger red circle here but the difference between them is actually quite small and that's that purple tiny little purple block in between the yellow and the red point that's our o epsilon term so if the value at a point epsilon away is this point's value plus this linear function of epsilon plus something really small that we can maybe ignore if we're squinting we call that function that gold function there the derivative so here's how this definition gets used i want to give you a sense for how this would get used to define or or compute derivatives as opposed to the way it's normally done so consider the function x squared uh this function we'll see its derivative actually i won't spoil it just in case you don't recall or haven't learned the derivative of x squared so x squared is equal to x times x let's calculate a derivative for the function x squared just using our definition what we do is we write out the left-hand side of our definition f of x plus epsilon right so this is saying okay what is the value of our function at this other point and then we just move around our we move around our symbols we play some algebra games until we can get it to look like the right hand side of our definition and then all of those pieces we just match them so let me show you how that works x plus epsilon times x plus epsilon that's what f of x is right it's x squared and so the input gets squared our input is now x epsilon so then we just expand that out x squared plus x times epsilon plus epsilon times x plus epsilon squared we do a little bit of rearrangement this is just two copies of x times epsilon let's take a look at what we got well we got x squared that's our f of x right this is what i mean by pattern matching we match each component of this here to each component of this here then we've also got this nice thing here where we've got a function of x on the left-hand side so 2 times x that's a function of x i give you an x you return to me that value double it's a simple function but it's a function right so that's f prime of x so long as the rest of this expression here matches our definitions expression so epsilon here it's interacting with x only by multiplication right it's interacting with our f prime of x term only through multiplication so that counts as our sort of linear term in epsilon and then we've got this term here epsilon squared now you might think of squaring something as making it bigger because we normally think of numbers bigger than one right two squared is bigger than two it's four a hundred squared is ten thousand but for one over a hundred one over a hundred squared is one over ten thousand so squaring actually you know just like it makes things really much bigger as numbers get bigger it actually makes things much much smaller as numbers get closer to zero so epsilon squared actually counts as something that is smaller than epsilon that's this little o of epsilon terms so that's our final piece and now we've done it we've pattern matched this expression here to our definition and so the derivative of x squared is 2x and the nice thing here is we do have to we do have to be able to reason about this little o term here but i find that substantially easier than doing this whole thing underneath a limit limits it's always a little bit unclear to me what's allowed and what's not allowed under a limit and in fact physicists and mathematicians often fight about which limits are appropriate to take it's useful technology to have limits around but i like to to focus on specific examples where i can be very clear that what i'm doing is correct so let's talk about what little o is it is indeed a type of limit and a limit of ratios so the idea that limits of ratios are important for calculus is not gone it's just been given a really nice api is how i think about it little o is a nice abstraction a nice thing built out of limits that's easier to work with than the thing itself like python is built out of c so a term is little o of epsilon if it gets smaller much faster than epsilon does that's the intuitive way of framing it so little of epsilon means as you get close to zero something that is little of epsilon if it gets closer to zero much faster than epsilon does which has a specific mathematical definition which is that if the limit as x goes to zero of f of x over g of x is zero which means g of x the bottom of the ratio is bigger than the top of the ratio then we can replace f of x with little o of g of x so often g will just be the identity function like previously we had little of epsilon as our the inner argument there so that says that this function of the variable is smaller than the variable itself if we have f of epsilon over epsilon has its limit being zero and this means that for a small enough epsilon anything little of epsilon can safely be ignored so here are a couple of examples uh so some terms that are a little of epsilon so zero is little of epsilon if i look the limit as x goes to 0 of 0 over x that's just going to be 0. if i look at x squared so the limit of x squared over x that's going to be x squared divided by x that's just x limit as x goes to 0 is 0. kx squared i can multiply x squared by something and that doesn't change the argument i just gave the limit of k times x as x goes to 0 remains 0. so all those terms are little of x some things on the right that are not little of x so one over x so a constant value is not little of x it's if our approximation will always be off by one right so intuitively that makes sense but by definition what that means is the limit as x goes to zero of one over x that is actually infinite rather than zero x is not little of x x over x that's just one the limit of x goes to zero of one is one and multiplying that by something doesn't doesn't change it there's some useful rules like you can add them together if i have two little o terms the sum of them is still little o but i won't go fully into the details because it's actually not that important that you become really good at calculating derivatives it's more important that you have a sense intuitively for what these pieces mean and what the derivative is which is something that is off by a little o term and so the reason why i like this is because it's really similar to something that i end up using and thinking about when i'm writing computer code which is big o notation from computer science let's just talk about how fast algorithms are while abstracting away a bunch of irrelevant details about the exact implementation of it and things about that might be different across cpus or whatever so the idea is something that's big o of n is faster than something that's big o of n squared which is much faster than something that's big o of two d n for a big enough input similarly little o lets us talk about the best linear approximation while abstracting away exactly how good or bad that approximation is when you're doing the limit like the classical limit approach to studying calculus these two things get mixed up together and you always need to both measure how good the linear approximation is exactly and produce the correct value for it but with little o notation with the frechet derivative style that's the name for this definition of the derivative then you can split those two things apart little and big o they're called landau symbols and so if you want to look up more about these and see a little bit more about how they how they're used and how they relate uh landau symbol is the relevant vocab term to google there's a lot of setup covering something that maybe people know decently some people maybe know decently well single variable calculus now let's see it actually do something really useful for us and make defining the gradient or the derivative for a function of a vector much easier we're talking about here scalar valued functions of vectors functions that take in a vector something in rn or an array with n entries and return a single real value in computing a single floating point value so the phrase deriver definition again centers approximation and linearity and it looks very very similar on the left hand side we have the value of the function at a different point and then we have our four pieces the sort of starting point x the value of the function there we have a little o term and it says that our approximation error is going to be small relative to the norm of our epsilon the norm of how we're changing our input but now instead of just a simple multiplication we have here an inner product or a dot product this is a linear map right here we take in a vector we do its inner product with epsilon and that gives us this central term here this linear term and this is what we're going to use to approximate the behavior of the function f we you can think of this as in 2d it sets up like a plane that you approximate the function f with where normally in 2d this would be some surface if we have two dimensional inputs and one dimensional outputs we represent the inputs in two dimensions the outputs in the third dimension sticking out and so you would get some surface and we approximate that with just a plane just like a flat sheet of paper that's what this middle part here defines this is basically the same thing as defining that line in the one-dimensional case we just now have a vector with multiple entries to define it so a couple of things to notice here one is that the gradient is a function so there's a little bit of confusion here people refer to both the the gradient itself and the output of it with the term gradient properly just like a derivative the gradient is a function it returns takes in arrays and returns arrays this guy here takes in a vector f is a function of vectors and then what it returns is something that we can use to take an inner product with epsilon which is also a vector right because epsilon is something that we add to our inputs it's got to be a function that takes in vectors and returns vectors and then we use those vectors to create linear functions that approximate the original function there's lots of ways to write that inner product but the most important sort of alternative way to write it is as a matrix multiplication and this makes it a little bit more clear that one it reduces to the derivative for vectors of length one right if i transpose a single number nothing happens so i just get the number times this number and that's what we did with the derivative and then it's more clear that this right here is a linear function of epsilon for fixed x right so if i vary x all kinds of weird stuff can happen to this value but the main thing we're thinking about is at a fixed value of x what happens when we vary epsilon and in that case this is just a linear function here it's matrix multiplication matrix multiplication is how we apply linear functions that sort of centers this idea that what we're doing here is creating a linear approximation to the original function all the same principles that we had in the single variable version so let's use this definition in an actual example so we're going to calculate a gradient for the function x transpose x it's also written as the squared norm of the vector x which is to say it's basically the sum of the squared entries of the vector that double bar sine is the same thing as measuring the length of the vector the norm the square root of the sum of squared entries but we're squaring it so we get something that is x transpose x what is x transpose x that means i take each entry and multiply the first one times itself the second one times itself and so on so let's write that out if we were to apply this function here transpose and then multiply the things together then it would look something like this f of x plus epsilon is equal to x plus epsilon transpose x plus epsilon and that gives us again four terms just like in x squared x transpose x plus x transpose epsilon plus epsilon transpose x plus epsilon transpose epsilon and then we need to do a little bit of algebraic manipulation so to start off we have an easy one x transpose x that's just our original function f then 2x transpose epsilon where did this guy come from if you look at the definition of this inner product here all we're doing is multiplying the two entries together and summing the whole thing up it doesn't matter what order we do that in right it doesn't matter if the left-hand side is x and the right-hand side is epsilon or the left-hand side is epsilon and the right-hand side is x so these two things are the same just like x times epsilon and epsilon times x are the same and so we get a single term here in the middle that's 2x transpose epsilon and now we're really we're really in business here we have a function of x here that's 2 times x we take that that's a vector and we transpose it and then combine that with epsilon that's exactly what the middle linear term of our definition of the frechet derivative definition of the gradient that's exactly what that looks like it's a transpose of a function of x and that gets combined with epsilon and there's no epsilons anywhere naked single epsilons anywhere else there's no epsilon here so this is our middle term this last term here this epsilon squared term just like x squared is little o of x the squared norm of epsilon is little o of the norm of epsilon so that might be useful to go back to the original definitions and check that this actually works out but it does indeed work out it's basically it's actually pretty much basically the same thing this can also be applied to functions of matrices functions that take a matrix input and produce a scalar output so an example of that would be maybe for multiple linear regression where you have multiple inputs multiple outputs if you wanted to calculate how well that's doing you would need a scalar valued function of matrices and we won't go into great detail about this one the slides i'm going to skip over a bunch of slides that are available if you check the online version of the slides if you want to see in detail how this works but the key point is that it looks exactly the same as it did for vectors which looks exactly the same as it did for scalar values so one single approach three different applications so now we have all these letters are capital because they represent matrices our function f takes in a matrix returns a single value we know how it behaves on say this single matrix x we want to know what happens if we change x by adding to it this matrix e here this is a sort of capital epsilon at x plus this big matrix of different entries what does that look like it looks like f of x plus an inner product now between two matrices uh if you want to think about what this means it's sort of like turn both of these matrices into vectors and take their inner product but again a basically the key point here is that it is a linear function of epsilon so as we vary epsilon this whole thing varies linearly because it remains an inner product it remains essentially a form of matrix multiplication and the exact matrix that goes in here it's going to come out a little bit differently depending on which value of x goes in and the function that tells us how to take a value x and return the approximating matrix is the gradient now of this matrix valued function and so it's a matrix just like in the second example it was a vector and again we're off a little bit in general and this is going to be a little o term that's a function of the norm of a matrix so if you there's a little bit in the slides about you know a little bit more about what the inner product of a matrix is what the normal matrix is and some links to additional results and actually goes through a derivation i believe for a function of matrices and it's exactly the same as what we did with the vector-valued functions so this is the major benefit to thinking this way there's this single style for gradients of single variable vector and matrix functions it actually even extends to functional calculus where we're taking derivatives where the input is a function itself not just a a single variable vector or matrix so that's cool it's always nice to be using a tool that really extends all the way to the hardest cases and you may have noticed there were no indices anywhere so indices have disappeared from our calculations i find it cleaner easier to follow and more straightforward to not have to use indices everywhere the little o notation showing up instead of limits allows us to use some of our intuitions from using big o notation from thinking about the speed of programs and to avoid doing everything underneath the limit uh so that gives us a view of what calculus is doing it's giving us linear approximations to functions so now let's use that view of what calculus is doing in order to understand our optimization process in machine learning so when we optimize in machine learning we optimize through many tiny changes almost always there are few times where we optimize by just doing it all at once but we almost always optimize by making many tiny changes what we want to do in machine learning is we want to update the parameter values of a model to make it perform better so usually in machine learning our effectively our program for doing the task that we want to do is specified by a vector and then we can calculate how well it's doing with this function called the loss function so it takes in the vector it usually applies some function to a bunch of data sort of checks maybe it's predicting something one data value based off another value maybe it's compressing data there's lots of things we can do with machine learning but we always need to be able to score how well we are doing and that's the function f here and we want to make that value usually it's golf rules where lower is better and so i want to choose new parameters such that the value of this function goes down you know it'd be great if we just knew like all the values of f if we could just look at the plot of the function and say oh that would be the best value over there but we can't do that in general these are 100 dimensional vectors million dimensional vectors trillion dimensional vectors nowadays in machine learning so we'll never ever be able to look at them and solve it uh and we can't even actually like know what the value of the function is going to be at some other point we have to calculate values of this function it's often extremely expensive and we have to calculate for some models we have to feed gigabytes of data into them to know what this value is going to be so we want to try and avoid that calculation as much as possible so what we're going to do is we're going to use linear approximations to that function so we're going to make my best linear guess to what the function will be somewhere else and we want that to say oh your performance will be better the value of f will be smaller so we're going to choose new parameters to make the best linear guess go down as fast as possible and so we'll see in a second that the way to do that is to pick our value of epsilon our change in our parameters we pick it to point basically in the opposite direction that makes this linear term as negative as possible then the question is how big do we make epsilon do we make epsilon really big because we want to make a big change well no because we know that this is just a linear guess right we shouldn't change too much and that's coming from this last term here this purple term saying that the bigger we make our change the wronger we should expect to be about whether our machine learning model is actually performing better or not and reading all that off leads us directly to the algorithm of gradient descent so it says for our new value of x here on the left which we do by assignment it's equal to the previous value of x minus the value of the gradient and this guy minus eta times gradient of f x that there is our epsilon term from above this part here that's how we are changing x negative eta gradient of f of x so now why are we descending the gradient why are we using the negative gradient is because this makes the linear term as negative as possible so i've drawn this here the gold arrow that's the direction the gradient points remember the gradient here is a vector and so it has a direction and so imagine this two-dimensional space here is all the directions that vectors could point and the gradient is this vector pointing in this direction so this is a function that takes in two numbers and spits out a single number so the question is how does this function behave how does this inner product function behave as i change epsilon and that's what this circle here is meant to demonstrate as i rotate epsilon around then the arrow here is moving around this circle and at each point it produces a particular value of this term here this inner product term so when it's close to the gradient you get a really high value as indicated by this color scale here and as we rotate around and go towards the middle we get middling values and as we go all the way towards the opposite direction this term here becomes negative so when we're pointing all the way in this blue in this direction represented by the blue arrow here we get very negative values so this function here shows you actually exactly how this function behaves as you spin epsilon around you say clockwise from gold to blue and that blue variable variable there is negative one times the gradient it's the gradient flipped around just fyi this function may look familiar it's cosine it's basically it's a scaled version of cosine depending on how big the vectors are so the basic point of this slide is just that if we want to make this function here as negative as possible we want to make the epsilon value equal to negative one times the gradient and so point in this direction so that makes this term here in the middle as negative as possible and then so long as this term on the right here is not bigger than epsilon which we can guarantee because it's little o then that will make f at x plus epsilon smaller than f x and we will have made our machine learning algorithm perform better than it was performing before and so gradient descent basically goes down the steepest slope so i've drawn the gradients here on this topography here so this is what the function maybe looked like for that previous example we were looking at a single point and we were imagining the gradient points this way how will our linear approximation behave as we rotate epsilon now we have a two dimensional function here that produces a scalar value and i've drawn here a couple places i've drawn what the gradient looks like so the gradient points sort of up the gradient goes uphill in the steepest direction possible at the tops of hills it's very clear you're at the top there's nowhere to go that will make you go upwards at the bottoms of of valleys gradient the gradient is also once again zero the way to think about it maybe is that at the bottom here there's a whole bunch of directions you could go up in there's no way to choose a particular direction to go up in but it's maybe better rather than thinking of gradients as going up or down slopes it's maybe better to think in terms of these linear approximations we should say that at this point the best linear approximation is constant because the function's curving away in every direction which is the same at the top here uh you know higher values of this function mean worse performance so we want to descend gradients we don't want to go up we want to go down so we flip them around that's another way of thinking about what gradient descent does so grainy descent tends to find the bottoms of valleys in something like this we would end up in one of these little divots here by applying gradient descent we don't know where to start necessarily in machine learning in optimization in general we often think of ourselves as starting at a random point and so we end up randomly in one of these valleys so to make that really that metaphor really direct i've got here the topography of my home state of california which has a much more interesting topography than the state i was born in which is illinois very flat not very interesting for gradient descent california is much more interesting so if you imagine you start somewhere in california here with these big mountains and this big central valley this uh the bay area down here another low-lying area and you imagine okay what would happen if i just walked downhill because that's basically what gradient descent does in most places that you start you would end up in this big area here the central valley if you pick a random part of california to start in this basin here of the central valley is the place you would probably end up the least likely place for you to end up is the highest point in california mount whitney because you'd have to land exactly on the top of mount whitney and then if you're exactly at the top then there's basically every direction is downhill and you would go nowhere the lowest place in california is actually not the place where you would end up in most cases the lowest place in california is actually this really deep cut in the middle of the rocky mountains here called bad water basin and unlike the central valley it is like very sharp it goes down really quickly the central valley is nice and sort of like gently sloping down from the mountains into this big flat area and so bad water basin here is maybe where you think you'd like to end up in if you're looking for the lowest point in california but it's not where you're going to end up most of the time now what people have found is that gradient descent finds things like the central valley and for realistic problems you actually things like bad water basin these like sharp deep little divots are not where you want to end up and the central valley is actually better and this is one of the reasons why gradient descent has become basically the most popular algorithm for optimizing for optimizing machine learning models over all kinds of other options that people have considered for how to make algorithms better we have time to talk about the automation of calculus that's good we talked a good amount about how you would actually calculate derivatives in our two sections on scalar and vector valued functions but it's actually not the case that in order to do machine learning you need to become really good at calculus that used to be the case but luckily that's not the case anymore so you don't really need to sweat actually being able to calculate these things though it is very important that you develop your intuition for things about calculus so that you can understand the behavior of algorithms without having to whip out a math textbook so in the bad old days calculus for machine learning was done entirely by hand so this block of mathematics on the right here is the calculation of one piece of the gradient so in addition to getting the math right an ml researcher engineer in the late 90s then had to translate that map into computer code without introducing any bugs and this was this tremendous bottleneck like not only do you have to spend time actually calculating the gradients you then need to write them into like an equally sized block of code and then test that code so this part here is actually just this is the part of the code that implements the model that goes from inputs to final value uh so that's this block of code here and now if we were in the battle days in the 90s then what we would do is we would then have to write an additional equally sized block of code that would take this math here and basically write it out so derivative for this function derivative for this function derivative for all of these so nowadays what happens instead is that you take something like this and then you just pass it to something that automatically calculates gradients for you so in this case we use i'm using the auto grad autograd library here you basically just pass whatever function is you want the gradient of in this case the loss function to autograd instead of writing an equally complicated block of code to this 32 or so lines of dense math code and the derivatives you get will be tested verified by the the community of of folks who are experts in making sure that these these gradients are calculated correctly so the core technology that makes this work is something called a computational graph so this example is adapted from a really nice blog post called calculus on computational graphs by chris ola who's one of the sort of top explainers in the ml space if we take an expression like this we can break down how this gets calculated by a computer and sort of trace all the operations that go into calculating the final value e so we have our a plus b we have our b plus one these become intermediate variables call them c and d and then we multiply c and d together to get e so we've broken this down this is a composite expression and this is a sequence of atomic simple operations adding and multiplying so we can write that as a graph so a goes into the add function and so does b that's what this arrow means b goes into this add1 function these are our two variables c and d on the left and right here those two things get put into the multiply function and that's our final value so it's in green here at the bottom because it's the final output and then the inputs are in blue so we can actually so this here was a cartoon based off of this mathematical expression here we can actually turn that into a python expression and then generate that graph automatically i i generated this in pi torch which is a machine learning framework that provides computational graph support uh so i've got e is this variable down here mole backward you can ignore the backward part there so this is the multiplication step here and what goes into the multiplication it's two variables here go into the multiplication step and this one this left addition here has a and b as its components so a plus b go into this one and then b plus one goes into this one so this is another addition and it consumes only b not a this is a way of sort of tracing how a value was calculated so normally we don't think about that necessarily when we're writing computer programs we don't think about about carrying around the sequence of operations that result in a final value but actually if you just write if you just implement addition and multiplication in the right way then you can track all this information automatically and if we have stored somewhere a function for the gradient of addition and for the gradient of multiplication then we can use them to get the gradient of e with respect to the input variables and this is essentially an automated version of what you learn to do in calculus you memorize a few derivatives what's the derivative of sine what's the derivative of x squared what's the derivative of of e to the x and then you use rules to calculate new ones rules like the sum rule the product rule the chain rule and the difference is that computers are much faster and they don't make mistakes so they can do this automatically really quickly there's some rules for this which is basically the chain rule and the sum rule that basically say as i go along a path in a graph the gradients multiply and as i look across multiple paths converging then the gradients add you can read either these slides here or the blog post from chris ola if you want more detail about the rules where these rules come from to make the computational graph but the important point here is just that this technology enables the automatic calculation of gradients and so means that this difficult step in machine learning of figuring out what the best linear approximation of your function is that used to require a mathematician now does not let's close out with some additional resources so if you need a refresher on deep concepts like limit if you felt like okay i actually was a little bit confused by our discussion about like limits and approximations and derivatives i would recommend three blue one browns youtube series the essence of calculus this guy is one of the best math explainers in the world and the essence of calculus series is excellent just as the essence of linear algebra series was excellent if you want to learn more about this phrase derivative style i have a blog post series facial derivatives one through four that applies the frechet derivative to a couple of very simple ml problems culminating in linear neural networks if you want to deep dive on vector calculus beyond just machine learning your appetite for calculus has been wedded and you want to understand calculus in general better consider the multi-variable calculus lectures on khan academy it's an old-school mooc covering the calculus mostly what you need for physics and engineering things like divergence and curl that we didn't talk about and a lot more stuff about integrals which are less important for machine learning those are great they're actually by grant sanderson who later became three blue one brown so if you prefer to learn from interactive jupiter notebooks python examples then check out this calculus notebook a collab an online notebook by aurelion garan for his book hands-on machine learning it's got a bunch of these for a bunch of different topics so there's actually other math for machine learning notebooks in that in that repository and if you really want sort of a programmer's approach to calculus rather than a mathematician's approach there's this crazy class structure and interpretation of classical mechanics by some folks who made one of the most famous computer science classes in the world structure and interpretation of computer programs which takes this very functional programming approach to understanding derivatives and calculus and in the end the classical mechanics for which calculus was invented newton's stuff friends charles here thanks for watching my video if you enjoyed it give it a like if you want more ways to buy this tutorial and demo content subscribe to our channel and if you've got any questions comments ideas for future videos leave a comment below we'd love to hear from you
Original Description
In this video, W&B's Deep Learning Educator Charles Frye covers the core ideas from calculus that you need in order to do machine learning.
In particular, we'll see a different way of thinking about calculus -- based on linear approximations -- that makes thinking about vector- and matrix-valued derivatives easier. Then, we'll talk about the gradient descent algorithm, which is ubiquitous in machine learning, and how it arises naturally from thinking this way about calculus, and briefly touch on how calculus gets automated away.
Slides here: http://wandb.me/m4ml-calculus
Exercise notebooks here: https://github.com/wandb/edu/tree/main/math-for-ml
Check out the other Math4ML videos here: http://wandb.me/m4ml-videos
0:00 Introduction and overview
2:01 Vector calculus involves approximation with linear maps
3:48 The Fréchet derivative definition for single-variable calculus
12:50 Little-o notation makes calculus easier
16:50 The Fréchet derivative makes vector calculus easier
25:43 Gradient descent: tiny changes using calculus
34:38 Automating calculus
40:09 Additional resources
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Weights & Biases · Weights & Biases · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
0. What is machine learning?
Weights & Biases
1. Build Your First Machine Learning Model
Weights & Biases
Intro to ML: Course Overview
Weights & Biases
2. Multi-Layer Perceptrons
Weights & Biases
3. Convolutional Neural Networks
Weights & Biases
Weights & Biases at OpenAI
Weights & Biases
Why Experiment Tracking is Crucial to OpenAI
Weights & Biases
4. Autoencoders
Weights & Biases
5. Sentiment Analysis
Weights & Biases
6. Recurrent Neural Networks [RNNs]
Weights & Biases
7. Text Generation using LSTMs and GRUs
Weights & Biases
8. Text Classification Using Convolutional Neural Networks
Weights & Biases
9. Hybrid LSTMs [Long Short-Term Memory]
Weights & Biases
Toyota Research Institute on Experiment Tracking with Weights & Biases
Weights & Biases
Weights and Biases - Developer Tools for Deep Learning
Weights & Biases
Introducing Weights & Biases
Weights & Biases
10. Seq2Seq Models
Weights & Biases
11. Transfer Learning for Domain-Specific Image Classification with Small Datasets
Weights & Biases
12. One-shot learning for teaching neural networks to classify objects never seen before
Weights & Biases
13. Speech Recognition with Convolutional Neural Networks in Keras/TensorFlow
Weights & Biases
14. Data Augmentation | Keras
Weights & Biases
15. Batch Size and Learning Rate in CNNs
Weights & Biases
Applied Deep Learning Fellowship Overview and Project Selection with Josh Tobin (2019)
Weights & Biases
Grading Rubric for AI Applications with Sergey Karayev (2019)
Weights & Biases
16. Video Frame Prediction using CNNs and LSTMs (2019)
Weights & Biases
Image to LaTeX - Applied Deep Learning Fellowship (2019)
Weights & Biases
17. Build and Deploy an Emotion Classifier (2019)
Weights & Biases
Applied Deep Learning - Data Management with Josh Tobin (2019)
Weights & Biases
Snorkel: Programming Training Data with Paroma Varma of Stanford University (2019)
Weights & Biases
Applied Deep Learning - Troubleshooting and Debugging with Josh Tobin (2019)
Weights & Biases
Troubleshooting and Iterating ML Models with Lee Redden (2019)
Weights & Biases
Designing a Machine Learning Project with Neal Khosla (2019)
Weights & Biases
Lukas Beiwald on ML Tools and Experiment Management (2019)
Weights & Biases
Building Machine Learning Teams with Josh Tobin (2019)
Weights & Biases
Pieter Abeel on Potential Deep Learning Research Directions (2019)
Weights & Biases
Testing and Deployment of Deep Learning Models with Josh Tobin (2019)
Weights & Biases
Five Lessons for Team-Oriented Research with Peter Welder (2019)
Weights & Biases
Applied Deep Learning - Rosanne Liu on AI Research (2019)
Weights & Biases
Making the Mid-career Leap from Urban Design to Deep Learning/Data Science
Weights & Biases
Organizing ML projects — W&B walkthrough (2020)
Weights & Biases
Brandon Rohrer — Machine Learning in Production for Robots
Weights & Biases
Nicolas Koumchatzky — Machine Learning in Production for Self-Driving Cars
Weights & Biases
My experiments with Reinforcement Learning with Jariullah Safi
Weights & Biases
Applications of Machine Learning to COVID-19 Research with Isaac Godfried
Weights & Biases
Testing Machine Learning Models with Eric Schles
Weights & Biases
How Linear Algebra is not like Algebra with Charles Frye
Weights & Biases
Predicting Protein Structures using Deep Learning with Jonathan King
Weights & Biases
Rachael Tatman — Conversational AI and Linguistics
Weights & Biases
Reformer by Han Lee
Weights & Biases
Sequence Models with Pujaa Rajan
Weights & Biases
GitHub Actions & Machine Learning Workflows with Hamel Husain
Weights & Biases
Look Mom, No Indices! Vector Calculus with the Fréchet Derivative by Charles Frye
Weights & Biases
Jack Clark — Building Trustworthy AI Systems
Weights & Biases
Surprising Utility of Surprise: Why ML Uses Negative Log Probabilities - Charles Frye
Weights & Biases
Track your machine learning experiments locally, with W&B Local - Chris Van Pelt
Weights & Biases
Antipatterns in open source research code with Jariullah Safi
Weights & Biases
Attention for time series forecasting & COVID predictions - Isaac Godfried
Weights & Biases
Made with ML - Goku Mohandas
Weights & Biases
Angela & Danielle — Designing ML Models for Millions of Consumer Robots
Weights & Biases
Deep Learning Salon by Weights & Biases
Weights & Biases
More on: ML Maths Basics
View skill →Related Reads
Chapters (8)
Introduction and overview
2:01
Vector calculus involves approximation with linear maps
3:48
The Fréchet derivative definition for single-variable calculus
12:50
Little-o notation makes calculus easier
16:50
The Fréchet derivative makes vector calculus easier
25:43
Gradient descent: tiny changes using calculus
34:38
Automating calculus
40:09
Additional resources
🎓
Tutor Explanation
DeepCamp AI