1.13 Machine Learning Overview | Visualizing the cost function --[Machine Learning | Andrew Ng]
 In the last video, you saw one visualization of the cost function J of w or J of wb. Let's look at some further, richer visualizations so you can get an even better intuition about what the cost function is doing. Here is what we've seen so far. There's the model, the model's parameters w and b, the cost function J of w and b, as well as the goal of linear regression, which is to minimize the cost function J of w and b over parameters w and b. In the last video, we had temporarily set b to zero in order to simplify the visualizations. But now, let's go back to the original model with both parameters w and b without setting b to be equal to zero. Same as last time, we want to get a visual understanding of the model function f of x, shown here on the left, and how it relates to the cost function J of wb, shown here on the right. Here's a training set of house sizes and prices. Let's see pick one possible function of x, like this one. Here I've set w to 0.06 and b to 50. So f of x is 0.06 times x plus 50. Note that this is not a particularly good model for this training set. It's actually a pretty bad model. It seems to consistently underestimate housing prices. Given these values for w and b, let's look at what the cost function J of w and b may look like. Recall what we saw last time was when you had only w because we temporarily set b to zero to simplify things. Back then, we had come up with a plot of the cost function that looked like this as a function of w only. So when we had only one parameter w, the cost function had this u-shaped curve, shaped a bit like a soup bowl. That sounds delicious. Now, in this housing price example that we have on this slide, we have two parameters, w and b, and so the plot becomes a little more complex. It turns out that the cost function also has a similar shape, like a soup bowl, except in three dimensions instead of two. In fact, depending on your training set, the cost function will look something like this. To me, this looks like a soup bowl, maybe because I'm a little bit hungry. Or maybe to you, it looks like a curved dinner plate or a hammock. Actually, that sounds relaxing too. And there's your coconut drink. Maybe when you're done with this course, you should treat yourself to vacation and relax in a hammock like this. What you see here is a 3D surface plot where the axes are labeled w and b. So as you vary w and b, which are the two parameters of the model, you get different values for the cost function j of w and b. This is a lot like the u-shaped curve you saw in the last video, except instead of having one parameter, w, as input in the j, you now have two parameters, w and b, as inputs into this soup bowl or this hammock-shaped function j. And I just want to point out that any single point on the surface represents a particular choice of w and b. For example, if w was minus 10 and b was minus 15, then the height of the surface above this point is the value of j when w is minus 10 and b is minus 15. Now in order to look even more closely at specific points, there's another way of plotting the cost function j that would be useful for visualization, which is rather than using these 3D surface plots. I'd like to take this exact same function j, so I'm not changing the function j at all, and plot it using something called a contour plot. And if you've ever seen a topographical map showing how high different mountains are, the contours in a topographical map are basically horizontal slices of the landscape of, say, a mountain. This image is of Mount Fuji in Japan. I still remember my family visiting Mount Fuji when I was a teenager. It was a beautiful sight. And if you fly directly above the mountain, that's what this contour map looks like. It shows all the points that are at the same height for different heights. At the bottom of this slide is a 3D surface plot of the cost function j. I know it doesn't look very bow-shaped, but it is actually a bow just very stretched out, which is why it looks like that. In an optional lab that is shortly to follow, you'll be able to see this in 3D and spin around the surface yourself, and it'll look more obviously bow-shaped there. Next, here on the upper right is a contour plot of this exact same cost function as that shown at the bottom. The two axes on this contour plot are B on the vertical axis and W on the horizontal axis. What each of these ovals, also called ellipses, shows is the set of points on the 3D surface which are at the exact same height. In other words, the set of points which have the same value for the cost function j. So, to get a contour plot, you take the 3D surface at the bottom and you use a knife to slice it horizontally. You take horizontal slices of that 3D surface and get all the points that are at the same height. Therefore, each horizontal slice ends up being shown as one of these ellipses, or one of these ovals. So concretely, if you take that point, and that point, and that point, all of these three points have the same value for the cost function j, even though they have different values for W and B. And in the figure on the upper left, you see also that these three points correspond to different functions f, all three of which are actually pretty bad for predicting housing prices in this case. Now, the bottom of the bow, where the cost function j is at a minimum, is this point right here, at the center of this concentric ovals. If you haven't seen contour plots much before, I'd like you to imagine, if you will, that you are flying high up above the bow in an airplane or in a rocket ship, and you're looking straight down at it. So that is as if you set your computer monitor flat on your desk facing up, and the bow shape is coming directly out of your screen rising above your desk. Imagine that the bow shape grows out of your computer screen lying flat, like that, so that each of these ovals have the same height above your screen, and the minimum of the bow is right down there in the center of the smallest oval. So it turns out that the contour plots are a convenient way to visualize the 3D cost function j, but in a way that's plotted in just 2D. In this video, you saw how the 3D bow-shaped surface plot can also be visualized as a contour plot. Using this visualization tool, in the next video, let's visualize some specific choices of WMB in the linear regression model so that we can see how these different choices affect the straight line you're fitting to the data. Let's go on to the next video.
