Gradient Descent Algorithm
Imagine you are blindfolded at some arbitrary point on a mountain, tasked with finding the absolute lowest point in the valley. You cannot see the entire landscape, only the immediate slope beneath your feet. Your best strategy is to take small steps in the direction that feels steepest downhill. This iterative process, guided by local information, is the core intuition behind Gradient Descent.
The Core Idea: Descending the Loss Landscape
In machine learning, we often define a loss function (or cost function) that quantifies how poorly our model performs for a given set of parameters. The goal of training is to find the parameter values that minimize this loss function. Gradient Descent treats this loss function as a multi-dimensional landscape, where the height represents the loss and the horizontal dimensions represent the model's parameters.
The Gradient: Direction of Steepest Ascent
The gradient of a function at a particular point is a vector that points in the direction of the function's steepest increase. For a loss function with multiple parameters, the gradient is composed of the partial derivatives with respect to each parameter. By knowing the direction of steepest increase, we can infer the direction of steepest decrease by simply taking the negative of the gradient.
For a function , its gradient is a vector of its partial derivatives:
The Update Rule: Stepping Downhill
To move towards the minimum of the loss function, we adjust our parameters in the direction opposite to the gradient. This adjustment is scaled by a crucial hyperparameter called the learning rate, denoted by . The learning rate determines the size of each step we take down the loss landscape. A larger learning rate means bigger steps, while a smaller one means finer adjustments.
The parameters are updated iteratively using the following rule:
Where:
- are the updated parameters
- are the current parameters
- is the learning rate
- is the gradient of the loss function with respect to at the current parameters
The Learning Rate (): Step Size Control
The learning rate is arguably the most critical hyperparameter in Gradient Descent. If is too large, the algorithm might overshoot the minimum, bounce around, or even diverge, never finding a stable solution. Conversely, if is too small, convergence will be extremely slow, requiring many more iterations and computational resources. Finding an optimal learning rate often involves experimentation and techniques like learning rate schedules.
gradient_descent function from the previous example, experiment with different learning rates. What happens if you set learning_rate = 0.9 or learning_rate = 0.001? Observe the final_x and final_loss values.Batch, Stochastic, and Mini-Batch Gradient Descent
The term 'Gradient Descent' often refers to Batch Gradient Descent (BGD), where the gradient is calculated using the entire training dataset for each parameter update. While precise, this can be computationally expensive for large datasets. To address this, Stochastic Gradient Descent (SGD) computes the gradient and updates parameters using only one randomly chosen training example at a time. Mini-Batch Gradient Descent (MBGD) strikes a balance, using a small, randomly selected subset (a 'mini-batch') of the training data for each update.
| Feature | Batch Gradient Descent (BGD) | Stochastic Gradient Descent (SGD) | Mini-Batch Gradient Descent (MBGD) |
|---|---|---|---|
| Data per Update | Entire dataset | One training example | Small subset (mini-batch) |
| Update Frequency | One update per epoch | Many updates per epoch | Multiple updates per epoch |
| Computational Cost per Update | Very high | Very low | Moderate |
| Convergence Path | Smooth, direct | Noisy, zig-zagging | Smoother than SGD, noisier than BGD |
| Risk of Local Minima | High (can get stuck) | Lower (noise can escape) | Moderate |
| Typical Use Case | Small datasets, convex functions | Very large datasets, online learning | Most common for deep learning |
Challenges and Practical Considerations
While powerful, Gradient Descent faces challenges. In complex, non-convex loss landscapes, it can get stuck in local minima or saddle points, where the gradient is zero but the function is not at its global minimum. The choice of learning rate is critical, as discussed. Additionally, deciding when to stop the iterative process (convergence criteria) is important; common criteria include a maximum number of iterations, or stopping when the change in loss or parameters falls below a small threshold.
Gradient Descent is an iterative optimization algorithm that minimizes a function by repeatedly moving in the direction of steepest decrease.
The gradient is a vector of partial derivatives that points towards the steepest increase of a function; we move in the opposite direction.
The parameter update rule is , where are parameters, is the learning rate, and is the loss function.
The learning rate () controls the step size. Too high, and the algorithm may overshoot or diverge; too low, and convergence becomes excessively slow.
Batch Gradient Descent uses the entire dataset for each update, Stochastic Gradient Descent uses one example, and Mini-Batch Gradient Descent uses a small subset, balancing speed and stability.
Gradient Descent can get stuck in local minima or saddle points in non-convex loss landscapes, which is a common challenge in deep learning.
Practical application involves defining a loss function, calculating its gradient, and iteratively updating parameters until a convergence criterion is met.