Stochastic Gradient Descent: A Basic Explanation

Basic Explanation of SGD

Mohit Mishra
6 min readJun 4, 2023

“SGD is like a drunkard trying to find his way home.” — Geoffrey Hinton

Introduction

Stochastic Gradient Descent (SGD) is an effective and popular optimization algorithm for machine learning. Its key strength is its ability to process large datasets and reach convergence quickly. It also has high computational efficiency due to the fact that the gradient can be estimated with a random sample of data points instead of requiring the full dataset.

Unlike traditional Gradient Descent, SGD relies on a single example or samples at each iteration, introducing randomness into the learning process. This makes your model learn faster as it converges quicker than other optimization methods such as Mini-Batch or Batch Gradient Descent. When applied to models with thousands of features, SGD performs extremely well because of its significant computational speed and accuracy.

Credits

Overall, Stochastic Gradient Descent presents an excellent opportunity for machine learning engineers to simplify their training process and improve model performance significantly.

“SGD is the workhorse of machine learning.” — Yoshua Bengio

What was the need for SGD?

There was a need for SGD (Stochastic Gradient Descent) in ML (Machine Learning) because of the following reasons:

1. Large datasets: In ML, it is common to have very large datasets with millions or even billions of data points. Using traditional gradient descent algorithms on such large datasets is computationally expensive and impractical. In such cases, SGD works well because it uses randomly selected data points to update the model parameters, making the process faster and more efficient.

2. Non-Convex optimization problems: In many ML problems, the objective function is not convex, which means there are multiple local minima. Traditional gradient descent algorithms may get stuck in one of the local minima, resulting in suboptimal models. SGD, on the other hand, is less likely to get stuck because it updates the parameters using only a few data points at a time, making it more likely to find the global minimum.

3. Online learning: In some ML applications, new data is constantly being generated and added to the dataset. In such cases, it is necessary to update the model parameters in real-time, as new data becomes available. SGD is well-suited for such tasks because it updates the model parameters using small batches of data and can be applied to data as it arrives.

Overall, SGD is a more efficient and practical alternative to traditional gradient descent algorithms in many ML applications, particularly for large datasets, non-convex optimization problems, and online learning.

The Math Behind SGD

SGD works by iteratively updating the model parameters in the direction of the negative gradient of the loss function. The gradient of the loss function is a vector that points in the direction of the steepest ascent of the loss function. By moving in the direction of the negative gradient, SGD is able to find the minimum of the loss function.

The loss function is a measure of how well the model fits the data. The goal of SGD is to find the set of model parameters that minimizes the loss function.

The gradient of the loss function can be calculated using the following formula:

gradient = ∂L/∂θ

where L is the loss function and θ is the vector of model parameters.

The gradient can be used to update the model parameters using the following formula:

θ = θ - η * gradient

where η is the learning rate. The learning rate is a hyperparameter that controls how much the model parameters are updated each iteration.

Stochastic Gradient Descent vs. Gradient Descent

Here are some of the benefits of using SGD over GD:

  • Speed: SGD is much faster than GD, especially when the dataset is large. This is because SGD only updates the model parameters after each individual data point, while GD updates the parameters after each batch of data points.
  • Robustness to noise: SGD is more robust to noise in the data than GD. This is because SGD only uses a single data point to update the parameters, so it is less likely to be affected by outliers or noise in the data.
  • Efficiency: SGD is more efficient than GD in terms of memory usage. This is because SGD only needs to store the current parameters and the gradient of the loss function, while GD needs to store the entire dataset.
Credits

Here is a mathematical proof that shows that SGD converges to the same minimum as GD, but at a slower rate. Let f(x) be a differentiable function and let x0​ be an initial guess for the minimum of f(x). GD updates the parameters as follow

x_{n+1} = x_n - \eta \nabla f(x_n)

where η is the learning rate. SGD updates the parameters as follows:

x_{n+1} = x_n - \eta \nabla f(x_i)

where i is a randomly chosen index from the dataset.

It can be shown that both GD and SGD converge to the same minimum of f(x), but SGD converges at a slower rate. This is because SGD only uses a single data point to update the parameters, while GD uses the entire dataset.

Here are some statistical facts about SGD:

  • SGD is a stochastic algorithm, which means that it is not guaranteed to converge to the same minimum every time it is run. However, it is typically very close to the minimum after a large number of iterations.
  • The learning rate η is a hyperparameter that controls the speed of convergence. A smaller learning rate will converge more slowly, but it will be more stable. A larger learning rate will converge more quickly, but it may be more likely to diverge.
  • The choice of batch size can also affect the speed of convergence. A smaller batch size will converge more slowly, but it will be more accurate. A larger batch size will converge more quickly, but it may be less accurate.

The Implementation of SGD

SGD can be implemented in any programming language that supports numerical computation. In Python, SGD can be implemented using the scipy.optimize library.

The following code shows how to implement SGD in Python:

import numpy as np
from scipy.optimize import minimize

def loss_function(θ):
# Calculate the loss function
return np.sum((θ - x)**2)

def gradient_descent(θ, η, iterations):
# Initialize the parameters
θ_new = θ

# Iterate for the specified number of iterations
for i in range(iterations):

# Calculate the gradient
gradient = np.gradient(loss_function(θ))

# Update the parameters
θ_new = θ - η * gradient

# Return the updated parameters
return θ_new

# Initialize the parameters
θ = np.array([1, 2])

# Set the learning rate
η = 0.01
# Set the number of iterations
iterations = 1000

# Run SGD
θ_new = gradient_descent(θ, η, iterations)
# Print the updated parameters
print(θ_new)

The Pros and Cons of SGD

SGD is a simple and efficient algorithm, but it can be sensitive to hyperparameters. It can also be slow to converge for large datasets.

“SGD is the most important algorithm in machine learning.” — Andrew Ng

Here are some of the pros and cons of SGD:

Pros:

  • Simple to implement
  • Efficient
  • Can be used with any type of loss function.

Cons:

  • Sensitive to hyperparameters
  • Can be slow to converge for large datasets.

Conclusion

SGD is a powerful optimization algorithm that can be used to train machine learning models. It is a simple algorithm, but it can be difficult to understand how it works. This blog post has provided a detailed explanation of SGD, including its math, implementation, and pros and cons.

Thank you for reading my blog post on Stochastic Gradient Descent: A Basic Explanation. I hope you found it informative and helpful. If you have any questions or feedback, please feel free to leave a comment below.

I also encourage you to check out my Portfolio and GitHub. You can find links to both in the description below.

I am always working on new and exciting projects, so be sure to subscribe to my blog so you don’t miss a thing!

Thanks again for reading, and I hope to see you next time!

[Portfolio Link] [Github Link]

--

--