[{"start": 0.0, "end": 8.700000000000001, "text": " In the last video, you saw one visualization of the cost function J of w or J of wb."}, {"start": 8.700000000000001, "end": 14.18, "text": " Let's look at some further, richer visualizations so you can get an even better intuition about"}, {"start": 14.18, "end": 16.740000000000002, "text": " what the cost function is doing."}, {"start": 16.740000000000002, "end": 18.86, "text": " Here is what we've seen so far."}, {"start": 18.86, "end": 26.18, "text": " There's the model, the model's parameters w and b, the cost function J of w and b, as"}, {"start": 26.18, "end": 31.96, "text": " well as the goal of linear regression, which is to minimize the cost function J of w and"}, {"start": 31.96, "end": 35.56, "text": " b over parameters w and b."}, {"start": 35.56, "end": 41.68, "text": " In the last video, we had temporarily set b to zero in order to simplify the visualizations."}, {"start": 41.68, "end": 48.64, "text": " But now, let's go back to the original model with both parameters w and b without setting"}, {"start": 48.64, "end": 50.8, "text": " b to be equal to zero."}, {"start": 50.8, "end": 57.48, "text": " Same as last time, we want to get a visual understanding of the model function f of x,"}, {"start": 57.48, "end": 65.28, "text": " shown here on the left, and how it relates to the cost function J of wb, shown here on"}, {"start": 65.28, "end": 66.28, "text": " the right."}, {"start": 66.28, "end": 70.88, "text": " Here's a training set of house sizes and prices."}, {"start": 70.88, "end": 75.08, "text": " Let's see pick one possible function of x, like this one."}, {"start": 75.08, "end": 80.6, "text": " Here I've set w to 0.06 and b to 50."}, {"start": 80.6, "end": 85.91999999999999, "text": " So f of x is 0.06 times x plus 50."}, {"start": 85.91999999999999, "end": 88.96, "text": " Note that this is not a particularly good model for this training set."}, {"start": 88.96, "end": 90.96, "text": " It's actually a pretty bad model."}, {"start": 90.96, "end": 95.39999999999999, "text": " It seems to consistently underestimate housing prices."}, {"start": 95.39999999999999, "end": 101.6, "text": " Given these values for w and b, let's look at what the cost function J of w and b may"}, {"start": 101.6, "end": 102.6, "text": " look like."}, {"start": 102.6, "end": 110.0, "text": " Recall what we saw last time was when you had only w because we temporarily set b to"}, {"start": 110.0, "end": 111.8, "text": " zero to simplify things."}, {"start": 111.8, "end": 118.0, "text": " Back then, we had come up with a plot of the cost function that looked like this as a function"}, {"start": 118.0, "end": 120.48, "text": " of w only."}, {"start": 120.48, "end": 127.08, "text": " So when we had only one parameter w, the cost function had this u-shaped curve, shaped a"}, {"start": 127.08, "end": 128.96, "text": " bit like a soup bowl."}, {"start": 128.96, "end": 130.76, "text": " That sounds delicious."}, {"start": 130.76, "end": 138.12, "text": " Now, in this housing price example that we have on this slide, we have two parameters,"}, {"start": 138.12, "end": 144.20000000000002, "text": " w and b, and so the plot becomes a little more complex."}, {"start": 144.20000000000002, "end": 152.08, "text": " It turns out that the cost function also has a similar shape, like a soup bowl, except"}, {"start": 152.08, "end": 155.04000000000002, "text": " in three dimensions instead of two."}, {"start": 155.04000000000002, "end": 160.96, "text": " In fact, depending on your training set, the cost function will look something like this."}, {"start": 160.96, "end": 165.6, "text": " To me, this looks like a soup bowl, maybe because I'm a little bit hungry."}, {"start": 165.6, "end": 170.6, "text": " Or maybe to you, it looks like a curved dinner plate or a hammock."}, {"start": 170.6, "end": 172.6, "text": " Actually, that sounds relaxing too."}, {"start": 172.6, "end": 175.95999999999998, "text": " And there's your coconut drink."}, {"start": 175.95999999999998, "end": 179.92, "text": " Maybe when you're done with this course, you should treat yourself to vacation and relax"}, {"start": 179.92, "end": 181.76, "text": " in a hammock like this."}, {"start": 181.76, "end": 188.88, "text": " What you see here is a 3D surface plot where the axes are labeled w and b."}, {"start": 188.88, "end": 194.51999999999998, "text": " So as you vary w and b, which are the two parameters of the model, you get different"}, {"start": 194.52, "end": 199.44, "text": " values for the cost function j of w and b."}, {"start": 199.44, "end": 204.56, "text": " This is a lot like the u-shaped curve you saw in the last video, except instead of having"}, {"start": 204.56, "end": 211.96, "text": " one parameter, w, as input in the j, you now have two parameters, w and b, as inputs into"}, {"start": 211.96, "end": 215.84, "text": " this soup bowl or this hammock-shaped function j."}, {"start": 215.84, "end": 220.52, "text": " And I just want to point out that any single point on the surface represents a particular"}, {"start": 220.52, "end": 223.72, "text": " choice of w and b."}, {"start": 223.72, "end": 232.32, "text": " For example, if w was minus 10 and b was minus 15, then the height of the surface above this"}, {"start": 232.32, "end": 240.04, "text": " point is the value of j when w is minus 10 and b is minus 15."}, {"start": 240.04, "end": 245.92, "text": " Now in order to look even more closely at specific points, there's another way of plotting"}, {"start": 245.92, "end": 251.6, "text": " the cost function j that would be useful for visualization, which is rather than using"}, {"start": 251.6, "end": 253.88, "text": " these 3D surface plots."}, {"start": 253.88, "end": 259.76, "text": " I'd like to take this exact same function j, so I'm not changing the function j at all,"}, {"start": 259.76, "end": 263.32, "text": " and plot it using something called a contour plot."}, {"start": 263.32, "end": 268.64, "text": " And if you've ever seen a topographical map showing how high different mountains are,"}, {"start": 268.64, "end": 275.52, "text": " the contours in a topographical map are basically horizontal slices of the landscape of, say,"}, {"start": 275.52, "end": 276.88, "text": " a mountain."}, {"start": 276.88, "end": 280.36, "text": " This image is of Mount Fuji in Japan."}, {"start": 280.36, "end": 284.8, "text": " I still remember my family visiting Mount Fuji when I was a teenager."}, {"start": 284.8, "end": 287.84000000000003, "text": " It was a beautiful sight."}, {"start": 287.84000000000003, "end": 294.0, "text": " And if you fly directly above the mountain, that's what this contour map looks like."}, {"start": 294.0, "end": 300.0, "text": " It shows all the points that are at the same height for different heights."}, {"start": 300.0, "end": 305.28000000000003, "text": " At the bottom of this slide is a 3D surface plot of the cost function j."}, {"start": 305.28, "end": 311.23999999999995, "text": " I know it doesn't look very bow-shaped, but it is actually a bow just very stretched out,"}, {"start": 311.23999999999995, "end": 313.15999999999997, "text": " which is why it looks like that."}, {"start": 313.15999999999997, "end": 318.84, "text": " In an optional lab that is shortly to follow, you'll be able to see this in 3D and spin"}, {"start": 318.84, "end": 323.59999999999997, "text": " around the surface yourself, and it'll look more obviously bow-shaped there."}, {"start": 323.59999999999997, "end": 330.34, "text": " Next, here on the upper right is a contour plot of this exact same cost function as that"}, {"start": 330.34, "end": 332.64, "text": " shown at the bottom."}, {"start": 332.64, "end": 340.2, "text": " The two axes on this contour plot are B on the vertical axis and W on the horizontal"}, {"start": 340.2, "end": 341.64, "text": " axis."}, {"start": 341.64, "end": 349.91999999999996, "text": " What each of these ovals, also called ellipses, shows is the set of points on the 3D surface"}, {"start": 349.91999999999996, "end": 352.15999999999997, "text": " which are at the exact same height."}, {"start": 352.15999999999997, "end": 357.0, "text": " In other words, the set of points which have the same value for the cost function j."}, {"start": 357.0, "end": 365.92, "text": " So, to get a contour plot, you take the 3D surface at the bottom and you use a knife"}, {"start": 365.92, "end": 368.64, "text": " to slice it horizontally."}, {"start": 368.64, "end": 374.34, "text": " You take horizontal slices of that 3D surface and get all the points that are at the same"}, {"start": 374.34, "end": 375.34, "text": " height."}, {"start": 375.34, "end": 382.68, "text": " Therefore, each horizontal slice ends up being shown as one of these ellipses, or one of"}, {"start": 382.68, "end": 385.16, "text": " these ovals."}, {"start": 385.16, "end": 394.12, "text": " So concretely, if you take that point, and that point, and that point, all of these three"}, {"start": 394.12, "end": 401.08000000000004, "text": " points have the same value for the cost function j, even though they have different values"}, {"start": 401.08000000000004, "end": 404.08000000000004, "text": " for W and B."}, {"start": 404.08000000000004, "end": 410.48, "text": " And in the figure on the upper left, you see also that these three points correspond to"}, {"start": 410.48, "end": 416.52000000000004, "text": " different functions f, all three of which are actually pretty bad for predicting housing"}, {"start": 416.52000000000004, "end": 418.36, "text": " prices in this case."}, {"start": 418.36, "end": 427.34000000000003, "text": " Now, the bottom of the bow, where the cost function j is at a minimum, is this point"}, {"start": 427.34000000000003, "end": 432.12, "text": " right here, at the center of this concentric ovals."}, {"start": 432.12, "end": 438.20000000000005, "text": " If you haven't seen contour plots much before, I'd like you to imagine, if you will, that"}, {"start": 438.2, "end": 444.92, "text": " you are flying high up above the bow in an airplane or in a rocket ship, and you're looking"}, {"start": 444.92, "end": 446.84, "text": " straight down at it."}, {"start": 446.84, "end": 453.84, "text": " So that is as if you set your computer monitor flat on your desk facing up, and the bow shape"}, {"start": 453.84, "end": 458.52, "text": " is coming directly out of your screen rising above your desk."}, {"start": 458.52, "end": 465.2, "text": " Imagine that the bow shape grows out of your computer screen lying flat, like that, so"}, {"start": 465.2, "end": 470.8, "text": " that each of these ovals have the same height above your screen, and the minimum of the"}, {"start": 470.8, "end": 477.08, "text": " bow is right down there in the center of the smallest oval."}, {"start": 477.08, "end": 482.84, "text": " So it turns out that the contour plots are a convenient way to visualize the 3D cost"}, {"start": 482.84, "end": 487.96, "text": " function j, but in a way that's plotted in just 2D."}, {"start": 487.96, "end": 495.15999999999997, "text": " In this video, you saw how the 3D bow-shaped surface plot can also be visualized as a contour"}, {"start": 495.16, "end": 496.48, "text": " plot."}, {"start": 496.48, "end": 502.16, "text": " Using this visualization tool, in the next video, let's visualize some specific choices"}, {"start": 502.16, "end": 507.94000000000005, "text": " of WMB in the linear regression model so that we can see how these different choices affect"}, {"start": 507.94000000000005, "end": 510.64000000000004, "text": " the straight line you're fitting to the data."}, {"start": 510.64, "end": 526.28, "text": " Let's go on to the next video."}]