5.7 Multiclass Classification  | Softmax --[Machine Learning | Andrew Ng]
 The softmax regression algorithm is a generalization of logistic regression, which is a binary classification algorithm to the multi-cost classification context. Let's take a look at how it works. Recall that logistic regression applies when y can take on two possible output values, either 0 or 1. And the way it computes its output is, you would first calculate z equals w dot product with x plus b, and then you would compute what I'm going to call here a equals g of z, which is a sigmoid function applied to z. And we interpreted this as logistic regression's estimate of the probability of y being equal to 1, given those input features x. Now, quick quiz question. If the probability of y equals 1 is 0.71, then what is the probability that y is equal to 0? Well, the chance of y being 1 and the chance of y being 0, they've got to add up to 1, right? So there's a 71% chance of it being 1. There has to be a 29% or a 0.29 chance of it being equal to 0. So to embellish logistic regression a little bit in order to set us up for the generalization to softmax regression, I'm going to think of logistic regression as actually computing two numbers. First, a 1, which is this quantity that we had previously of the chance of y being equal to 1, given x, and second, I'm going to think of logistic regression as also computing a 2, which is 1 minus this, which is just the chance of y being equal to 0, given the input features x. And so a 1 and a 2, of course, have to add up to 1. Let's now generalize this to softmax regression. And I'm going to do this with a concrete example of when y can take on four possible outputs. So y can take on the values 1, 2, 3, or 4. Here's what softmax regression will do. It will compute z1 as w1 dot product with x plus b1, and then z2 equals w2 dot product with x plus b2, and so on for z3 and z4. Here w1, w2, w3, w4, as well as b1, b2, b3, b4, these are the parameters of softmax regression. Next, here's the formula for softmax regression. We'll compute a1 equals e to the z1 divided by e to the z1 plus e to the z2 plus e to the z3 plus e to the z4. And a1 will be interpreted as the average estimate of the chance of y being equal to 1, given the input features x. Then the formula for softmax regression will compute a2 equals e to the z2 divided by the same denominator, e to the z1 plus e to the z2 plus e to the z3 plus e to the z4, and will interpret a2 as the average estimate of the chance that y is equal to 2, given the input features x. And similarly for a3, where here the numerator is now e to the z3 divided by the same denominator, that's the estimated chance of y being equal to 3, and similarly a4 takes on this expression. Whereas on the left, we wrote down the specification for the logistic regression model, these equations on the right are our specification for the softmax regression model. It has parameters w1 through w4 and b1 through b4, and if you can learn appropriate choices for all these parameters, then this gives you a way of predicting what's the chance of y being 1, 2, 3 or 4, given a set of input features x. Quick quiz, let's say you run softmax regression on a new input x, and you find that a1 is 0.30, a2 is 0.20, a3 is 0.15. What do you think a4 will be? Why don't you take a look at this quiz and see if you can figure out the right answer. So you might have realized that because the chance of y taking on the values of 1, 2, 3 or 4, they have to add up to 1, a4, the chance of y being equal to 4, has to be 0.15. So 0.35, which is 1 minus 0.3 minus 0.2 minus 0.15. So here I wrote down the formulas for softmax regression in the case of 4 possible outputs, and let's now write down the formula for the general case for softmax regression. In the general case, y can take on n possible values, so y can be 1, 2, 3 and so on up to n. In that case, softmax regression will compute zj equals wj dot product with x plus bj, where now the parameters of softmax regression are w1, w2 through wn, as well as b1, b2 through bn. And then finally it will compute aj equals e to the zj divided by sum from k equals 1 to n of e to the z sub k. Well here I'm using another variable k to index the summation, because here j refers to a specific fixed number like j equals 1. aj is interpreted as the model's estimate that y is equal to j given the input features x. And notice that by construction of this formula, if you add up a1, a2 all the way through an, these numbers always will end up adding up to 1. So we specified how you would compute the softmax regression model. And I won't prove it in this video, but it turns out that if you apply softmax regression with n equals 2, so there are only two possible output classes, then softmax regression ends up computing basically the same thing as logistic regression. The parameters end up being a little bit different, but it ends up reducing to a logistic regression model. But that's why the softmax regression model is a generalization of logistic regression. Having defined how softmax regression computes its outputs, let's now take a look at how to specify the cost function for softmax regression. Recall for logistic regression, this is what we had. We said z is equal to this, and then I wrote earlier that a1 is g of z, it was interpreted as the probability that y is equal to 1. And we also wrote a2 is the probability that y is equal to clause 0. So previously we had written the loss of logistic regression as negative y log a1 minus 1 minus y log 1 minus a1. But 1 minus a1 is also equal to just a2, because a2 is 1 minus a1 according to this expression over here. So I can rewrite or simplify the loss for logistic regression a little bit to be negative y log a1 minus 1 minus y log of a2. And in other words, the loss if y is equal to 1 is negative log a1, and if y is equal to 0, then the loss is negative log a2. And then same as before, the cost function for all the parameters in the model is the average loss, average over the entire training set. So that was the cost function for logistic regression. Let's write down the cost function that is conventionally used for softmax regression. Recall that these are the equations we use for softmax regression. The loss we're going to use for softmax regression is just this. The loss for if the algorithm outputs a1 through an, and the ground truth label is y, is if y equals 1, the loss is negative log a1, so it's negative log of the probability that it thought y was equal to 1. Or if y is equal to 2, then the loss I'm going to define as negative log a2. So if y is equal to 2, the loss of the algorithm on this example is negative log of the probability it thought y was equal to 2. And so on all the way down to if y is equal to n, then the loss is negative log of an. And to illustrate what this is doing, if y is equal to j, then the loss is negative log of aj, and that's what this function looks like. Negative log of aj is a curve that looks like this. And so if aj was very close to 1, then you'd be on this part of the curve and the loss would be very small. But if it thought, say, aj had only a 50% chance, then the loss gets a little bit bigger. And the smaller aj is, the bigger the loss. And so this incentivizes the algorithm to make aj as large as possible, as close to 1 as possible, because whatever the actual value y was, you want the algorithm to say, hopefully, that the chance of y being that value was pretty large. Notice that in this loss function, y in each training example can take on only one value. And so you end up computing this negative log of aj only for one value of aj, which is whatever was the actual value of y equals j in that particular training example. For example, if y was equal to 2, you end up computing negative log of a2, but not any of the other negative log of a1 or the other terms here. So that's the form of the model, as well as the cost function for softmax regression. And if you were to train this model, you can start to build multi-class classification algorithms. And what we'd like to do next is take this softmax regression model and fit it into a neural network so that you're going to do something even better, which is to train a neural network for multi-class classification. Let's go through that in the next video.
