Stochastic Gradient Descent in Python: A Complete Guide for ML Optimization

Dhirendra Choudhary
4 min readJul 26, 2024

--

Stochastic Gradient Descent (SGD) is a cornerstone technique in machine learning optimization. This guide will walk you through the essentials of SGD, providing you with both theoretical insights and practical Python implementations.

Contents

  1. What is Stochastic Gradient Descent? The Short Answer
  2. What Is Optimization in Machine Learning?
  3. The Concept of Error in Machine Learning
  4. The Gradient
  5. Gradient Descent: Taking Steps Towards the Solution
  6. Stochastic Gradient Descent
  7. Variations of SGD And When to Use Them
  8. Epochs in Gradient Descent Algorithms
  9. SGD in Action: A Walkthrough Example
  10. Using SGD in Real-world Problems
  11. Practical Tips And Tricks When Using SGD
  12. Conclusion

What is Stochastic Gradient Descent? The Short Answer

Stochastic Gradient Descent (SGD) is an optimization technique used to minimize errors in predictive models. Unlike traditional gradient descent, which uses the entire dataset to compute gradients, SGD updates model parameters using only one data point at a time. This approach enhances computational efficiency, especially with large datasets, though it introduces more noise and less stability.

What Is Optimization in Machine Learning?

Optimization in machine learning involves adjusting model parameters to minimize error. For instance, in a simple linear regression model predicting diamond prices, we seek optimal values for parameters like m (price increase per carat) and b (base price) to reduce prediction error. Various optimization algorithms, including SGD, help achieve these optimal values.

The Concept of Error in Machine Learning

Error, or loss, quantifies the difference between predicted and actual values. For regression problems, Mean Squared Error (MSE) is a common cost function that measures this discrepancy. Minimizing MSE helps improve model accuracy by reducing the average squared distance between predictions and true values.

The Gradient

The gradient indicates the direction of the steepest ascent in a function. In optimization, we use the gradient to find the direction of the steepest descent by moving in the opposite direction. The gradient is a vector of partial derivatives, showing how changes in model parameters affect the error function.

Gradient Descent: Taking Steps Towards the Solution

Gradient Descent involves iteratively updating model parameters to minimize the cost function. The learning rate controls the step size, balancing between speed and stability. The algorithm continues to update parameters until a stopping condition, such as a minimal change in error or a predefined number of iterations, is met.

Stochastic Gradient Descent

SGD introduces randomness by using one data point at a time to estimate the gradient. This method accelerates the optimization process for large datasets but results in noisier updates. By frequently updating parameters, SGD can efficiently navigate the error surface, albeit with more fluctuation compared to traditional gradient descent.

Variations of SGD And When to Use Them

SGD has variations such as:

  • Vanilla SGD: Updates parameters for each training example, suitable for very large datasets.
  • Mini-Batch Gradient Descent: Uses batches of data points, offering a balance between speed and stability.
  • Batch Gradient Descent: Utilizes the entire dataset for each update, ideal for smaller datasets with fewer parameters.

Epochs in Gradient Descent Algorithms

An epoch is one complete pass through the training dataset. Multiple epochs allow the model to refine its parameters progressively. Shuffling the data before each epoch helps prevent the model from learning the order of the training examples, improving generalization.

SGD in Action: A Walkthrough Example

Here’s a Python implementation of SGD using NumPy. This example demonstrates how to perform stochastic gradient descent with mini-batches to optimize a simple linear regression model.

import numpy as np
import seaborn as sns

def model(m, x, b):
return m * x + b

def loss(y_true, y_pred):
return np.mean((y_true - y_pred) ** 2)

def stochastic_gradient_descent(x, y, epochs=100, learning_rate=0.01, batch_size=32, stopping_threshold=1e-6):
m = np.random.randn()
b = np.random.randn()
n = len(x)
previous_loss = np.inf

for i in range(epochs):
indices = np.random.permutation(n)
x = x[indices]
y = y[indices]

for j in range(0, n, batch_size):
x_batch = x[j:j + batch_size]
y_batch = y[j:j + batch_size]
y_pred = model(m, x_batch, b)
m_gradient = -2 * np.mean(x_batch * (y_batch - y_pred))
b_gradient = -2 * np.mean(y_batch - y_pred)
m -= learning_rate * m_gradient
b -= learning_rate * b_gradient

y_pred = model(m, x, b)
current_loss = loss(y, y_pred)

if previous_loss - current_loss < stopping_threshold:
break
previous_loss = current_loss

return m, b

# Example usage
diamonds = sns.load_dataset('diamonds')
xy = diamonds[['carat', 'price']].values
np.random.shuffle(xy)
train_size = int(0.8 * len(xy))
train_xy, test_xy = xy[:train_size], xy[train_size:]
m, b = stochastic_gradient_descent(train_xy[:, 0], train_xy[:, 1])
y_preds = model(m, test_xy[:, 0], b)
mean_squared_error = loss(test_xy[:, 1], y_preds)
print(f'RMSE: {mean_squared_error ** 0.5}')

Using SGD in Real-world Problems

While the above example demonstrates a basic implementation, real-world applications often involve more complex data and models. It’s crucial to tune parameters such as learning rate and batch size, and consider advanced techniques like learning rate schedules and momentum to optimize performance.

Practical Tips And Tricks When Using SGD

  1. Shuffle your data.
  2. Use mini-batches.
  3. Normalize inputs.
  4. Choose a suitable learning rate.
  5. Implement learning rate schedules.
  6. Use momentum.
  7. Consider adaptive learning rate methods.
  8. Apply gradient clipping.
  9. Monitor validation performance.
  10. Use regularization.

Conclusion

SGD is a powerful tool in machine learning optimization, offering a balance between speed and computational efficiency. By understanding its core principles and practical implementation, you can effectively leverage SGD to enhance your machine-learning models.

Thanks for taking time to check out my article! If you’re as passionate about data science, especially language models, let’s connect on LinkedIn! I’m always up for insightful discussions and looking forward to sharing more content. Don’t forget to hit that like button and subscribe to stay tuned for the latest updates. Your engagement means a lot!

--

--