Explaining Gradient Descent

A classic statistics problem

In machine learning, problems are often framed as optimization problems. For example, let us take one of the simplest applications of supervised learning: the linear regression. It’s conceptually an easy problem - given a set of data points, create a function that best approximates these points. If you ever took statistics or have used excel, this is the line of best fit. Conceptually, this operation doesn’t seem too difficult. Note that this is a supervised learning problem - we need to have a set of observed data to “train” from. We use this data to create the line of best fit, which we can then subsequently use to make predictions given new parameters.

Framing it as a machine learning problem

Defining a linear regression

Let’s rewrite the equation as . We have two weights that we need to define, and , that correspond to the features and . and is kind of a “dummy variable” because we need to fit a linear regression which has the offset , there isn’t any actual observation associated with and it has no measured effect on the final line of best fit.

Our data is defined as a matrix where each row of the matrix represents the readings for the observation. Each row, , is the set of observed variables for a particular observation, which means it must have our feature and the observed output. Note that it is transposed because is a vector with the recorded features for that observation. We have another vector that holds the actual observations for , so the element in corresponds to the row of .

Our objective is to create some function in the form that best approximates the data in and . This means that we need to pick such that best approximates _all* of the data in . So how do we pick the best ?

The loss function

Suppose we randomly guess . We want to know how far off from the actual data we are, we need a quantitative way to determine how wrong we are. We can measure the “wrongness” of our function with something called a loss function. A loss function is a function that provides a measure of your wrongness, and our objective is to minimize it as best as possible. There are several loss functions you can use, a very popular loss function is called mean-squared error (MSE).

We define as follows:

In essence, we are taking every row in , making a prediciton with our function , taking the difference between the actual value and our prediction , then squaring the difference. We do this for every row, then we divide by (the number of data points we have). This gives us the average squared error in the data set, and lets us quantify how good our line of best fit is. Of course, we want to minimize our loss function.

We can find the minimum of a quadratic function by finding the root of the derivation. Note that it has to be a concave down function, otherwise the root will give us the maximum.

Let us define our loss function as and find the minimum.

We have our derived function, solving for will be left as an exercise to the reader.

Gradient Descent

With our defined loss function, how do we figure out how to guess in an iterative manner? We could start with some random vector for and then increment the variables a little, trying to lower with every step until we converge on the minimum (so cannot be minimized any further). This is the intuition behind gradient descent, except gradient descent has a clever way of incrementing the new values so that every time gradient descent iterates, is guaranteed to decrease.

In gradient descent, take our vector where is our current iteration. With each iteration of gradient descent, we set equal to some new value as defined below:

Gradient descent allows us to iteratively minimize the loss function - in other words, this is how we find the value with the least amount of error. Intuitively, imagine a ball rolling down a hill. The hill is drawn by the loss function, and we want to reach the bottom. Because we are subtracting by a ratio defined by the slope of the hill, with a convex function, we should be able to optimize and find the minimum. If we don’t have a strictly concave down function, there is a chance we will not be able to find the minimum. Additionally, note the term. This is the step size, this is a constant that we define which will affect how fast our gradient descent mechanism rolls the ball down the hill. If we keep the step size small, it could take a while until we reach the bottom of the hill, it might also get stuck in a local minima. If the step size is too large, it may oscillate around the minimum but never quite converge.

Optimize for big data (stochastic gradient descent)

Gradient descent can be an expensive process - imagine our is extremely large (this is the definition of big data). If is really big, then every iteration of gradient descent will be extremely expensive. In practice, we must balance the accuracy of our models with computation time. A lot of models that use massive amounts of data mitigate this with a loss function called stochastic gradient descent.

The main intuition behind stochastic gradient descent is that the model is trained and tested on a small subset of the data at a time. This way, you get a rougher approximation of the optimal value(s), but it is much faster. Because gradient descent is an approximation itself, the penalty in accuracy brought on by stochastic gradient descent isn’t big enough to outweigh the performance gains that are brought on by operating on only a batch of the data at a time.

If you’re using stochastic gradient descent, you’ll want to randomly shuffle the data, and perform each iteration on a randomly shuffled mini-batch.