Back

Stochastic Gradient Descent (SGD)

What is Stochastic Gradient Descent (SGD)?

Stochastic Gradient Descent (SGD) is a variant of the gradient descent optimization algorithm that updates the model's parameters using only one or a small subset (mini-batch) of training examples at each iteration, rather than the entire dataset. This approach introduces randomness into the parameter updates, which can lead to faster convergence and the ability to escape local minima more effectively compared to standard gradient descent.

How Does Stochastic Gradient Descent Work? 

SGD involves the following steps:

  1. Shuffle Dataset: The training data is shuffled to ensure that the updates are not biased by the order of the data.
  2. Initialize Parameters: The model's parameters (weights and biases) are initialized.
  3. Iterate Through Data: For each training example (or mini-batch) in the dataset:some text
    • Compute Loss: The loss function is computed for the current training example.
    • Calculate Gradient: The gradient of the loss function with respect to the model's parameters is calculated.
    • Update Parameters: The parameters are updated using the gradient: θ=θ−η⋅∇J(θ;x(i),y(i))\theta = \theta - \eta \cdot \nabla J(\theta; x^{(i)}, y^{(i)})θ=θ−η⋅∇J(θ;x(i),y(i)) where x(i),y(i)x^{(i)}, y^{(i)}x(i),y(i) are the input and output of the iii-th training example, η\etaη is the learning rate, and ∇J(θ;x(i),y(i))\nabla J(\theta; x^{(i)}, y^{(i)})∇J(θ;x(i),y(i)) is the gradient of the loss with respect to θ\thetaθ for that example.
  4. Repeat: Steps 3 is repeated for several epochs until the loss function converges or the maximum number of iterations is reached.

Why is Stochastic Gradient Descent Important?

  • Faster Convergence: SGD can converge faster than batch gradient descent, especially for large datasets, because it updates parameters more frequently.
  • Generalization: The noise introduced by the random selection of training examples can help the model generalize better to unseen data by avoiding overfitting.
  • Scalability: SGD is well-suited for large-scale machine learning tasks because it processes small subsets of data at a time, reducing memory requirements.

Conclusion 

Stochastic Gradient Descent (SGD) is an efficient and scalable optimization algorithm that updates model parameters using individual training examples or mini-batches. Its ability to handle large datasets and its potential for faster convergence make it a popular choice for training machine learning models, particularly deep neural networks.