← back

how neural networks learn, no magic.

how neural networks learn, no magic.

You've probably heard these words a hundred times: gradient descent, derivatives, chain rule, backpropagation. They make AI sound like a secret language.

The central idea is simple. A network makes a prediction. We measure how wrong it was. We work out which adjustable numbers caused the error. We nudge them a little and try again.

That's the learning loop. Let's walk through it.

01. the loss: measuring the mistake

Say a model predicts a house costs ₹50 lakh. The real price is ₹60 lakh. We need one number that says how wrong that was. That's the loss.

A simple choice is squared error:

L = (y_hat - y)^2

y_hat is the prediction and y is the correct answer. With 50 vs 60, the loss is (50 - 60)^2 = 100. If the prediction improves to 58, the loss drops to 4.

Squaring keeps the loss positive and punishes big errors more. Averaged across many examples, it's called mean squared error (MSE). The goal of training: make this number smaller.

02. gradient descent: walking downhill

Picture standing on a foggy hill. You can't see the whole landscape, but you can feel the slope under your feet. So you take a small step downhill, check again, repeat.

That's gradient descent. The height is the loss. Your position is the model's weights and biases. The gradient points uphill, so we step the opposite way:

theta_new = theta_old - eta * dL/dtheta

In plain English: new weight = old weight − learning rate × gradient.

Try it on L = x^2, where the gradient is 2x. Start at x = 3 with a learning rate of 0.1:

Each step gets closer to the minimum, and the steps shrink as the slope flattens.

The learning rate matters. Too small and learning crawls. Too big and you jump past the answer.

03. the chain rule and backpropagation

A real network has millions of weights. Most don't touch the loss directly. An early weight changes a hidden value, which changes the next layer, which changes the prediction, which changes the loss.

The chain rule traces that influence link by link:

dL/dx = dL/du * du/dx

Think of a presentation that went wrong. It was confusing because the slides were, because the analysis was, because the data was incomplete. To find the cause, you trace the chain back.

Backpropagation applies the chain rule backward through the whole network. It reuses each step's result, so every weight's gradient comes out efficiently.

Remember the split: backpropagation calculates the gradients. The optimizer uses them to update the weights.

04. the full loop, and one catch

Here's the whole thing in a few lines of Python, with one weight learning y = 2x:

x, target, weight, lr = 3.0, 6.0, 0.0, 0.01

for step in range(100):
    prediction = weight * x                      # forward pass
    loss = (prediction - target) ** 2            # measure the mistake
    gradient = 2 * (prediction - target) * x     # backward math
    weight = weight - lr * gradient              # update

print(weight)  # moves toward 2

Frameworks like PyTorch calculate the gradients for you with automatic differentiation. Large language models run the same loop, just at a vastly bigger scale.

One catch: low training loss isn't proof the model is good. It can memorize examples instead of learning patterns. That's overfitting, which is why we test on data the model has never seen.

Forward pass → loss → backpropagation → update → repeat. No magic. Just functions, derivatives, data and a lot of small updates.