Loss functions, derivatives, update rule, learning rate — explained with full numeric examples
Author: M. Nagesh
Institution: KL University
Domain: AI / Machine Learning
Every time a neural network trains — every time ChatGPT got smarter — Gradient Descent was quietly doing all the work. Let's understand exactly how, with real numbers at every step.
Gradient Descent = A simple way to slide down the error curve step by step until we reach the lowest point — that lowest point is where the model has learned.
CONCEPT 01What is a Loss Function?
Before gradient descent can do anything, it needs something to minimize. A loss function is that thing — it measures how wrong the model's prediction is. The bigger the loss, the worse the model.
🇬🇧 EnglishLoss = how far the model's prediction is from the actual value.
🇮🇳 TeluguLoss = model prediction అసలు value నుండి ఎంత దూరంలో ఉందో.
🇮🇳 HindiLoss = model की prediction असली value से कितनी दूर है।
Over many examples — Mean Squared Error (MSE): MSE = (1/n) × Σ (actual − predicted)²
➕
Why square the error?
Squaring makes all errors positive — negative and positive errors won't cancel out.
📈
Why not use |Error|?
Squaring penalizes large errors much more — it forces the model to fix big mistakes first.
Our Goal:
Make the loss as small as possible. This is exactly what Gradient Descent does.
CONCEPT 02What is Gradient Descent?
📐
Gradient
The slope or steepness of the loss function at any given point. It tells us which direction is uphill and by how much.
⬇️
Descent
Going downward — moving in the direction that reduces the loss, step by step, toward the minimum.
One-Line Definition
Gradient Descent = repeatedly adjust weights in the direction that reduces the loss, until the loss is minimized.
CONCEPT 03The Intuition — Foggy Hill Analogy
⛰️ Foggy Hill Analogy
Imagine standing on a hill in thick fog. You cannot see the bottom of the valley — your goal. The only thing you can feel is the slope under your feet.
What do you do? Feel which direction the ground goes down and take a step that way. Steep slope → big step. Gentle slope → small step. Keep repeating until the ground is flat — you reached the bottom!
The Hill = Loss Function | The Valley = Minimum Loss | The Slope = Gradient | Each Step = Weight Update
CONCEPT 04The Math — Derivative & Update Rule
Now let's translate the hill analogy into actual mathematics — step by step.
What is a Derivative? — The Slope of a Function
💡
A derivative tells us how much a function's output changes when we change the input by a tiny amount.
📝
For function f(w), derivative is written as: f'(w) = df(w) / dw
+
Positive f'(w) → function going uphill at this point
−
Negative f'(w) → function going downhill at this point
0
Zero f'(w) → function is flat — we are at a minimum ✓
New loss: f(1.464) = (1.464−3)² = (−1.536)² = 2.359
ALL 5 ITERATIONS — SUMMARY TABLE:
Step
w (before)
Gradient: 2(w−3)
α × Gradient
w_new
Loss f(w_new)
1
0
−6.000
−0.600
0.600
5.760
2
0.600
−4.800
−0.480
1.080
3.686
3
1.080
−3.840
−0.384
1.464
2.359
4
1.464
−3.072
−0.307
1.771
1.510
5
1.771
−2.458
−0.246
2.017
0.966 ↓
Key Observation:
w is getting closer to 3 with every step. The gradient is also shrinking — meaning steps get smaller as we approach the minimum. This is how gradient descent converges.
# Step 2: update rule → w = w - α × gradient w=w-learning_rate*gradient
# Step 3: compute new loss → f(w) = (w - 3)² loss= (w-3) **2
print(f"Step {step+1:>2}: w = {w:.4f} loss = {loss:.6f}")
Output — Selected Steps
1
Step 1: w = 0.6000 loss = 5.760000
5
Step 5: w = 2.0170 loss = 0.966289
10
Step 10: w = 2.6508 loss = 0.121577
25
Step 25: w = 2.9722 loss = 0.000773
50
Step 50: w = 3.0000 loss = 0.000000 ✓ MINIMUM FOUND
After 50 steps, w = 3 exactly and loss = 0. In real deep learning frameworks like PyTorch and TensorFlow, gradients are computed automatically via backpropagation — the same update rule applies.
FINALPutting It All Together
🎯 Key Takeaways
The loss function measures how wrong the model is — our goal is to minimize it.
Gradient Descent adjusts weights step by step in the direction that reduces loss.
The derivative (gradient) gives us the slope — which direction is uphill and by how much.
The update rule w_new = w_old − α × f'(w) always moves us toward the minimum.
The minus sign is crucial — it ensures we always go downhill regardless of which side we are on.
Learning rate α controls step size — too small is slow, too large diverges.
Mini-batch gradient descent is the standard in practice — balanced speed and accuracy.
In real models, gradients are computed automatically via backpropagation — but the same math applies.
THE CORE FORMULA — NEVER FORGET
w_new = w_old − α × f'(w_old)
→This one line is how every neural network — GPT, BERT, ResNet — actually learns.