5.8 Multiclass Classification  | Neural Network with Softmax output --[Machine Learning | Andrew Ng]
 In order to build a neural network that can carry out multi-class classification, we're going to take the Softmax regression model and put it into essentially the output layer of a neural network. Let's take a look at how to do that. Previously, when we were doing handwritten digit recognition with just two clauses, we used a neural network with this architecture. If you now want to do handwritten digit classification with 10 clauses, all the digits from 0 to 9, then we're going to change this neural network to have 10 output units, like so. And this new output layer will be a Softmax output layer. So sometimes we'll say this neural network has a Softmax output or that this output layer is a Softmax layer. And the way forward propagation works in this neural network is, given an input x, a1 gets computed exactly the same as before, and then a2, deactivations for the second hidden layer, also get computed exactly the same as before. And we now have to compute deactivations for this output layer. That is a3. This is how it works. If you have 10 output clauses, we will compute z1, z2, through z10 using these expressions. So this is actually very similar to what we had previously for the formula you used to compute z. z1 is w1.product with a2, deactivations from the previous layer, plus b1, and so on, for z1 through z10. Then a1 is equal to e to the z1 divided by e to the z1 plus dot dot dot plus up to e to the z10. And that's our estimate of the chance that y is equal to 1. And similarly for a2, and similarly all the way up to a10. And so this gives you your estimates of the chance of y being equal to 1, 2, and so on up through the 10th possible label for y. And just for completeness, if you want to indicate that these are the quantities associated with layer 3, technically I should add these superscript 3s there. It does make the notation a little bit more cluttered, but this makes explicit that this is, for example, the z31 value and this is the parameters associated with the first unit of layer 3 of this neural network. And with this, your softmax output layer now gives you estimates of the chance of y being any of these 10 possible output labels. I do want to mention that the softmax layer, or sometimes also called the softmax activation function, it is a little bit unusual in one respect compared to the other activation functions we've seen so far like sigmoid, ReLU, and linear, which is that when we're looking at sigmoid or ReLU or linear activation functions, a1 was a function of z1 and a2 was a function of z2 and only z2. In other words, to obtain the activation values, we could apply the activation function g, be it sigmoid or ReLU or something else, element-wise to z1 and z2 and so on to get any of the a1 and a2 and a3 and a4. But with the softmax activation function, notice that a1 is a function of z1 and z2 and z3 all the way up to z10. So each of these activation values depends on all of the values of z. And this is a property that's a bit unique to the softmax output or the softmax activation function. I'll state it differently, if you want to compute a1 through a10, that is a function of z1 all the way up to z10 simultaneously. And this is unlike the other activation functions we've seen so far. Finally, let's look at how you would implement this in TensorFlow. If you want to implement the neural network that I've shown here on this slide, this is the code to do so. Similar as before, there are three steps to specifying and training the model. The first step is to tell TensorFlow to sequentially string together three layers. First layer is this 25 units with ReLU activation function, second layer, 15 units with ReLU activation function, and then the third layer, because there are now 10 output units, you want to output a1 through a10, so there are now 10 output units, and we'll tell TensorFlow to use the softmax activation function. And the cost function that you saw in the last video, TensorFlow calls that the sparse categorical cross entropy function. So I know this name is a bit of a mouthful, whereas for logistic regression, we had the binary cross entropy function. Here we're using the sparse categorical cross entropy function, and what sparse categorical refers to is that you still classify y into categories, so it's categorical, it takes on values from 1 to 10, and sparse refers to that y can only take on one of these 10 values, so each image is either 0 or 1 or 2 or so on up to 9, you're not going to see a picture that is simultaneously the number 2 and the number 7, so sparse refers to that each digit is only one of these categories. So that's why the loss function that you saw in the last video is called, in TensorFlow, the sparse categorical cross entropy loss function. And then the code for training the model is just the same as before, and if you use this code, you can train a neural network on a multi-class classification problem. Just one important note, if you use this code exactly as I've written here, it will work, but don't actually use this code, because it turns out that in TensorFlow, there's a better version of the code that makes TensorFlow work better. So even though the code shown in this slide works, don't use this code the way I've written it here, because in a later video this week, you'll see a different version, there's actually the recommended version of implementing this that will work better, but we'll take a look at that in a later video. So now you know how to train a neural network with a softmax output layer with one caveat. There's a different version of the code that will make TensorFlow able to compute these probabilities much more accurately. Let's take a look at that in the next video, which will also show you the actual code that I recommend you use if you're training a softmax neural network. Let's go on to the next video.