[{"start": 0.0, "end": 8.48, "text": " The softmax regression algorithm is a generalization of logistic regression, which is a binary"}, {"start": 8.48, "end": 13.0, "text": " classification algorithm to the multi-cost classification context."}, {"start": 13.0, "end": 16.2, "text": " Let's take a look at how it works."}, {"start": 16.2, "end": 23.48, "text": " Recall that logistic regression applies when y can take on two possible output values,"}, {"start": 23.48, "end": 26.44, "text": " either 0 or 1."}, {"start": 26.44, "end": 32.56, "text": " And the way it computes its output is, you would first calculate z equals w dot product"}, {"start": 32.56, "end": 39.04, "text": " with x plus b, and then you would compute what I'm going to call here a equals g of"}, {"start": 39.04, "end": 43.400000000000006, "text": " z, which is a sigmoid function applied to z."}, {"start": 43.400000000000006, "end": 49.28, "text": " And we interpreted this as logistic regression's estimate of the probability of y being equal"}, {"start": 49.28, "end": 52.08, "text": " to 1, given those input features x."}, {"start": 52.08, "end": 57.199999999999996, "text": " Now, quick quiz question."}, {"start": 57.199999999999996, "end": 66.64, "text": " If the probability of y equals 1 is 0.71, then what is the probability that y is equal"}, {"start": 66.64, "end": 68.08, "text": " to 0?"}, {"start": 68.08, "end": 74.16, "text": " Well, the chance of y being 1 and the chance of y being 0, they've got to add up to 1,"}, {"start": 74.16, "end": 75.16, "text": " right?"}, {"start": 75.16, "end": 77.36, "text": " So there's a 71% chance of it being 1."}, {"start": 77.36, "end": 85.16, "text": " There has to be a 29% or a 0.29 chance of it being equal to 0."}, {"start": 85.16, "end": 90.2, "text": " So to embellish logistic regression a little bit in order to set us up for the generalization"}, {"start": 90.2, "end": 95.28, "text": " to softmax regression, I'm going to think of logistic regression as actually computing"}, {"start": 95.28, "end": 96.52, "text": " two numbers."}, {"start": 96.52, "end": 103.76, "text": " First, a 1, which is this quantity that we had previously of the chance of y being equal"}, {"start": 103.76, "end": 109.64, "text": " to 1, given x, and second, I'm going to think of logistic regression as also computing a"}, {"start": 109.64, "end": 120.80000000000001, "text": " 2, which is 1 minus this, which is just the chance of y being equal to 0, given the input"}, {"start": 120.80000000000001, "end": 121.80000000000001, "text": " features x."}, {"start": 121.80000000000001, "end": 126.84, "text": " And so a 1 and a 2, of course, have to add up to 1."}, {"start": 126.84, "end": 131.08, "text": " Let's now generalize this to softmax regression."}, {"start": 131.08, "end": 137.72000000000003, "text": " And I'm going to do this with a concrete example of when y can take on four possible outputs."}, {"start": 137.72000000000003, "end": 143.0, "text": " So y can take on the values 1, 2, 3, or 4."}, {"start": 143.0, "end": 145.72000000000003, "text": " Here's what softmax regression will do."}, {"start": 145.72000000000003, "end": 155.04000000000002, "text": " It will compute z1 as w1 dot product with x plus b1, and then z2 equals w2 dot product"}, {"start": 155.04000000000002, "end": 160.20000000000002, "text": " with x plus b2, and so on for z3 and z4."}, {"start": 160.2, "end": 170.56, "text": " Here w1, w2, w3, w4, as well as b1, b2, b3, b4, these are the parameters of softmax regression."}, {"start": 170.56, "end": 175.56, "text": " Next, here's the formula for softmax regression."}, {"start": 175.56, "end": 184.32, "text": " We'll compute a1 equals e to the z1 divided by e to the z1 plus e to the z2 plus e to"}, {"start": 184.32, "end": 187.16, "text": " the z3 plus e to the z4."}, {"start": 187.16, "end": 193.68, "text": " And a1 will be interpreted as the average estimate of the chance of y being equal to"}, {"start": 193.68, "end": 196.4, "text": " 1, given the input features x."}, {"start": 196.4, "end": 205.76, "text": " Then the formula for softmax regression will compute a2 equals e to the z2 divided by the"}, {"start": 205.76, "end": 211.12, "text": " same denominator, e to the z1 plus e to the z2 plus e to the z3 plus e to the z4, and"}, {"start": 211.12, "end": 216.84, "text": " will interpret a2 as the average estimate of the chance that y is equal to 2, given"}, {"start": 216.84, "end": 218.44, "text": " the input features x."}, {"start": 218.44, "end": 225.96, "text": " And similarly for a3, where here the numerator is now e to the z3 divided by the same denominator,"}, {"start": 225.96, "end": 233.04, "text": " that's the estimated chance of y being equal to 3, and similarly a4 takes on this expression."}, {"start": 233.04, "end": 240.5, "text": " Whereas on the left, we wrote down the specification for the logistic regression model, these equations"}, {"start": 240.5, "end": 246.6, "text": " on the right are our specification for the softmax regression model."}, {"start": 246.6, "end": 254.12, "text": " It has parameters w1 through w4 and b1 through b4, and if you can learn appropriate choices"}, {"start": 254.12, "end": 258.94, "text": " for all these parameters, then this gives you a way of predicting what's the chance"}, {"start": 258.94, "end": 265.68, "text": " of y being 1, 2, 3 or 4, given a set of input features x."}, {"start": 265.68, "end": 271.68, "text": " Quick quiz, let's say you run softmax regression on a new input x, and you find that a1 is"}, {"start": 271.68, "end": 280.76, "text": " 0.30, a2 is 0.20, a3 is 0.15."}, {"start": 280.76, "end": 282.8, "text": " What do you think a4 will be?"}, {"start": 282.8, "end": 289.56, "text": " Why don't you take a look at this quiz and see if you can figure out the right answer."}, {"start": 289.56, "end": 294.96000000000004, "text": " So you might have realized that because the chance of y taking on the values of 1, 2,"}, {"start": 294.96000000000004, "end": 301.6, "text": " 3 or 4, they have to add up to 1, a4, the chance of y being equal to 4, has to be 0.15."}, {"start": 301.6, "end": 307.72, "text": " So 0.35, which is 1 minus 0.3 minus 0.2 minus 0.15."}, {"start": 307.72, "end": 315.04, "text": " So here I wrote down the formulas for softmax regression in the case of 4 possible outputs,"}, {"start": 315.04, "end": 321.28000000000003, "text": " and let's now write down the formula for the general case for softmax regression."}, {"start": 321.28000000000003, "end": 328.32000000000005, "text": " In the general case, y can take on n possible values, so y can be 1, 2, 3 and so on up to"}, {"start": 328.32000000000005, "end": 329.72, "text": " n."}, {"start": 329.72, "end": 338.68, "text": " In that case, softmax regression will compute zj equals wj dot product with x plus bj, where"}, {"start": 338.68, "end": 348.04, "text": " now the parameters of softmax regression are w1, w2 through wn, as well as b1, b2 through"}, {"start": 348.04, "end": 349.76000000000005, "text": " bn."}, {"start": 349.76, "end": 362.56, "text": " And then finally it will compute aj equals e to the zj divided by sum from k equals 1 to n of e to the z sub k."}, {"start": 362.56, "end": 369.2, "text": " Well here I'm using another variable k to index the summation, because here j refers"}, {"start": 369.2, "end": 372.64, "text": " to a specific fixed number like j equals 1."}, {"start": 372.64, "end": 380.0, "text": " aj is interpreted as the model's estimate that y is equal to j given the input features"}, {"start": 380.0, "end": 381.0, "text": " x."}, {"start": 381.0, "end": 387.76, "text": " And notice that by construction of this formula, if you add up a1, a2 all the way through an,"}, {"start": 387.76, "end": 390.96, "text": " these numbers always will end up adding up to 1."}, {"start": 390.96, "end": 396.47999999999996, "text": " So we specified how you would compute the softmax regression model."}, {"start": 396.47999999999996, "end": 401.28, "text": " And I won't prove it in this video, but it turns out that if you apply softmax regression"}, {"start": 401.28, "end": 408.28, "text": " with n equals 2, so there are only two possible output classes, then softmax regression ends"}, {"start": 408.28, "end": 413.03999999999996, "text": " up computing basically the same thing as logistic regression."}, {"start": 413.03999999999996, "end": 417.79999999999995, "text": " The parameters end up being a little bit different, but it ends up reducing to a logistic regression"}, {"start": 417.79999999999995, "end": 418.79999999999995, "text": " model."}, {"start": 418.79999999999995, "end": 424.64, "text": " But that's why the softmax regression model is a generalization of logistic regression."}, {"start": 424.64, "end": 429.71999999999997, "text": " Having defined how softmax regression computes its outputs, let's now take a look at how"}, {"start": 429.72, "end": 434.84000000000003, "text": " to specify the cost function for softmax regression."}, {"start": 434.84000000000003, "end": 437.52000000000004, "text": " Recall for logistic regression, this is what we had."}, {"start": 437.52000000000004, "end": 445.36, "text": " We said z is equal to this, and then I wrote earlier that a1 is g of z, it was interpreted"}, {"start": 445.36, "end": 447.88000000000005, "text": " as the probability that y is equal to 1."}, {"start": 447.88000000000005, "end": 454.96000000000004, "text": " And we also wrote a2 is the probability that y is equal to clause 0."}, {"start": 454.96, "end": 463.59999999999997, "text": " So previously we had written the loss of logistic regression as negative y log a1 minus 1 minus"}, {"start": 463.59999999999997, "end": 467.28, "text": " y log 1 minus a1."}, {"start": 467.28, "end": 477.2, "text": " But 1 minus a1 is also equal to just a2, because a2 is 1 minus a1 according to this expression"}, {"start": 477.2, "end": 479.09999999999997, "text": " over here."}, {"start": 479.1, "end": 484.92, "text": " So I can rewrite or simplify the loss for logistic regression a little bit to be negative"}, {"start": 484.92, "end": 491.28000000000003, "text": " y log a1 minus 1 minus y log of a2."}, {"start": 491.28000000000003, "end": 500.12, "text": " And in other words, the loss if y is equal to 1 is negative log a1, and if y is equal"}, {"start": 500.12, "end": 505.62, "text": " to 0, then the loss is negative log a2."}, {"start": 505.62, "end": 509.88, "text": " And then same as before, the cost function for all the parameters in the model is the"}, {"start": 509.88, "end": 513.94, "text": " average loss, average over the entire training set."}, {"start": 513.94, "end": 517.44, "text": " So that was the cost function for logistic regression."}, {"start": 517.44, "end": 525.08, "text": " Let's write down the cost function that is conventionally used for softmax regression."}, {"start": 525.08, "end": 530.0, "text": " Recall that these are the equations we use for softmax regression."}, {"start": 530.0, "end": 534.48, "text": " The loss we're going to use for softmax regression is just this."}, {"start": 534.48, "end": 546.52, "text": " The loss for if the algorithm outputs a1 through an, and the ground truth label is y, is if"}, {"start": 546.52, "end": 552.72, "text": " y equals 1, the loss is negative log a1, so it's negative log of the probability that"}, {"start": 552.72, "end": 556.36, "text": " it thought y was equal to 1."}, {"start": 556.36, "end": 563.02, "text": " Or if y is equal to 2, then the loss I'm going to define as negative log a2."}, {"start": 563.02, "end": 569.9399999999999, "text": " So if y is equal to 2, the loss of the algorithm on this example is negative log of the probability"}, {"start": 569.9399999999999, "end": 572.6, "text": " it thought y was equal to 2."}, {"start": 572.6, "end": 580.56, "text": " And so on all the way down to if y is equal to n, then the loss is negative log of an."}, {"start": 580.56, "end": 590.1, "text": " And to illustrate what this is doing, if y is equal to j, then the loss is negative log"}, {"start": 590.1, "end": 595.16, "text": " of aj, and that's what this function looks like."}, {"start": 595.16, "end": 599.86, "text": " Negative log of aj is a curve that looks like this."}, {"start": 599.86, "end": 606.24, "text": " And so if aj was very close to 1, then you'd be on this part of the curve and the loss"}, {"start": 606.24, "end": 608.36, "text": " would be very small."}, {"start": 608.36, "end": 614.72, "text": " But if it thought, say, aj had only a 50% chance, then the loss gets a little bit bigger."}, {"start": 614.72, "end": 619.36, "text": " And the smaller aj is, the bigger the loss."}, {"start": 619.36, "end": 625.76, "text": " And so this incentivizes the algorithm to make aj as large as possible, as close to"}, {"start": 625.76, "end": 630.92, "text": " 1 as possible, because whatever the actual value y was, you want the algorithm to say,"}, {"start": 630.92, "end": 635.82, "text": " hopefully, that the chance of y being that value was pretty large."}, {"start": 635.82, "end": 642.98, "text": " Notice that in this loss function, y in each training example can take on only one value."}, {"start": 642.98, "end": 650.44, "text": " And so you end up computing this negative log of aj only for one value of aj, which"}, {"start": 650.44, "end": 655.58, "text": " is whatever was the actual value of y equals j in that particular training example."}, {"start": 655.58, "end": 661.28, "text": " For example, if y was equal to 2, you end up computing negative log of a2, but not any"}, {"start": 661.28, "end": 664.7, "text": " of the other negative log of a1 or the other terms here."}, {"start": 664.7, "end": 670.48, "text": " So that's the form of the model, as well as the cost function for softmax regression."}, {"start": 670.48, "end": 675.36, "text": " And if you were to train this model, you can start to build multi-class classification"}, {"start": 675.36, "end": 677.24, "text": " algorithms."}, {"start": 677.24, "end": 682.16, "text": " And what we'd like to do next is take this softmax regression model and fit it into a"}, {"start": 682.16, "end": 687.12, "text": " neural network so that you're going to do something even better, which is to train a"}, {"start": 687.12, "end": 690.48, "text": " neural network for multi-class classification."}, {"start": 690.48, "end": 701.16, "text": " Let's go through that in the next video."}]