Stochastic Gradient Descent

Published Oct 6, 2023
Contribute to Docs

Stochastic Gradient Descent (SGD) is a variant of the Gradient Descent optimization algorithm, widely used in machine learning to efficiently train models, particularly when dealing with large datasets.

How Does Stochastic Gradient Descent Work?

Similar to Gradient Descent, Stochastic Gradient Descent also aims to minimize the cost function of a model. However, instead of updating the model parameters after evaluating the entire dataset, SGD updates the parameters after each individual data point. This approach makes SGD computationally more efficient, as it avoids the need to process the entire dataset in each iteration.

The following is the pseudocode of the process:

Shuffle Training Data
for each epoch:
    for each mini-batch b:
        Compute Gradient using b
        Update Parameters using the Gradient and Learning Rate

In each iteration of SGD, the following steps are performed:

  1. Shuffle Data: Training data is randomly shuffled before each epoch to introduce randomness and prevent biases in updates.
  2. Mini-Batch Selection: A small subset (mini-batch) of data points is randomly selected from the shuffled training dataset.
  3. Compute Gradient: The gradient of the cost function is computed using the selected mini-batch of data points. The gradient represents the direction of the steepest descent, guiding the algorithm towards the minimum.
  4. Update Parameters: Based on the calculated gradient, the model’s parameters are updated. The learning rate determines the step size of these updates and controls the convergence speed of the algorithm.
  5. Repeat: Steps 2 to 4 are repeated for a specified number of epochs or until a convergence criterion is met.

Uses in Machine Learning

SGD is widely used in machine learning, especially for large-scale tasks like training deep neural networks. Its efficiency in processing large datasets, coupled with the ability to escape local minima, makes it a popular choice in modern machine learning frameworks. Moreover, SGD’s adaptability allows it to be used in online learning settings where new data continuously arrives.

Advantages of Stochastic Gradient Descent

  • Efficiency: SGD processes only a small subset of data in each iteration, making it faster than traditional Gradient Descent for large datasets.
  • Convergence: SGD can escape local minima due to its stochastic nature, improving exploration of the solution space.
  • Noise Tolerance: The inherent randomness in mini-batch selection adds noise, which helps avoid convergence to undesirable solutions.

Challenges of Stochastic Gradient Descent

  • High Variance: The randomness introduced by mini-batch selection may lead to higher variance in parameter updates, resulting in noisy convergence.
  • Learning Rate Tuning: Choosing an appropriate learning rate is crucial, as a large learning rate may cause divergence, while a small one may slow down convergence.
  • Approximate Minima: SGD may not converge to the global minimum due to its stochastic updates.

All contributors

Looking to contribute?

Learn AI on Codecademy