Welcome! We're about to explore Stochastic Gradient Descent (SGD), a pivotal optimization algorithm. SGD, a variant of Gradient Descent, is renowned for its efficiency with large datasets due to its unique stochastic nature. Stochastic means "random" and is the opposite of deterministic. A deterministic algorithm runs the same every time, but a stochastic one introduces a randomness. Our journey includes understanding SGD, its theoretical concepts, and implementing it in C++.
SGD starts by understanding its structure. Unlike Gradient Descent, SGD calculates an estimate of the gradient using a randomly selected single data point, not the entire dataset. Consequently, SGD is highly efficient for large datasets.
While the efficient handling of large datasets by SGD is a blessing, its stochasticity can often lead to a slightly noisier process for convergence, resulting in the model not settling at an absolute minimum.
We are going to use this simple example of data:
In terms of math, SGD can be formulated as follows. Imagine we are looking for a best-fit line, setting the parameters of the familiar equation. Remember, is the slope and is the y-intercept. Then:
Now, let's dive into C++ to implement SGD. This process encompasses initializing parameters randomly, selecting a random training sample, calculating the gradient, updating the parameters, and running several iterations (also known as epochs).
Let's break it down with the following code:
After running the SGD implementation, we should see the final optimized values of m (slope) and b (intercept).
Notice, the learning rate in SGD, set to 0.01 in our example, is a crucial hyperparameter. While 0.01 is a common starting value, it may not always guarantee convergence for all datasets. If the learning rate is too high, the algorithm might overshoot the minimum and fail to converge; if it is too low, convergence can be extremely slow. It is often necessary to experiment with different learning rates or use techniques like learning rate schedules to find the most suitable value for your specific problem.
We apply our SGD function and then visualize the progress using matplotlib-cpp.
Here is the result:

This plot visualizes the implementation of SGD on a simple linear regression problem, showcasing the resulting model.
Today's lesson unveiled critical aspects of the Stochastic Gradient Descent algorithm. We explored its significance, advantages, disadvantages, mathematical formulation, and C++ implementation. You'll soon practice these concepts in upcoming tasks, cementing your understanding of SGD and enhancing your C++ coding skills in machine learning. Happy learning!
