#27 Machine Learning Specialization [Course 1, Week 2, Lesson 2]

Plot your training cost against iterations to see if gradient descent is working. The cost should drop each step and flatten out as it converges. You can also set a threshold like 0.001 to stop automatically when improvement slows enough.
Chapters
When running gradient descent, how can you tell if it is converging, that is whether it's helping you to find parameters close to the global minimum of the cost function. By learning to recognize what a well running implementation of gradient descent looks like, we will also, in a later video, be better able to choose a good learning rate alpha. Let's take a look. As a reminder, here's the gradient descent rule and one of the key choices is the choice of the learning rate alpha. Here's something that I often do to make sure that gradient descent is working well. Recall that the job of gradient descent is to find parameters W and B that hopefully minimize the cost function J. So what I'll often do is plot the cost function J, which is calculated on the training set, and I'll plot the value of J at each iteration of gradient descent. Remember that each iteration means after each simultaneous update of the parameters W and B. So in this plot, the horizontal axis is the number of iterations of gradient descent that you've run so far. And so you may get a curve that looks like this. Notice that the horizontal axis is the number of iterations of gradient descent and not a parameter like W or B. This differs from previous graphs you've seen where the vertical axis was cos j and the horizontal axis was a single parameter like W or B. This curve is also called a learning curve. Note that there are a few different types of learning curves used in machine learning, and you'll see some of the types later in this course as well. Concretely, if you look at this point on the curve, this means that after you've run gradient descent for 100 iterations, meaning 100 simultaneous updates of the parameters, you have some learned values for W and V. And if you compute the cost JWB for those values of W and B, the ones you got after 100 iterations, you get this value for the cost J that is this point on the vertical axis and this point here corresponds to the value of J for the parameters that you got after 200 iterations of gradient descent. So looking at this graph helps you to see how your cost j changes after each iteration of gradient descent. If gradient descent is working properly, then the cost J should decrease after every single iteration. If J ever increases after one iteration, that means either alpha is chosen poorly and it usually means alpha is too large, or there could be bug in the code. Another useful thing that this plot can tell you is that if you look at this curve, by the time you reach maybe 300 iterations or so, the Cost J is leveling off and is no longer decreasing much. And by 400 iterations, it looks like the curve has flattened out. So this means that gradient descent has more or less converged because the curve is no longer decreasing. So looking at this learning curve, you can try to spot whether or not gradient descent is converging. By the way, the number of iterations that gradient descent takes to converge can vary a lot between different applications. In one application, it may converge after just 30 iterations. For a different application, it could take 1,000 or 100,000 iterations. It turns out to be very difficult to tell in advance how many iterations gradient descent needs to converge, which is why you can create a graph like this, a learning curve, to try to find out when you can stop training your particular model. Another way to decide when your model is done training is with an automatic convergence test. So let's let epsilon. This here is the Greek Alphabet epsilon. Let's let epsilon be a variable representing a small number, such as 0.001 or 10 to the power of negative 3. If the cost j decreases by less than this number epsilon on one iteration, then you're likely on this flattened part of the curve that you see on the left, and you can declare convergence. Remember, convergence hopefully indicates that you found parameters W and B that are close to the minimum possible value of j. I usually find that choosing the right threshold epsilon is pretty difficult, so I actually tend to look at graphs like this one on the left rather than rely on automatic convergence tests. Looking at this sort of figure can tell you I'll give you some advance warning if maybe gradient descent is not working correctly as well. So you've now seen what the learning curve should look like when gradient descent is running well. Let's take these insights and in the next video, take a look at how to choose an appropriate learning rate.










