Stochastic Gradient Descent (SGD) is a fundamental optimization algorithm in machine learning and deep learning, used to minimize the loss function of a model. Unlike standard gradient descent, which computes the gradient using the entire dataset (batch gradient descent), SGD updates model parameters using only a single training example or a small random subset (mini-batch) per iteration.
How it works:
- Initialize model parameters (weights) randomly.
- Shuffle the training data.
- For each epoch (full pass over data), iterate over training examples:
- Compute the gradient of the loss function for that example.
- Update parameters in the opposite direction of the gradient, scaled by a learning rate.
- Repeat until convergence.
Advantages:
- Much faster than batch gradient descent on large datasets.
- Can escape local minima due to noisy updates.
- Naturally supports online learning.
Challenges:
- Learning rate tuning is critical; too high can overshoot, too low can be slow.
- Noisy updates may cause oscillations.
- Converges to a minimum but not always the global optimum.
Variants:
- Momentum: Accelerates SGD by adding a fraction of the previous update.
- AdaGrad, RMSProp, Adam: Adaptive learning rate methods that adjust per-parameter learning rates.
SGD remains the workhorse behind training neural networks and is essential knowledge for any machine learning practitioner.