Mini Batch Gradient Descent (C2W2L01)

Andrew Ng explains mini-batch gradient descent, where you split a large training set into smaller batches to speed up learning. With 5 million examples and batch size 1000, you get 5000 mini-batches per epoch. It's faster than processing all data at once and is the standard method in deep learning.
Chapters
Hello and welcome back. In this week, you learned about optimization algorithms that will enable you to train your neural networks much faster. You've heard me say before that applied machine learning is a highly empirical process. It's highly iterative process in which you just have to train a lot of models to find one that works really well. So it really helps to really train models quickly. One thing that makes it more difficult is that deep learning tends to work best in the regime of big data. When you're able to train your neural networks on, on a huge data set and training on the large data set is just slow. So what you find is that having fast optimization algorithms, having good optimization algorithms, can really speed up the efficiency of you and your team. So let's get started by talking about mini batch gradient descent. You've learned previously that vectorization allows you to efficiently compute on all M examples. That allows you to process your whole training set without an explicit form. So that's why we would take our training examples and stack them into this huge matrix capital x. So x1, x2, x3, you know, and then eventually it goes up to XM if you have M training examples. And similarly for y, this is y1, y2, y3, and so on up to. Yeah. So the dimension of x was nx by m and this was 1 by m. Vectorization allows you to process all M examples quickly, relatively quickly. If M is very large, then it can still be slow. So for example, what if M was 5 million or 50 million or even bigger? With the implementation of gradient descent on your whole training set, what you have to do is you have to process your entire training set before you take one little step step of gradient descent. And then you have to process your entire training sets of 5 million training examples again before you take another little step of gradient descent. So it turns out that you can get a faster algorithm if you let gradient descent start to make some progress even before you finish processing your entire your giant training sets of 5 million examples. In particular, here's what you can do. Let's say that you split up your training set into smaller your little baby training sets. Um, and these baby training sets are called mini batches. And let's say each of your baby training sets have just 1,000 examples each. So you take x1 through x1000, and you call that your first little baby training set, also called a mimi batch. And then you take the next 1000 examples, x1100 through x2000, that's the next thousand examples, and call it the next one and so on. And I'm gonna introduce a new notation. I'm gonna call this X SuperScript with curly braces 1, and I'm gonna call this X Superscript with curly braces 2. Now, if you have 5 million training examples total, and each of these little mini batches has 1000 examples, that means you have 5000 of these because, you know, times 1000 equals 5 million. So altogether you would have 5000 of these mini batches. So, so it ends with x superscript curly braces 5000. And then similarly, you do the same thing for Y. You'd also split up your training data for Y accordingly. So you call that Y1. Um, then this is Y1001 through Y2000. This becomes called Y2, and so on, until you have Y5000. So now, mini batch number T is going to be comprised of X t and y t, and that is thousand training examples with the corresponding input output pairs. Before moving on, just to make sure our notation is clear, we have previously used superscript round brackets I to index in the training set. So xi is the ith training sample. We use superscript square brackets l to index into the different layers of a neural network. So zl comes from the z values for the l th layer of the neural network. And here we're introducing the curly brackets t to index into different meaning factors. So you have xt y, t. And to check your understanding of these, what's the dimension right of x, t and yt? Well, x is nx by m. So if x1 is a thousand training examples or the x values for a thousand examples, then this dimension should be NX by 1000, and X2 should also be NX by 1000 and so on. So all of these should have dimension NX by 1000, and these should have dimension 1 by 1000. Right. To explain the name of this algorithm, Bash gradient descent refers to the gradient descent algorithm we've been talking about previously, where you process your entire training set all at the same time. And the name comes from viewing that as processing your entire batch of training examples all at the same time. Obnoxious is a great name, but that's just what it's called. Mini batch train descent, in contrast, refers to the algorithm which we'll talk about on the next slide, in which you process a single mini batch xt yt at the same time, rather than processing your entire training set XY at the same time. So let's see how mini batch gradient descent works. To run mini batch gradient descent on your training sets, you would run for T equals 1 to 5,000, because we had 5,000 mini batches of size 1,000 each. And what you're gonna do inside the for loop is basically implement one step of gradient descent using XT, YT. And it's as if you had a training set of size 1000 examples. And it was as if you were to implement the algorithm you're already familiar with. But just on this, you know, little training set size of M equals 1000, rather than having an explicit for loop over all 1000 examples, you would use vectorization to process all 1000 examples, sort of all at the same time. Okay, so let's write this out. First, you implement forward prop on the inputs, so just on XT. And you do that by implementing, you know, Z1 equals W1. Now, previously we would just have X there, right? But now you aren't processing the entire training set, you're just processing the first mini batch. So this becomes XT when you're processing mini bash t. Um, and then you would have A1 equals G1 of Z1, right? This would be a capital Z, since we're. This is actually a vectorized implementation and so on until you end up with, um, a L, you know, as, I guess GL of zl. And then this is your prediction. And you notice that here you should use a vectorized implementation. It's just that this vectorized implementation processes 1,000 examples at a time rather than 5 million examples. Next, you compute the cost function j, which I'm gonna write as 1/1, since here 1000 is the size of your little training set. Sum From I equals 1 through L of really the, you know, loss of y hat I yi. Um, and this notation, for clarity, refers to examples from the mini batch XT yt. And then if you're using regularization, you can also have this regularization term, I guess maybe a 2 in the denominator times sum over L, you know, Frobenius norm, the weight matrices is squared. So because this is really the cost on just one mini batch, I'm gonna index this cost j with a superscript t in curly braces. So you notice that everything we're doing is exactly the same as when we were previously implementing gradient descent, except that instead of doing it on xy, you're now doing it on X t, Y T. Next, you'd implement backprop to compute gradients with respect to really with respect to this jt. So you're still using only XT yt. And then you update the weights you know, W really every WL gets updated as wl minus alpha, D wl and similarly for B. And so this is one pass through your training set using mini batch gradient descent. The code I've written down here is also called doing one epoch of training. An epoch is a word that just means a single pass through the training set. So whereas with batch gradient descent, a single pass through the training set allows you to take only one gradient descent step, with mini batch gradient descent, a single pass through the training set, that is one epoch, allows you to take 5,000 gradient descent steps. Now, of course you want to take multiple passes through the training set, which you usually want to. You might want another for loop or another, you know, while loop out there. So you keep taking passes through the training set until hopefully you converge or have approximately converged when you have a loss. Training set, meaning batch gradient descent, runs much faster than batch gradient descent and is pretty much what everyone in deep learning will use when you're training on a large dataset. In the next video, let's delve deeper into meaning batch gradient descent so you can get a better understanding of what it's doing and why it works so well.










