# ML | Stochastic Gradient Descent (SGD)

**What is Gradient Descent?**

Before explaining Stochastic Gradient Descent (SGD), let’s first describe what Gradient Descent is. Gradient Descent is a popular optimization technique in Machine Learning and Deep Learning, and it can be used with most, if not all, of the learning algorithms. A gradient is the slope of a function. It measures the degree of change of a variable in response to the changes of another variable. Mathematically, Gradient Descent is a convex function whose output is the partial derivative of a set of parameters of its inputs. The greater the gradient, the steeper the slope.

Starting from an initial value, Gradient Descent is run iteratively to find the optimal values of the parameters to find the minimum possible value of the given cost function.

**Types of Gradient Descent:**

Typically, there are three types of Gradient Descent:

- Batch Gradient Descent
- Stochastic Gradient Descent
- Mini-batch Gradient Descent

In this article, we will be discussing **Stochastic Gradient Descent** or SGD.

### Stochastic Gradient Descent (SGD):

The word ‘*stochastic*‘ means a system or a process that is linked with a random probability. Hence, in Stochastic Gradient Descent, a few samples are selected randomly instead of the whole data set for each iteration. In Gradient Descent, there is a term called “batch” which denotes the total number of samples from a dataset that is used for calculating the gradient for each iteration. In typical Gradient Descent optimization, like Batch Gradient Descent, the batch is taken to be the whole dataset. Although, using the whole dataset is really useful for getting to the minima in a less noisy and less random manner, but the problem arises when our datasets get big.

Suppose, you have a million samples in your dataset, so if you use a typical Gradient Descent optimization technique, you will have to use all of the one million samples for completing one iteration while performing the Gradient Descent, and it has to be done for every iteration until the minima are reached. Hence, it becomes computationally very expensive to perform.

This problem is solved by Stochastic Gradient Descent. In SGD, it uses only a single sample, i.e., a batch size of one, to perform each iteration. The sample is randomly shuffled and selected for performing the iteration.

**SGD algorithm:**

So, in SGD, we find out the gradient of the cost function of a single example at each iteration instead of the sum of the gradient of the cost function of all the examples.

In SGD, since only one sample from the dataset is chosen at random for each iteration, the path taken by the algorithm to reach the minima is usually noisier than your typical Gradient Descent algorithm. But that doesn’t matter all that much because the path taken by the algorithm does not matter, as long as we reach the minima and with a significantly shorter training time.

The path took by Batch Gradient Descent –

A path has been taken by Stochastic Gradient Descent –

One thing to be noted is that, as SGD is generally noisier than typical Gradient Descent, it usually took a higher number of iterations to reach the minima, because of its randomness in its descent. Even though it requires a higher number of iterations to reach the minima than typical Gradient Descent, it is still computationally much less expensive than typical Gradient Descent. Hence, in most scenarios, SGD is preferred over Batch Gradient Descent for optimizing a learning algorithm.

Pseudocode for SGD in Python:

## Python3

`def` `SGD(f, theta0, alpha, num_iters):` ` ` `"""` ` ` `Arguments:` ` ` `f -- the function to optimize, it takes a single argument` ` ` `and yield two outputs, a cost and the gradient` ` ` `with respect to the arguments` ` ` `theta0 -- the initial point to start SGD from` ` ` `num_iters -- total iterations to run SGD for` ` ` `Return:` ` ` `theta -- the parameter value after SGD finishes` ` ` `"""` ` ` `start_iter ` `=` `0` ` ` `theta ` `=` `theta0` ` ` `for` `iter` `in` `xrange` `(start_iter ` `+` `1` `, num_iters ` `+` `1` `):` ` ` `_, grad ` `=` `f(theta)` ` ` ` ` `# there is NO dot product ! return theta` ` ` `theta ` `=` `theta ` `-` `(alpha ` `*` `grad)` |

This cycle of taking the values and adjusting them based on different parameters in order to reduce the loss function is called **back-propagation**.