Gradient Descent is the go-to optimization algorithm in machine learning for minimizing errors by tweaking model parameters like weights to nail predictions.
๐ Whatโs the Goal?
Find optimal parameter values that shrink the loss functionโthe gap between what your model predicts and the real truth.
๐ง How It Works (Step-by-Step):
1. Kick off with random weights
2. Predict using those weights
3. Compute the loss (error)
4. Calculate the gradient (slope) of loss vs. weights
5. Update weights opposite the gradient to descend
6. Loop until loss bottoms out
๐ Formula:
new_weight = old_weight - learning_rate ร gradient
โฆ Learning rate sets step size: Too big overshoots, too small crawls slowly.
๐ฆ Types of Gradient Descent:
โฆ Batch GD โ Full dataset per update (accurate but slow)
โฆ Stochastic GD (SGD) โ One data point at a time (fast, noisy)
โฆ Mini-Batch GD โ Small chunks (sweet spot for efficiency, most used in 2025)
๐ Simple Example (Python):
weight = 0
lr = 0.01 # learning rate
for i in range(100):
pred = weight * 2 # input x = 2
loss = (pred - 4) ** 2
grad = 2 * 2 * (pred - 4)
weight -= lr * grad
print("Final weight:", weight) # Should converge near 2
โ Summary:
โฆ Powers loss minimization in ML models
โฆ Essential for Linear Regression, Neural Networks, and deep learning
โฆ Variants like Adam optimize it further for modern AI
๐ฌ Tap โค๏ธ for more