3.5 Cost Function | Simplified Cost Function for Logistic Regression --[Machine Learning|Andrew Ng]
 In the last video, you saw the loss function and the cost function for logistic regression. In this video, you'll see a slightly simpler way to write out the loss and cost functions so that the implementation can be a bit simpler when we get to gradient descent for fitting the parameters of a logistic regression model. Let's take a look. As a reminder, here's the loss function that we had defined in the previous video for logistic regression. Now, because we're still working on the binary classification problem, y is either 0 or 1. Because y is either 0 or 1 and cannot take on any value other than 0 or 1, we'll be able to come up with a simpler way to write this loss function. You can write the loss function as follows. Given the prediction f of x and the target label y, the loss equals negative y times log of f minus 1 minus y times log of 1 minus f. And it turns out this equation, which we just wrote in one line, is completely equivalent to this more complex formula up here. Let's see why this is the case. Now remember, y can only take on the values of either 1 or 0. In the first case, let's say y equals 1. This first y over here is 1, and this 1 minus y is 1 minus 1, which is therefore equal to 0. And so the loss becomes negative 1 times log of f of x minus 0 times a bunch of stuff that becomes 0 and goes away. And so when y is equal to 1, the loss is indeed the first term on top, negative log of f of x. Let's look at the second case, when y is equal to 0. In this case, this y here is equal to 0, so this first term goes away. And the second term is 1 minus 0 times that logarithmic term. So the loss becomes this negative 1 times log of 1 minus f of x. And that's just equal to this second term up here. And so in the case of y equals 0, we also get back the original loss function as defined above. So what you see is that whether y is 1 or 0, the single expression here is equivalent to the more complex expression up here, which is why this gives us a simpler way to write the loss with just one equation without separating out these two cases like we did on top. Using this simplified loss function, let's go back and write out the cost function for logistic regression. So here again is the simplified loss function. And recall that the cost j is just the average loss, average across the entire training set of m examples. So it's 1 over m times the sum of the loss from i equals 1 to m. Now if you plug in a definition for the simplified loss from above, then it looks like this. 1 over m times the sum of this term above. And if you bring the negative signs and move them outside, then you end up with this expression over here. And this is the cost function. The cost function that pretty much everyone uses to train logistic regression. Now you might be wondering, why do we choose this particular function when there could be tons of other cost functions we could have chosen? Although we won't have time to go into great detail on this in this class, I'd just like to mention that this particular cost function is derived from statistics using a statistical principle called maximum likelihood estimation, which is an idea from statistics on how to efficiently find parameters for different models. And this cost function has the nice property that it is convex. But don't worry about learning the details of maximum likelihood. This puts a deeper rationale and justification behind this particular cost function. The upcoming optional lab will show you how the logistic cost function is implemented in code. I recommend taking a look at it because you implement this later in the practice lab at the end of the week. This upcoming optional lab also shows you how two different choices of the parameters will lead to different cost calculations. So you can see in the plot that the better fitting blue decision boundary has a lower cost relative to the magenta decision boundary. So with the simplified cost function, we're now ready to jump into applying gradient descent to logistic regression. Let's go see that in the next video.
