Gradient Descent & Backpropagation

Deep Learning with Keras and TensorFlow 12 min min read Updated: Nov 16, 2026 Beginner
Gradient Descent & Backpropagation
Beginner Topic 3 of 9

The Learning Problem

A network starts with random weights, so its predictions are bad. We need a method to improve the weights. That method is gradient descent, and the way to calculate the needed changes is backpropagation.

Gradient Descent: Walking Down a Hill

Simple example: Imagine you are on a foggy mountain and want to reach the valley. You cannot see far, so you feel the ground with your feet, find which direction goes down and take a small step. You repeat this until you cannot go lower. The height is the loss, and your position is the weights.
  • The gradient tells us the slope: which direction increases the loss
  • We move in the opposite direction to decrease the loss
  • The learning rate is the size of each step
text ew_weight = old_weight - learning_rate * gradient

A Tiny Example in Python

Let us find the value of w that minimizes the loss (w - 3)^2. We know the answer is 3, so we can check the result.

python w = 0.0 # starting guess learning_rate = 0.1 for step in range(20): gradient = 2 * (w - 3) # derivative of (w - 3)^2 w = w - learning_rate * gradient print(step, round(w, 4)) # w moves closer and closer to 3
Result

The value of w goes 0.6, 1.08, 1.46 ... until it is very close to 3.0.

Learning Rate

Learning rateWhat happens
Too smallLearning is very slow
Just rightSmooth and fast learning
Too largeSteps overshoot, the loss jumps around or grows

Backpropagation

In a real network there are thousands or millions of weights. Backpropagation uses the chain rule from calculus to find out how much each weight contributed to the final error. It works from the output layer backward to the input layer, and that is why it has this name.

  1. Forward pass: the data flows through the network and produces a prediction.
  2. Loss: compare the prediction with the true value.
  3. Backward pass: calculate the gradient for every weight.
  4. Update: change each weight a little using gradient descent.

TensorFlow Does the Math for You

tf.GradientTape records operations and computes gradients automatically. This is called automatic differentiation.

python import tensorflow as tf w = tf.Variable(0.0) for step in range(20): with tf.GradientTape() as tape: loss = (w - 3.0) ** 2 grad = tape.gradient(loss, w) w.assign_sub(0.1 * grad) print(w.numpy()) # close to 3.0
Good news: When you use model.fit() in Keras, all of this happens automatically for every weight in the network. You do not have to write it yourself.

Batches and Epochs

TermMeaning
Batch sizeHow many samples are used for one weight update
Iteration (step)One weight update
EpochOne full pass through the entire training dataset

If you have 1000 samples and a batch size of 100, one epoch has 10 steps.

Conclusion

Gradient descent walks downhill on the loss surface and backpropagation tells it which way is down for every weight. Keras and TensorFlow take care of the calculations.

Quick Summary: Compute loss, find gradients with backpropagation, update weights with a learning rate and repeat for many epochs.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators