Regularization (C2W1L04)

Lecture on regularization to reduce overfitting (high variance). Covers L2 (weight-decay) for logistic regression and neural networks (using Frobenius norm for weight matrices), brief note on L1 (sparsity), how regularization alters gradient-descent/backprop updates (multiplicative decay term), and tuning the regularization hyperparameter λ (use dev set/cross-validation; use LAMBD in Python exercises).
Chapters
If you suspect your neural network is overfitting your data, that is, if you have a high variance problem, one of the first things you should try is probably regularization. The other way to address high variance is to get more training data. That's also quite reliable. But you can't always get more training data. It could be expensive to get more data. But adding regularization will often help to prevent overfitting or to reduce variance in your neural network. So let's see how regularization works. Let's develop these ideas using logistic regression. Recall that for logistic regression, you try to minimize the cost function J, which is defined as this cost function sum over your training examples of the losses of the individual predictions, and the different examples where you recall that W and B in logistic regression are the parameters. So W is a an X dimensional parameter vector and B is, is a real number. And so to add regularization to logistic regression, what you do is add to it this thing lambda, which is called the regularization parameter. Say more about that in a second. But Lambda over 2m times the norm of W squared. So here the norm of W squared is just equal to sum From j equals 1 to nx of wj squared. Or this can also be written W transpose W is just a squared Euclidean norm of the parameter vector W. And this is called L2 regularization. Because here you're using the Euclidean norm, also called the L2 norm, with the parameter vector W. Now why do we regularize just the parameter W? Why don't we add something? Here you know about B as well. In practice you could do this, but I usually just omit this because if you look at your parameters, W is usually a pretty high dimensional parameter vector, especially for a high variance problem. Maybe W just has a lot of parameters, so you aren't fitting all the parameters well. Whereas B is just a single number. So almost all the parameters are in W rather than B. And if you add this last term in practice, it won't make much of a difference because B is just one parameter out of a very large number of parameters. In practice I usually just don't bother to include it, but you can if you want. So L2 regularization is the most common type of regularization. Um, you might have also heard of some people talk about L1 regularization, and that's when, um, you add Instead of this L2 norm, you instead add a term that is, um, lambda over m of sum over of this. And this is also called the L1 norm of the parameter vector W. So little subscript 1 down there, right? And I guess whether you put m or 2 m in the denominator, it's just a scaling constant. If you use L1 regularization, then W will end up being sparse. And what that means is that the W vector will have a lot of zeros in it. And some people say that this can help with compressing the model. Because the sum of parameters are zero, then you need less memory to store the model. Although I find that in practice, L1 regularization to make your model sparse helps only a little bit. So I don't think it's used that much, at least not for the purpose of compressing your model. And when people train neural networks, L2 regularization is just used much, much more often. Oh, sorry. Fixing up some of the notation here. So, one last detail. Lambda here is called the regularization parameter. And usually you set this using your development set or using holdout cross validation, where you try a variety of values and see what does the best in terms of trading off between doing well in your training set versus also setting the two norm of your parameters to be small, which helps prevent overfitting. So lambda is another hyper parameter that you might have to tune. And by the way, for the programming exercises, lambda is a reserved keyword in the Python programming language. So in the programming exercise, you know, we'll have LAMBD without the A so as not to clash with a reserved keyword in Python. So we use LAMBD to represent the lambda regularization parameter. So this is how you implement L2 regularization for logistic regression. How about a neural network? In a neural network, you have a cost function that's a function of all of your parameters, W1B1 through W, capital L, B capital L, where capital L is the number of layers in your neural network. Um, and so the cost function is this sum of the losses summed over your m training examples. And so to add regularization, you add lambda over 2m of sum over all of your parameter w, your parameter matrices, W of their, let's call it the squared norm, where this norm of a matrix, really the squared norm is defined as the sum over I sum over j of, you know, each of the elements of that matrix squared. And if you want the indices of the summation, this is sum From I equals 1 through nl minus 1, sum from j equals 1 through nl, because w is a n l minus 1 by nl dimensional matrix, where these are the number of hidden units or number of units in layers L minus 1 and layer L. So this matrix norm it turns out, is called the Frobenius norm of a matrix, denoted with F in the subscript. So for arcane linear algebra technical reasons, this is not called the, you know, L2 norm of a matrix. Instead, it's called the Frobenius norm of a matrix. I know it sounds like it'd be more natural just call the L2 norm of the matrix. But for really arcane reasons that you don't need to know by convention, this is called the Frobenius norm, which just means the sum of square of elements of a matrix. So how do you implement gradient descent with this? Previously, we would compute dw, you know, using backprop, where backprop would give us the partial derivative of J with respect to W. Or, well, really W for any given L, and then you update WL as wl minus the learning rate times D. So this was before we added this extra regularization term to the objective. Now that we've added this regularization term to the objective, what you do is you take DW and you add to it lambda over m times W, and then you just compute this update, same as before. And it turns out that with this new definition of dwl, this is still, you know, this new DWL is still a correct definition of the derivative of your cost function with respect to your parameters. Now that you've added the extra regularization term at the end. And it's for this reason that L2 regularization is sometimes also called weight decay. So if I take this definition of, you know, DWL and just plug it in here, then you see that the update is WL gets updated as wl times the learning rate, alpha times, you know, the thing from backprop, um, plus lambda of m times wl with a minus sign there. And so this is equal to wl minus alpha lambda over m times wl minus alpha times, you know, the thing you got from backprop. And so this term shows that whatever the matrix WL is, you're gonna make it, you know, a little bit smaller, right? This is actually as if you're taking the matrix W and you're multiplying it by 1 minus Alpha Lambda over m. You're really taking the matrix W and subtracting alpha lambda over m times it. See, it's like you're multiplying the matrix matrix W by this number, which is going to be a little bit less than 1. So this is why L2 norm regularization is also called weight decay, because it's just like the ordinary gradient descent where you update W by subtracting alpha times the original gradient you got from backprop. But now you're also, you know, multiplying W by this by distinct, which is a little bit less than 1. So the alternative name for L2 regularization is weight decay. I'm not really gonna use that name, but the intuition for why it's called weight decay is that this first term here is equal to this. So you're just multiplying the weight matrix by numbers slightly less than 1. So that's how you implement L2 regularization in a neural network. Now, one question that people sometimes ask me is, you know, hey, Andrew, why does regularization prevent overfitting? Let's take a quick look at the next and gain some intuition for how regularization prevents overfitting.