[{"start": 0.0, "end": 8.16, "text": " In the last video, you saw the loss function and the cost function for logistic regression."}, {"start": 8.16, "end": 13.9, "text": " In this video, you'll see a slightly simpler way to write out the loss and cost functions"}, {"start": 13.9, "end": 19.0, "text": " so that the implementation can be a bit simpler when we get to gradient descent for fitting"}, {"start": 19.0, "end": 22.56, "text": " the parameters of a logistic regression model."}, {"start": 22.56, "end": 23.76, "text": " Let's take a look."}, {"start": 23.76, "end": 29.8, "text": " As a reminder, here's the loss function that we had defined in the previous video for logistic"}, {"start": 29.8, "end": 30.8, "text": " regression."}, {"start": 30.8, "end": 38.72, "text": " Now, because we're still working on the binary classification problem, y is either 0 or 1."}, {"start": 38.72, "end": 46.32, "text": " Because y is either 0 or 1 and cannot take on any value other than 0 or 1, we'll be able"}, {"start": 46.32, "end": 50.72, "text": " to come up with a simpler way to write this loss function."}, {"start": 50.72, "end": 53.96, "text": " You can write the loss function as follows."}, {"start": 53.96, "end": 61.72, "text": " Given the prediction f of x and the target label y, the loss equals negative y times"}, {"start": 61.72, "end": 70.6, "text": " log of f minus 1 minus y times log of 1 minus f."}, {"start": 70.6, "end": 76.92, "text": " And it turns out this equation, which we just wrote in one line, is completely equivalent"}, {"start": 76.92, "end": 80.88, "text": " to this more complex formula up here."}, {"start": 80.88, "end": 84.08, "text": " Let's see why this is the case."}, {"start": 84.08, "end": 92.19999999999999, "text": " Now remember, y can only take on the values of either 1 or 0."}, {"start": 92.19999999999999, "end": 96.32, "text": " In the first case, let's say y equals 1."}, {"start": 96.32, "end": 105.24, "text": " This first y over here is 1, and this 1 minus y is 1 minus 1, which is therefore equal to"}, {"start": 105.24, "end": 106.64, "text": " 0."}, {"start": 106.64, "end": 115.44, "text": " And so the loss becomes negative 1 times log of f of x minus 0 times a bunch of stuff that"}, {"start": 115.44, "end": 118.88, "text": " becomes 0 and goes away."}, {"start": 118.88, "end": 126.24000000000001, "text": " And so when y is equal to 1, the loss is indeed the first term on top, negative log of f of"}, {"start": 126.24000000000001, "end": 128.52, "text": " x."}, {"start": 128.52, "end": 134.6, "text": " Let's look at the second case, when y is equal to 0."}, {"start": 134.6, "end": 142.12, "text": " In this case, this y here is equal to 0, so this first term goes away."}, {"start": 142.12, "end": 150.24, "text": " And the second term is 1 minus 0 times that logarithmic term."}, {"start": 150.24, "end": 159.24, "text": " So the loss becomes this negative 1 times log of 1 minus f of x."}, {"start": 159.24, "end": 164.76000000000002, "text": " And that's just equal to this second term up here."}, {"start": 164.76000000000002, "end": 171.44, "text": " And so in the case of y equals 0, we also get back the original loss function as defined"}, {"start": 171.44, "end": 173.56, "text": " above."}, {"start": 173.56, "end": 180.68, "text": " So what you see is that whether y is 1 or 0, the single expression here is equivalent"}, {"start": 180.68, "end": 186.44, "text": " to the more complex expression up here, which is why this gives us a simpler way to write"}, {"start": 186.44, "end": 194.4, "text": " the loss with just one equation without separating out these two cases like we did on top."}, {"start": 194.4, "end": 199.0, "text": " Using this simplified loss function, let's go back and write out the cost function for"}, {"start": 199.0, "end": 202.66, "text": " logistic regression."}, {"start": 202.66, "end": 207.36, "text": " So here again is the simplified loss function."}, {"start": 207.36, "end": 214.16, "text": " And recall that the cost j is just the average loss, average across the entire training set"}, {"start": 214.16, "end": 216.48, "text": " of m examples."}, {"start": 216.48, "end": 223.35999999999999, "text": " So it's 1 over m times the sum of the loss from i equals 1 to m."}, {"start": 223.35999999999999, "end": 228.84, "text": " Now if you plug in a definition for the simplified loss from above, then it looks like this."}, {"start": 228.84, "end": 233.76, "text": " 1 over m times the sum of this term above."}, {"start": 233.76, "end": 239.51999999999998, "text": " And if you bring the negative signs and move them outside, then you end up with this expression"}, {"start": 239.51999999999998, "end": 241.12, "text": " over here."}, {"start": 241.12, "end": 243.4, "text": " And this is the cost function."}, {"start": 243.4, "end": 249.12, "text": " The cost function that pretty much everyone uses to train logistic regression."}, {"start": 249.12, "end": 254.48000000000002, "text": " Now you might be wondering, why do we choose this particular function when there could"}, {"start": 254.48000000000002, "end": 258.52, "text": " be tons of other cost functions we could have chosen?"}, {"start": 258.52, "end": 263.38, "text": " Although we won't have time to go into great detail on this in this class, I'd just like"}, {"start": 263.38, "end": 269.88, "text": " to mention that this particular cost function is derived from statistics using a statistical"}, {"start": 269.88, "end": 276.6, "text": " principle called maximum likelihood estimation, which is an idea from statistics on how to"}, {"start": 276.6, "end": 281.0, "text": " efficiently find parameters for different models."}, {"start": 281.0, "end": 286.96, "text": " And this cost function has the nice property that it is convex."}, {"start": 286.96, "end": 290.84, "text": " But don't worry about learning the details of maximum likelihood."}, {"start": 290.84, "end": 298.24, "text": " This puts a deeper rationale and justification behind this particular cost function."}, {"start": 298.24, "end": 303.88, "text": " The upcoming optional lab will show you how the logistic cost function is implemented"}, {"start": 303.88, "end": 305.0, "text": " in code."}, {"start": 305.0, "end": 311.12, "text": " I recommend taking a look at it because you implement this later in the practice lab at"}, {"start": 311.12, "end": 314.18, "text": " the end of the week."}, {"start": 314.18, "end": 319.68, "text": " This upcoming optional lab also shows you how two different choices of the parameters"}, {"start": 319.68, "end": 322.84000000000003, "text": " will lead to different cost calculations."}, {"start": 322.84, "end": 329.0, "text": " So you can see in the plot that the better fitting blue decision boundary has a lower"}, {"start": 329.0, "end": 334.4, "text": " cost relative to the magenta decision boundary."}, {"start": 334.4, "end": 339.88, "text": " So with the simplified cost function, we're now ready to jump into applying gradient descent"}, {"start": 339.88, "end": 342.03999999999996, "text": " to logistic regression."}, {"start": 342.04, "end": 353.68, "text": " Let's go see that in the next video."}]