Hi everybody!
Welcome to the second episode of AI Development 101.
In this one we are going to see
an incredibly important concept
behind machine learning,
the gradient descent.
Or as you'll see in a second,
how to hike in the mountains in a foggy day,
math edition. You'll see that in a minute.
Remember that you can follow me here
for more videos like these
or the equivalent for AI in Business 101.
So, we ended the previous episode
with a big problem that we found.
We were doing the most basic operation
in machine learning, a linear regression.
A linear regression tries to find a linear function
theta 0 plus theta 1 x
that minimizes a cost function.
A cost function measures how bad my model is,
And it is calculated with the distances
between my predictions
and the real outputs from the examples,
so these red segments that you see here.
Let's say that for this particular pair of theta 0 and theta 1
the value of the cost function is J.
We saw that finding the perfect line
that minimizes this number J
was relatively easy if we have only a few dimensions
and a few samples,
but it could be very challenging
for more complex scenarios with more features,
or even impossible, directly impossible
for other machine learning algorithms.
So, what can we do? A valid approach would be to just try
different combinations of theta 0 and theta 1
and see which one creates the lowest J.
For example, let's just use some other random combination
theta 0 prime and theta 1 prime.
That combination will create a different line,
and I can now calculate the distances again
and get a new J which we will call J prime.
I could do that with a lot of combinations
until I find one that is small enough for me.
That is of course a horrible solution,
even less efficient than just applying
the closed equation that we saw before,
but we may be into something.
Let's imagine that I do that random approach
several times. Let's see what happens.
What I'm going to do is to represent
the numbers that I get in a 3D chart.
These two axes are going to be theta 0 and theta 1.
The height is going to be the cost function.
So I'm going to take a couple of random theta values
and I'm going to apply the cost function.
If you remember from the last video,
the cost function that we were using
was the average of the squared distances,
so 1 divided by n multiplied by the summation
of the squared distances
for each sample in my dataset.
The predicted y would depend
on the parameters theta 0 and theta 1,
so this formula depends on
all the data in my training dataset,
and the parameters theta 0 and theta 1.
Imagine that I apply the formula with my data
and with that random theta 0, theta 1 that I took before.
Let's plot the result J in this 3D chart.
Imagine that it's this one here.
Let's do it for another one,
I can pick another random pair of theta 0 and theta 1,
and get in this case this other J.
If I keep doing this, at the end I'm going to get a 3D shape
that is going to look something like this.
Let me try to represent this in 3D.
It's going to look like a kind of a bowl of soup.
So in this case the shape is actually quite simple
and it's easy to visualize,
but for other functions it may look very complex,
with multiple mountains, and multiple valleys,
and even it may have more than 2 dimensions
for models of more than two parameters
so it's impossible to represent in a chart like this.
But here's the interesting thing.
Maybe now that I have seen this,
I can come up with a better way
to find the minimum of this function,
instead of just trying random values of theta 0 and theta 1.
Imagine that the first try gives me this point.
In the next try what I could do
is just go in this direction,
instead of taking another random point.
It's like, imagine that you are in a mountain,
and it's all foggy, you can't see anything,
just one meter around you.
A good idea if you want to go down the mountain
is to take steps in the direction that goes down, right?
So every step I'm going to see the value of the cost function
and the direction of the slope,
and I'm going to take my next step in that direction.
So how do I do this in Math?
If you paid attention in high school you will probably remember…
The Math operation to get that rate of a slope
in a certain direction is a derivative.
So what I can do is to take my function J,
this one that you see here,
and calculate the derivative for both theta zero and theta 1.
I'm not going to do that Math
but you can find it anywhere
So if you are interested so can look it online.
Here's the final result,
the first equation is the derivative with respect to theta 0
and the second one with respect to theta 1.
The resulting vector will go in the positive direction of the slope of that particular point,
So if I turn it around it would go down the mountain, like this.
So now I can repeat that process for the next point,
and then move again in that direction,
and again, and again
until I get to the minimum point
or I think that I'm close enough.
That process is called gradient descent,
and it's the absolutely foundation of machine learning.
You are going to use it everywhere,
from this simple linear regression
to the most complex neural networks you can think of.
Let me put it now in steps
so it's easier to understand.
This process is called training.
We are going to start with random values
for the parameters of my model.
Then we are going to evaluate the derivative of our cost function,
let's call it J prime.
That's basically the two formulas we saw before,
which depend on the previous thetas,
and the samples x and y that I'm using to train my model.
That will give me that vector in the direction that I want to move.
Let me show this in a simplified version of our chart.
Imagine that I'm here,
the direction will be this one here.
So now we update my parameters
theta 0 and theta 1 in that direction.
But how much do I move in that direction?
That's another important number.
If I move just a little bit,
then it's going to take me a lot to get the base.
But if I move a lot, I may be jumping back and forth
and it will also take me a long time
to get to the base of the mountain.
So we'll call that number with the amount
that I'm going to be moving in every step,
the Learning Rate, and it's another parameter
that you will need to decide for your training.
You are going to see it in every algorithm,
and usually it's referred with the symbol alpha.
So now that you are going in that direction
you are going to repeat these steps a number of times.
How many times?
You have to decide that too, so that's completely up to you.
We are going to call that number,
the number of times that you are going to do this process,
the number of epochs.
So an epoch is one iteration in this loop
where you update your model parameters
with all your dataset.
You are going to find this loop
in every single model training in machine learning.
But I want you to be aware,
you are going to see many times
something called stochastic gradient descent.
Don't freak out when you see that,
because it's very similar to this.
The stochastic gradient descent is a simplification
of this process to make it more efficient.
The only difference is that instead of using all the samples
to calculate the derivative,
it's going to use just one sample, a random one,
In other cases we are going to use several samples,
and we are going to call those samples a batch.
This inner loop for each sample or each batch
it's going to be called an iteration.
So summarizing an iteration means updating my model
with a subset of my data.
And then an epoch will happen when I cover my entire dataset,
so an epoch is a collection of iterations
that I'm using to cover all my data.
In real life you are going to use a lot of epochs to train a model,
and each of them with a lot of iterations in smaller batches.
Wow, this was an intense video
but I can assure you that understanding the gradient descent
is the most important thing
that you have to do to become an AI developer.
So from here it's all a descent.
My jokes are getting worse and worse in every video.
I'm sorry about that.
So from here we are going to start exploring more complex models,
all the way to artificial neural networks
with billions of parameters.
Remember you can watch these videos
and the equivalent for business at aka.ms/ai101,
and you can follow me in LinkedIn at aka.ms/davidc.
Thank you for watching!