[{"start": 0.0, "end": 7.12, "text": " In order to build a neural network that can carry out multi-class classification, we're"}, {"start": 7.12, "end": 13.24, "text": " going to take the Softmax regression model and put it into essentially the output layer"}, {"start": 13.24, "end": 14.24, "text": " of a neural network."}, {"start": 14.24, "end": 16.92, "text": " Let's take a look at how to do that."}, {"start": 16.92, "end": 22.98, "text": " Previously, when we were doing handwritten digit recognition with just two clauses, we"}, {"start": 22.98, "end": 26.580000000000002, "text": " used a neural network with this architecture."}, {"start": 26.58, "end": 34.519999999999996, "text": " If you now want to do handwritten digit classification with 10 clauses, all the digits from 0 to"}, {"start": 34.519999999999996, "end": 42.519999999999996, "text": " 9, then we're going to change this neural network to have 10 output units, like so."}, {"start": 42.519999999999996, "end": 47.04, "text": " And this new output layer will be a Softmax output layer."}, {"start": 47.04, "end": 51.8, "text": " So sometimes we'll say this neural network has a Softmax output or that this output layer"}, {"start": 51.8, "end": 55.0, "text": " is a Softmax layer."}, {"start": 55.0, "end": 61.08, "text": " And the way forward propagation works in this neural network is, given an input x,"}, {"start": 61.08, "end": 67.2, "text": " a1 gets computed exactly the same as before, and then a2, deactivations for the second"}, {"start": 67.2, "end": 72.6, "text": " hidden layer, also get computed exactly the same as before."}, {"start": 72.6, "end": 77.6, "text": " And we now have to compute deactivations for this output layer."}, {"start": 77.6, "end": 79.8, "text": " That is a3."}, {"start": 79.8, "end": 81.8, "text": " This is how it works."}, {"start": 81.8, "end": 89.64, "text": " If you have 10 output clauses, we will compute z1, z2, through z10 using these expressions."}, {"start": 89.64, "end": 94.64, "text": " So this is actually very similar to what we had previously for the formula you used to"}, {"start": 94.64, "end": 95.64, "text": " compute z."}, {"start": 95.64, "end": 104.4, "text": " z1 is w1.product with a2, deactivations from the previous layer, plus b1, and so on, for"}, {"start": 104.4, "end": 107.2, "text": " z1 through z10."}, {"start": 107.2, "end": 116.24000000000001, "text": " Then a1 is equal to e to the z1 divided by e to the z1 plus dot dot dot plus up to e"}, {"start": 116.24000000000001, "end": 117.8, "text": " to the z10."}, {"start": 117.8, "end": 122.76, "text": " And that's our estimate of the chance that y is equal to 1."}, {"start": 122.76, "end": 129.52, "text": " And similarly for a2, and similarly all the way up to a10."}, {"start": 129.52, "end": 135.44, "text": " And so this gives you your estimates of the chance of y being equal to 1, 2, and so on"}, {"start": 135.44, "end": 139.84, "text": " up through the 10th possible label for y."}, {"start": 139.84, "end": 145.72, "text": " And just for completeness, if you want to indicate that these are the quantities associated"}, {"start": 145.72, "end": 151.3, "text": " with layer 3, technically I should add these superscript 3s there."}, {"start": 151.3, "end": 155.44, "text": " It does make the notation a little bit more cluttered, but this makes explicit that this"}, {"start": 155.44, "end": 165.07999999999998, "text": " is, for example, the z31 value and this is the parameters associated with the first unit"}, {"start": 165.08, "end": 169.16000000000003, "text": " of layer 3 of this neural network."}, {"start": 169.16000000000003, "end": 176.44, "text": " And with this, your softmax output layer now gives you estimates of the chance of y being"}, {"start": 176.44, "end": 180.10000000000002, "text": " any of these 10 possible output labels."}, {"start": 180.10000000000002, "end": 185.04000000000002, "text": " I do want to mention that the softmax layer, or sometimes also called the softmax activation"}, {"start": 185.04000000000002, "end": 191.0, "text": " function, it is a little bit unusual in one respect compared to the other activation functions"}, {"start": 191.0, "end": 196.68, "text": " we've seen so far like sigmoid, ReLU, and linear, which is that when we're looking at sigmoid"}, {"start": 196.68, "end": 206.4, "text": " or ReLU or linear activation functions, a1 was a function of z1 and a2 was a function"}, {"start": 206.4, "end": 210.04, "text": " of z2 and only z2."}, {"start": 210.04, "end": 215.48, "text": " In other words, to obtain the activation values, we could apply the activation function g,"}, {"start": 215.48, "end": 220.96, "text": " be it sigmoid or ReLU or something else, element-wise to z1 and z2 and so on to get any of the"}, {"start": 220.96, "end": 224.88, "text": " a1 and a2 and a3 and a4."}, {"start": 224.88, "end": 232.88, "text": " But with the softmax activation function, notice that a1 is a function of z1 and z2"}, {"start": 232.88, "end": 235.56, "text": " and z3 all the way up to z10."}, {"start": 235.56, "end": 240.96, "text": " So each of these activation values depends on all of the values of z."}, {"start": 240.96, "end": 246.24, "text": " And this is a property that's a bit unique to the softmax output or the softmax activation"}, {"start": 246.24, "end": 247.24, "text": " function."}, {"start": 247.24, "end": 254.68, "text": " I'll state it differently, if you want to compute a1 through a10, that is a function"}, {"start": 254.68, "end": 261.24, "text": " of z1 all the way up to z10 simultaneously."}, {"start": 261.24, "end": 264.72, "text": " And this is unlike the other activation functions we've seen so far."}, {"start": 264.72, "end": 270.32, "text": " Finally, let's look at how you would implement this in TensorFlow."}, {"start": 270.32, "end": 276.16, "text": " If you want to implement the neural network that I've shown here on this slide, this is"}, {"start": 276.16, "end": 278.32000000000005, "text": " the code to do so."}, {"start": 278.32000000000005, "end": 282.20000000000005, "text": " Similar as before, there are three steps to specifying and training the model."}, {"start": 282.20000000000005, "end": 288.20000000000005, "text": " The first step is to tell TensorFlow to sequentially string together three layers."}, {"start": 288.20000000000005, "end": 294.48, "text": " First layer is this 25 units with ReLU activation function, second layer, 15 units with ReLU"}, {"start": 294.48, "end": 299.56, "text": " activation function, and then the third layer, because there are now 10 output units, you"}, {"start": 299.56, "end": 304.92, "text": " want to output a1 through a10, so there are now 10 output units, and we'll tell TensorFlow"}, {"start": 304.92, "end": 309.2, "text": " to use the softmax activation function."}, {"start": 309.2, "end": 316.04, "text": " And the cost function that you saw in the last video, TensorFlow calls that the sparse"}, {"start": 316.04, "end": 320.40000000000003, "text": " categorical cross entropy function."}, {"start": 320.40000000000003, "end": 326.0, "text": " So I know this name is a bit of a mouthful, whereas for logistic regression, we had the"}, {"start": 326.0, "end": 328.92, "text": " binary cross entropy function."}, {"start": 328.92, "end": 335.40000000000003, "text": " Here we're using the sparse categorical cross entropy function, and what sparse categorical"}, {"start": 335.40000000000003, "end": 342.20000000000005, "text": " refers to is that you still classify y into categories, so it's categorical, it takes"}, {"start": 342.20000000000005, "end": 349.6, "text": " on values from 1 to 10, and sparse refers to that y can only take on one of these 10"}, {"start": 349.6, "end": 354.76, "text": " values, so each image is either 0 or 1 or 2 or so on up to 9, you're not going to see"}, {"start": 354.76, "end": 360.28, "text": " a picture that is simultaneously the number 2 and the number 7, so sparse refers to that"}, {"start": 360.28, "end": 364.0, "text": " each digit is only one of these categories."}, {"start": 364.0, "end": 369.12, "text": " So that's why the loss function that you saw in the last video is called, in TensorFlow,"}, {"start": 369.12, "end": 373.8, "text": " the sparse categorical cross entropy loss function."}, {"start": 373.8, "end": 379.59999999999997, "text": " And then the code for training the model is just the same as before, and if you use this"}, {"start": 379.6, "end": 385.48, "text": " code, you can train a neural network on a multi-class classification problem."}, {"start": 385.48, "end": 391.08000000000004, "text": " Just one important note, if you use this code exactly as I've written here, it will work,"}, {"start": 391.08000000000004, "end": 396.28000000000003, "text": " but don't actually use this code, because it turns out that in TensorFlow, there's a"}, {"start": 396.28000000000003, "end": 401.16, "text": " better version of the code that makes TensorFlow work better."}, {"start": 401.16, "end": 406.48, "text": " So even though the code shown in this slide works, don't use this code the way I've written"}, {"start": 406.48, "end": 411.56, "text": " it here, because in a later video this week, you'll see a different version, there's actually"}, {"start": 411.56, "end": 416.12, "text": " the recommended version of implementing this that will work better, but we'll take a look"}, {"start": 416.12, "end": 418.70000000000005, "text": " at that in a later video."}, {"start": 418.70000000000005, "end": 425.76, "text": " So now you know how to train a neural network with a softmax output layer with one caveat."}, {"start": 425.76, "end": 430.96000000000004, "text": " There's a different version of the code that will make TensorFlow able to compute these"}, {"start": 430.96000000000004, "end": 433.84000000000003, "text": " probabilities much more accurately."}, {"start": 433.84, "end": 438.32, "text": " Let's take a look at that in the next video, which will also show you the actual code that"}, {"start": 438.32, "end": 442.23999999999995, "text": " I recommend you use if you're training a softmax neural network."}, {"start": 442.24, "end": 464.68, "text": " Let's go on to the next video."}]