Training Softmax Classifier (C2W3L09)

This content was created by LARRUZ. This content was uploaded by LARRUZ. This is a YouTube video titled "Training Softmax Classifier (C2W3L09)" created on August 25, 2017. It features a lecture by Andrew Ng discussing the softmax layer, softmax activation function, and training models using softmax classification, including loss functions and gradient descent in neural networks.
Chapters
In the last video, you learned about the softmax layer and the softmax activation function. In this video, you deepen your understanding of softmax classification and also learn how to train a model that uses a softmax layer. Recall our early example where the output layer computes zl as follows. So if you have four classes C equals four, then zl is going to be four by one dimensional vector. And we said we compute T, which is this temporary variable that performs element wise exponentiation. And then finally, if the activation function for your output layer G of L is the softmax activation function, then the output will be this. It's basically taking that temporary variable T and normalizing it to sum to one. So this then becomes a of L. So you notice that in the Z vector, the biggest element was five, and the biggest probability ends up being this first probability. The name softmax comes from contrasting it to what's called a hardmax, which would have taken the vector Z and mapped it to this vector. So hard max function will look at the elements of z and just put a 1 in the position of the biggest element of z and then zeros everywhere else. And so this is a very hard max, where the biggest element gets a output of 1 and everything else gets an output of 0. Whereas in contrast, the softmax is a more gentle mapping from Z to these probabilities. So I'm not sure if this is a great name, but at least that was the intuition behind why we call it a softmax. It's in contrast to the hard max. And one thing I didn't really show but had alluded to is that softmax regression, or the softmax activation function, generalizes the logistic activation function to C classes rather than just two classes. And it turns out that if C is equal to 2, then softmax with C equals to 2 essentially reduces to logistic regression. And I'm not going to prove this in this video, but the rough outline for the proof is that if C equals to 2, and if you apply softmax, you know, then the output layer Al will output two numbers if C equals 2. So maybe it outputs 0.842 and 0.158, right? And these two numbers always have to sum to 1. And because these two numbers always have to sum To 1, they're actually redundant. And maybe you don't need to bother to compute two of them. Maybe you just need to compute one of them. And it turns out that the way you end up computing that number reduces to the way that logistic regression is computing this single output. So that wasn't much of a proof. But the takeaway from this is that softmax regression is a generalization of logistic regression to more than two classes. Now, let's look at how you would actually train a neural network with a softmax output layer. So, in particular, let's define the loss function you use to train your neural network. Let's take an example. Let's say you have an example in your training set where the output is where the target output, the ground truth label is 01, 00. So the example from the previous video, this means that this is an image of a CA because it falls into Class 1. And now let's say that your neural network is currently outputting y hat equals. So y hat would be a vector probability. It's got a sum to 1 0.1, 0.4, so you can check that sums to 1, and this is going to be al. So the neural network is not doing very well in this example because this is actually a cat and assigned only a 20% chance that this is a cat. So it didn't do very well in this example. So what's the loss function you would want to use to train this neural network? In softmax classification, the loss we typically use is negative sum of j equals 1 through 4 and is really sum from 1 to C. In the general case, I'll just use 4 here of y log or yj log y hat of j. So let's look at our single example above to better understand what happens. Notice that in this example, y1 equals y3 equals y4 equals 0 because those are zeros and only y2 is equal to 1. So if you look at this summation, all the terms with zero values of yj will equal to zero, and the only term you're left with is negative y2 log y hat 2. Because when you sum over the indices of j, all the terms will end up 0, except when j is equal to 2. And because y 2 is equal to 1, this is just negative log y hat 2. So what this means is that if your learning algorithm is trying to make this small because you use gradient descent to, you know, try to reduce the loss on your training set, then the only way to make this small is to make this small. And the only way to do that is to make y hat 2 as big as possible. And these are probabilities, so it can never be bigger than one. But this kind of makes sense because if X for this example, is a picture of a cat, then you want that Output probability to be as big as possible. So more generally, what this loss function does is it looks at whatever is the ground truth class in your training set, and it tries to make the corresponding probability of that class as high as possible. If you're familiar with maximum likelihood estimation and statistics, this turns out to be a form of maximum likelihood estimation. But if you don't know what that means, don't worry about it. The intuition we just talked about will suffice. Now, this is the loss on a single training example. How about the cost j on the entire training set? So the cost of setting the parameters you know, and so on. Of all the ways and biases, you define that as pretty much what you guess sum over your entire training set of the loss, your learning algorithms predictions summed over your training samples. And so what you do is use gradient descent in order to try to minimize this cost function. Finally, one more implementational detail. Notice that because C is equal to 4, y is a 4 by 1 vector and y hat is also a 4 by 1 vector. So if you're using a vectorized permutation, the matrix capital Y is going to be Y1, Y2 through YM stacked horizontally. And so for example, if this example up here is your first training example, then the first column of this matrix y will be 01, 00, and then your second example, maybe the second example is a dog, maybe the third example is a none of the above, and so on. And then this matrix capital Y will end up being a 4 by M dimensional matrix. And similarly Y hat will be Y hat one stacked up horizontally going through Y hat m. So if this is actually y hat1 or the output on the first training example, then Y hat will be this 0.3, 0.2, 0.1, 0.4 and so on. And Y hat itself will also be a 4 by M dimensional matrix. Finally, let's take a look at how you'd implement gradient descent when you have a softmax output layer. So this output layer will compute zl, which is c by one in our example four by one. And then you apply the softmax activation function to get al or Y hat. And then that in turn allows you to compute the loss. So we've talked about how to implement the forward propagation step of the neural network to get this output and to compute that loss. How about the back propagation step or gradient descent? Turns out that the key step or the key equation you need to initialize backprop is this expression that the derivative with respect to Z at the last layer, this Turns out you can compute as y hat the 4 by 1 vector minus y the 4 by 1 vector. So you notice that all of these are going to be four by one vectors when you have four classes and c by one in a more general case. And so this going by our usual definition of what is dz? This is a partial derivative for the cost function with respect to zl. Um, if you're an expert in calculus, you can derive this yourself. Uh, or if you're an expert in calculus, you can try to derive this yourself. But using this formula will also just work fine if you ever need to implement this from scratch. But with this, you can then compute DZL and then sort of start off the backprop process to compute all the derivatives you need throughout your neural network. But it turns out that in this week's programming exercise, we'll start to use one of the deep learning programming frameworks. And for those programming frameworks, usually it turns out you just need to focus on getting the forward prop right. And so long as you specify the programming framework, the forward prop pass, the Perring framework will figure out how to do backprop or how to do the backward pass for you. So this expression is worth keeping in mind for if you ever need to implement softmax regression or softmax classification from scratch. Although you won't actually need this in this week's programming exercise, because the programming framework you use will take care of this derivative computation for you. So that's it for softmax classification. With it, you can now implement learning algorithms to categorize your inputs into not just one of two classes, but one of sub. See different classes. Next, I want to show you some of the deep learning programming frameworks, which can make you much more efficient in terms of implementing deep learning algorithms. Let's go on to the next video to discuss that.










