Machine Learning Series: Gradient Descent

 



Hello Reader,

 

In this post, I am going to describe the way a machine learning algorithm learns. The secret is an optimization algorithm called Gradient Descent

 

As we have seen in the previous post that was focused on Linear Regression to understand the quality of the predictions during the training phase of a supervised learning algorithm we assign a Cost function, a common choice for linear regression is MSE (Mean Square Error).



The goal of a machine learning algorithm is to tune the values in the parameter vector θ theta to minimize the cost. Minimizing the cost means finding the minimum of the cost function J(θ), to do that Gradient Descent determine the derivative of the function J(θ), the derivative represents the slope of the cost function at that point, gradient descent follows the slope toward the valley in other words toward the minimum.


The picture above shows how the Gradient Descent algorithm works, let's list down the various steps:

  • First, the vector theta is initialized with random values
  • Second, calculate the derivative of J
  • Third update the vector theta subtracting the gradient multiply by hyperparameter alfa also called the learning rate
  • Repeat steps two and three for the number of times defined in the hyperparameter called number of epochs

The derivative of MSE calculated at the second steps looks like this:



The good news is that MSE is a convex function meaning that it has a bowl shape, one global minimum, and no local minimum where gradient descent could be stuck.


Once the derivative is calculated the parameter vector theta is updated with the formula below

Alfa represents the learning rate, in other words, the size of the step toward the minimum. It is crucial to tune this hyperparameter because if it is too big the algorithm will not converge to the minimum while if it is too small the algorithm converges too slowly. Below on the left an example of a small learning rate on the right a large one.



There are three main ways you can implement the Gradient Descent algorithm:

  • Batch Gradient Descent: the derivative is calculated on the entire dataset at each step. The descent to the minimum is stable, but for large datasets, it can slow down the training process.
  • Stochastic Gradient Descent: the derivative is calculated for each instance, a loop through all the instances is called an epoch. In this case, the descent to the minimum is extremely irregular and the algorithm may not converge to the exact minimum but bouncing around it.
  • Mini-Batch Gradient Descent: this is a trade-off between the previous techniques, the training dataset is split into mini-batches and the derivative is calculated as the mean of the mini-batch.

 

Let's get our hands dirty. Let's predict the price of Bitcoin. First, let's get the data




Here is the amazing rush of the Bitcoin price during the last 365 days. We want to train our model to predict the price of tomorrow (t+1) given today's price (t). We are going to shift the labels by one day, the vector Y contains the price of t+1.



In the code above we also split the dataset into train and test. Here is how the variables look like


Let's proceed with a tiny data preparation, we scale our dataset. The scaling operation subtracts the feature by the mean and divide by the standard deviation, the operation center the mean of the features to 0 and transform the cost function to a regular rounded shape which speeds up the convergence of gradient descent.



Now we are ready to create Batch Gradient Descent




The learning rate alfa is set to 0.1 and the number of epochs to 20, let's see visually what the algorithm is doing



The red line represents the parameter vector calculated at each learning iteration, as you can see it regularly converge to the optimal value. Now let's see stochastic gradient descent



In this case we have reduced the learning rate alpha and the number of epochs


The algorithm converges into a point close to the optimal less regularly compared to batch gradient descent. Let's build the third version of the algorithm



I set the batch size to 32 examples, kept the same learning rate to 0.01, and increased the number of epochs to 20, let's how it looks like




Mini batch gradient converge more regularly, but let's compare the three algorithms in another plot that shows the evolution of the vector theta



Let's use the parameter vector theta optimized using Gradient Descent to predict the Bitcoin price.



Here is how the prediction looks like on the test set



Definitively not a good prediction :-)



Recommended resources 

Notebook:

Course:  

Books:


Comments