Okay, so now it's time to run out gradient descent. I'm going to scroll down a little bit, 1
hit the plus sign, add a few cells and this cell I'm going to convert to Markdown and I'm going to call 2
this cell the subheading "Batch Gradient Descent with SymPy". Hit Shift+Enter. Now with gradient descent, 3
the first thing is we're going to do a bit of setup, we're going to provide the initial values for the parameters and we're 4
going to specify these up top. 5
And these were things like the multiplier and the maximum number of iterations. 6
So I'm going to say multiplier = 0.1. 7
I'm going to say the max_iter = 200. 8
And now I'm gonna supply my initial values. 9
So previously, we've had a single variable and we've just said you know "new_x =" 10
and then there was our guess, 1.8 something. 11
But this time we have two values. 12
So how are we gonna do this this time? 13
If you have two or more values, the best data structure to work with is a numpy array. 14
So I'm going to "params = np.array([])" 15
and then I'm going to supply two values - 16
our initial guess for X which is gonna be 1.8 and comma and then our initial guess for our 17
y which is going to be 1.0. 18
And this is our initial guess. 19
So I'm going to put that in a comment in the same line after the Python code. 20
Now one thing that you'll see when we write our loop is just how convenient this is because the params 21
is going to hold the values that we actually care about optimizing. 22
It has our x and our y values and these need to be changed to get to the lowest cost. 23
Previously, we only had to worry about one variable and we were using previous_x and 24
new_x inside our loop, but this time we're gonna switch it up and write it a little differently 25
because we're working with a collection instead. Our loop is going to have a very, very similar syntax as 26
before, we're gonna say "for n in range" and then provide the maximum number of iterations, 27
colon do something. 28
Step 1 29
is gonna be working out the slope. Now, if you remember, the slope that we had up here was "diff(f(a,b), 30
a).evalf", and then substituting a particular value. 31
So this is actually the format we're gonna be using. 32
So what's our gradient gonna be in the x direction? 33
I'm going to create a variable called gradient_x and set it equal to, you guessed it, it's 34
"diff(f(a,b), 35
a)". 36
This is gonna be our cost function differentiated with respect to x. ".evalf", open parentheses and 37
then we're gonna substitute some value. I'm going to say "subs = " and then we gonna provide that 38
dictionary. 39
So curly braces and we're gonna say a is gonna be equal to what, it's gonna be equal to the first element 40
in our parameter array, 41
It's gonna be "params[]", first element is at 0, and then for our b, we're gonna say "b: 42
params[1]". 43
So there's a lot going on in this line here, and this is why we've practiced in the cell above a little 44
bit to make sure we can get our head around all of this nesting of the code, but we're differentiating 45
our cost function with respect to one of the parameters, with respect to a and we're evaluating the slope 46
where we're asking Python to figure out what the value is of the slope at a particular point. 47
Which point is that? It's going to be the values in our parameter array. 48
So params[0] and params[1], the very very first time this loop runs those are gonna be equal to our 49
initial guess. 50
So as a challenge, can you create a variable called "gradient_y" and then complete the rest 51
of the code? 52
Can you write the Python code that calculates the value of our slope in the y direction? 53
I'll give you a few seconds to pause the video and have a go at this. 54
And here's the solution. Again we're using "diff(f(a,b))" 55
and this time we're not gonna be differentiating our cost function with respect to a, we're gonna be 56
differentiating it with respect to b because b is our second parameter and I'm going to put a dot after 57
it, 58
I'm going to "evalf" and I'm going to a substitute the same dictionary, I'm gonna say "a: params 59
[0], b: params[1]". I made a typo in params, 60
so I'm gonna fix that. And this is my finished Python code for our gradients. 61
Now I've split this up a little bit to make it a bit easier to read, but I said earlier what we actually 62
want to do is we want to work with arrays primarily because if you're working with more than one value 63
it's handy to work with numpy arrays. 64
So let's combine these two values into a single numpy array. 65
So I'm going to say "gradients = np.array([gradient_x, 66
gradient_y])". 67
So in these three lines of code we have calculated our cost. 68
We've calculated how far away we are from the minimum based on the steepness of the slope. 69
So the next step is updating our parameters. 70
This is the learning step, the most important part of our loop, right? 71
And because we've written our Python code and used numpy arrays, we can actually work with this in a 72
very, very nice and easy to read manner. 73
We can say our new value of our parameter array, so "params =", it should be equal to our old 74
values, 75
so "params - multiplier * gradients". 76
So we're multiplying our gradients array with the learning rate. 77
So the learning rate times all the slopes 78
is gonna be subtracted from the current values of our parameters. 79
So this is updating them and then we're storing those values inside the parameter array. 80
So we're replacing the values inside the parameters, the old ones with the new ones. So these are the 81
old ones on the right hand side of the equation, 82
we're doing some calculation with them and then we're overwriting those values in our parameter array 83
by updating them. 84
This is what this line of code does. And that's our for loop done, just for lines of code. 85
Now it's time to maybe print out the results, so 86
I'm going to say "# Results", and then we're gonna print out maybe the values in our gradient 87
array, so we can see the values of our slopes. 88
So I'm going to say "'Values in a gradient array', gradients". 89
I'm going to print out the final value of our x, so after the optimization has run, 90
so I'm going to say "Minimum occurs at x value of:" 91
and then where is the final optimized value gonna be stored? 92
Well it's gonna be stored in our parameter array at value 0, first value in our parameter 93
array. 94
And I'm also going to print out the "Minimum occurs at y value of ", and this is gonna be the second value 95
in our parameter array. 96
It's gonna be at index 1. And finally I'm going to print out what the cost is at these x and y values. 97
So I'm going to say "The cost is: " as a string then a comma and then how do I calculate the cost at the final 98
values of our X and Y, 99
the optimized values? I'm going to have to substitute them into our cost function which is f, and then the 100
first parameter is gonna be "params[0]" and then a comma and then "params[ 101
1]". And that's it. 102
Now all I have to do is a check if I've made any typos and then run this cell. 103
See what we get. 104
Here we go. 105
So we can see here that the minimum cost is around 0.5 and that our x and y values are actually 106
very, very small. 107
If we look at our chart up here then we can see that this minimum is probably going to occur where X 108
and Y are equal to zero. 109
And on the z axis we know that this is probably going to be around 1/2. 110
Scrolling back down to our cell here, let's see if this is true. 111
Let's see what happens if I run this cell instead of 200 times maybe 500 times and 112
hit Shift+Enter. I've got wait a little longer this code takes some time to execute and I can see here indeed, 113
our minimum cost is definitely 0.5 and and our x and y values have gotten very, very, 114
very small. So that's really, really cool right? 115
We've been able to keep our mathematics that we had to do by hand to a minimum, having a conceptual understanding 116
of what's going on got us very, very far; and we also saw the power of working with arrays and being able 117
to update a whole bunch of values at once. And, in addition, we learned a little bit about a new data structure, 118
namely dictionaries which associate values with a key. 119
So you have a key value pair. And on that note I think I'm gonna get some more coffee or something. 120
I'll see you in the next lesson. 121
Take care.