13
$\begingroup$

What is the difference between Gradient Descent and Stochastic Gradient Descent?

I am not very familiar with these, can you describe the difference with a short example?

$\endgroup$
19
$\begingroup$

For a quick simple explanation:

In both gradient descent (GD) and stochastic gradient descent (SGD), you update a set of parameters in an iterative manner to minimize an error function.

While in GD, you have to run through ALL the samples in your training set to do a single update for a parameter in a particular iteration, in SGD, on the other hand, you use ONLY ONE or SUBSET of training sample from your training set to do the update for a parameter in a particular iteration. If you use SUBSET, it is called Minibatch Stochastic gradient Descent.

Thus, if the number of training samples are large, in fact very large, then using gradient descent may take too long because in every iteration when you are updating the values of the parameters, you are running through the complete training set. On the other hand, using SGD will be faster because you use only one training sample and it starts improving itself right away from the first sample.

SGD often converges much faster compared to GD but the error function is not as well minimized as in the case of GD. Often in most cases, the close approximation that you get in SGD for the parameter values are enough because they reach the optimal values and keep oscillating there.

If you need an example of this with a practical case, check Andrew NG's notes here where he clearly shows you the steps involved in both the cases. cs229-notes

Source: Quora Thread

$\endgroup$
  • $\begingroup$ thanks, Briefly like this? There are three variants of the Gradient Descent: Batch, Stochastic and Minibatch: Batch updates the weights after all training samples have been evaluated. Stochastic, weights are updated after each training sample. The Minibatch combines the best of both worlds. We do not use the full data set, but we do not use the single data point. We use a randomly selected set of data from our data set. In this way, we reduce the calculation cost and achieve a lower variance than the stochastic version. $\endgroup$ – Engin Aug 7 '18 at 15:50
4
$\begingroup$

In Gradient Descent or Batch Gradient Descent, we use the whole training data per epoch whereas, in Stochastic Gradient Descent, we use only single training example per epoch and Mini-batch Gradient Descent lies in between of these two extremes, in which we can use a mini-batch(small portion) of training data per epoch, thumb rule for selecting the size of mini-batch is in power of 2 like 32, 64, 128 etc.
For more details: cs231n lecture notes

$\endgroup$
  • $\begingroup$ thanks, Briefly like this? There are three variants of the Gradient Descent: Batch, Stochastic and Minibatch: Batch updates the weights after all training samples have been evaluated. Stochastic, weights are updated after each training sample. The Minibatch combines the best of both worlds. We do not use the full data set, but we do not use the single data point. We use a randomly selected set of data from our data set. In this way, we reduce the calculation cost and achieve a lower variance than the stochastic version. $\endgroup$ – Engin Aug 7 '18 at 15:51
3
$\begingroup$

The inclusion of the word stochastic simply means the random samples from the training data are chosen in each run to update parameter during optimisation, within the framework of gradient descent.

Doing so not only computed errors and updates weights in faster iterations (because we only process a small selection of samples in one go), it also often helps to move towards an optimum more quickly. Have a look at the answers here, for more information as to why using stochastic minibatches for training offers advantages.

One perhaps downside, is that the path to the optimum (assuming it would always be the same optimum) can be much noisier. So instead of a nice smooth loss curve, showing how the error descreases in each iteration of gradient descent, you might see something like this:

noisy loss curve

We clearly see the loss decreasing over time, however there are large variations from epoch to epoch (training batch to training batch), so the curve is noisy.

This is simply because we compute the mean error over our stochastically/randomly selected subset, from the entire dataset, in each iteration. Some samples will produce high error, some low. So the average can vary, depending on which samples we randomly used for one iteration of gradient descent.

$\endgroup$
  • $\begingroup$ thanks, Briefly like this? There are three variants of the Gradient Descent: Batch, Stochastic and Minibatch: Batch updates the weights after all training samples have been evaluated. Stochastic, weights are updated after each training sample. The Minibatch combines the best of both worlds. We do not use the full data set, but we do not use the single data point. We use a randomly selected set of data from our data set. In this way, we reduce the calculation cost and achieve a lower variance than the stochastic version. $\endgroup$ – Engin Aug 7 '18 at 15:51
  • $\begingroup$ I'd say there is batch, where a batch is the entire training set (so basically one epoch), then there is mini-batch, where a subset is used (so any number less than the entire set $N$) - this subset is chosen at random, so it is stochastic. Using a single sample would be referred to as online learning, and is a subset of mini-batch... Or simply mini-batch with n=1. $\endgroup$ – n1k31t4 Aug 7 '18 at 19:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.