CadetX
CX Learn | Complete Neural Networks Guide
TensorFlow · Keras
CadetX logo CadetX CX Learn
NN-101 · Module 4 Beginner about 40 minutes 9 Lessons Prereq: Modules 1 to 3

Loss Functions & Gradient Descent

Every neural network learns in the same way: it measures how wrong it is, then nudges its weights to be a little less wrong. This module explains both halves of that idea: the loss function that measures the error, and gradient descent, the method that reduces it.

  • Level: Beginner
  • Time: about 40 minutes
  • Needs: Modules 1 to 3

By the end of this module you will be able to

  • Explain what a loss function measures
  • Choose the right loss for regression and classification
  • Describe the loss landscape and what "going downhill" means
  • Apply the gradient descent update by hand
  • Explain how the learning rate affects training
  • Explain batches, mini-batches and epochs
  • Describe momentum and the Adam optimiser
  • Code gradient descent from scratch in NumPy

4.1What is a loss function?

A network can't improve unless it knows how wrong it is. A loss function (also called a cost function) turns every mistake into a single number: the loss. A big loss means bad predictions; a loss of zero means perfect ones.

The whole of training in one sentence

Training a neural network means finding the weights that make the loss as small as possible.

Like a satnav

A satnav constantly measures one number: how far you are from your destination. Every turn it recommends is chosen to make that number smaller. The loss is the network's "distance to destination", and gradient descent is its satnav.

4.2Choosing a loss function

You've met most of these in the Machine Learning track. The right choice depends on the task, and it goes hand in hand with the output activation from Module 3.

TaskLoss functionWhat it measuresKeras name
RegressionMean squared error (MSE)Average of the squared errors; punishes big misses"mse"
Regression with outliersMean absolute error (MAE)Average size of the errors; less upset by extreme values"mae"
Binary classificationBinary cross-entropy (log loss)Punishes confident wrong probabilities very hard"binary_crossentropy"
Multi-class classificationCategorical cross-entropyThe same idea, across several classes with softmax"sparse_categorical_crossentropy"

Cross-entropy, in plain English

For classification, the loss only looks at the probability the network gave to the correct answer. If the correct digit is 7:

Probability given to "7"Cross-entropy lossVerdict
0.950.05Confident and right: tiny penalty
0.500.69Unsure: medium penalty
0.053.00Confident and wrong: big penalty

The loss is calculated as −log(probability of the correct answer). You don't need to calculate it by hand; just remember it rewards being confidently right and heavily punishes being confidently wrong.

4.3The loss landscape

Imagine every possible combination of weights as a location on a map, and the loss as the height at that location. The result is a landscape of hills and valleys. Training means finding the lowest valley.

A loss landscape with a shallow dip on the left, called a local minimum, and a deeper valley on the right, the global minimum. A ball rolling downhill from the left could get stuck in the shallow dip.Local minimumlooks like the bottom, but isn'tGlobal minimumthe lowest possible lossWeight value →Loss →
Figure 1. A loss landscape for a single weight. Real networks have millions of weights, so their landscape has millions of dimensions, but the idea is the same: find the lowest point.

Walking down a mountain in fog

You're on a mountain in thick fog and want to reach the valley. You can't see the bottom, but you can feel the slope under your feet. So you take a step in the steepest downhill direction, feel again, and repeat. That's gradient descent. The risk: you might end up in a small dip (a local minimum) rather than the lowest valley. In practice, big networks have so many dimensions that there's usually a way downhill, so this is less of a problem than it sounds.

4.4Gradient descent

The gradient is the slope of the loss for each weight: it says which direction is uphill, and how steeply. To go downhill, we step in the opposite direction:

new weight = weight − learning rate × gradient
If the gradient is…It means…So the weight…
Positive (+)Increasing this weight increases the lossGoes down
Negative (−)Increasing this weight decreases the lossGoes up
Close to 0We're near a flat spot, hopefully the bottomBarely changes

A worked example

A weight is currently 5. The gradient of the loss for this weight is 4 (uphill to the right). With a learning rate of 0.1:

new weight = 5 − 0.1 × 4 = 4.6

The weight moves a little to the left, downhill. Next step, we measure the new gradient and repeat. A real network does this for every weight at once. Working out all those gradients efficiently is the job of backpropagation, in Module 5.

4.5The learning rate

The learning rate sets how big each step is. It's one of the most important settings in deep learning. Try it: the ball starts on the left and wants to reach the bottom of the curve.

Try it: roll down the loss curve

Learning rateWhat happens
Too smallTiny, safe steps, but training takes forever
Just rightReaches the bottom quickly and settles
A bit too bigOvershoots and bounces from side to side before settling
Far too bigEach step overshoots more than the last: the loss explodes (divergence)

Common starting values are 0.001 for the Adam optimiser and 0.01 for plain gradient descent. If your loss shoots up to huge numbers or becomes nan ("not a number"), the learning rate is usually too high.

4.6Batches, mini-batches and epochs

Should the network look at every training example before taking one step, or take a step after each one? There are three options:

MethodExamples per stepProsCons
Batch gradient descentAll of themSmooth, accurate stepsVery slow and memory-hungry with big datasets
Stochastic gradient descent (SGD)1Fast, frequent updatesVery noisy, jumpy path
Mini-batch gradient descentA small group, e.g. 32The best of both; makes good use of GPUsOne more setting to choose

Mini-batches are the standard. Three words you'll see all the time:

TermMeaningExample: 1,000 images, batch size 32
Batch sizeExamples used for each weight update32
Iteration (step)One weight update1 update per batch of 32
EpochOne full pass through the whole training set1,000 ÷ 32 ≈ 32 iterations

4.7Smarter optimisers: momentum and Adam

Plain gradient descent has a weakness. In long, narrow valleys, the steepest direction points across the valley rather than along it, so it zigzags from side to side. Modern optimisers fix this.

Three optimisers heading for the lowest point of a long, narrow valley. Plain gradient descent zigzags from side to side. Momentum builds up speed along the valley. Adam adjusts its step size for each direction and heads more directly to the bottom.Lowest lossStartGradient descent: zigzagsMomentum: builds speedAdam: adapts each step
Figure 2. 30 steps of three optimisers in a long, narrow valley. The contour lines show equal loss, like a map. Plain gradient descent (red) zigzags across; momentum (amber) builds up speed along the valley but can overshoot; Adam (green) adapts its step size for each direction.
OptimiserIdeaAnalogy
SGDPlain gradient descent on mini-batchesWalking downhill, one careful step at a time
MomentumKeeps a running average of recent steps, so it speeds up in a consistent directionA ball rolling downhill, building speed
RMSpropGives each weight its own step size, smaller for weights with big, jumpy gradientsTaking smaller steps on steep, slippery ground
AdamCombines momentum and RMSpropA heavy ball with smart brakes

What should I use?

Adam with its default learning rate (0.001) is the usual starting point. It works well on most problems with little tuning. In Keras: optimizer="adam".

4.8Gradient descent from scratch in Python

Let's write gradient descent ourselves in NumPy. We'll create data that follows the rule y = 3x + 2 (plus noise), start with a terrible guess of y = 0x + 0, and let gradient descent find the rule. Then we'll try three learning rates.

gradient_descent.py
import numpy as np

# 1. Some data: y is roughly 3x + 2, plus noise
rng = np.random.default_rng(0)
x = rng.uniform(0, 2, 100)
y = 3 * x + 2 + rng.normal(0, 0.3, 100)

def mse(y_true, y_pred):
    return np.mean((y_true - y_pred) ** 2)

# 2. Gradient descent: start with a bad guess and improve it step by step
def train(learning_rate, steps=200):
    w, b = 0.0, 0.0                              # starting guess
    history = []
    for step in range(steps):
        y_pred = w * x + b                        # forward pass
        loss = mse(y, y_pred)                     # how wrong are we?
        grad_w = -2 * np.mean(x * (y - y_pred))   # slope of the loss for w
        grad_b = -2 * np.mean(y - y_pred)         # slope of the loss for b
        w -= learning_rate * grad_w               # step downhill
        b -= learning_rate * grad_b
        history.append(loss)
    return w, b, history

w, b, history = train(learning_rate=0.1)
for step in [0, 10, 50, 199]:
    print(f"Step {step:3}: loss = {history[step]:.4f}")
print(f"Learned: y = {w:.2f}x + {b:.2f}   (true rule: y = 3x + 2)")

# 3. The learning rate matters
print()
for lr in [0.001, 0.1, 0.9]:
    w, b, h = train(lr)
    print(f"learning rate {lr:<5}: final loss = {h[-1]:.4g}, w = {w:.3g}, b = {b:.3g}")
Output
Step   0: loss = 31.0796
Step  10: loss = 0.0895
Step  50: loss = 0.0850
Step 199: loss = 0.0846
Learned: y = 2.98x + 2.00   (true rule: y = 3x + 2)

learning rate 0.001: final loss = 4.593, w = 1.77, b = 1.35
learning rate 0.1  : final loss = 0.0846, w = 2.98, b = 2
learning rate 0.9  : final loss = 3.82e+210, w = -3.33e+105, b = -2.58e+105

What the output tells us

  • The loss falls fast: from 31.1 at the start to 0.09 after just 10 steps, then it levels off. It can't reach zero, because the data contains random noise.
  • Gradient descent found the rule by itself: y = 2.98x + 2.00, extremely close to the true y = 3x + 2.
  • A learning rate of 0.001 is too small: after 200 steps it's still far from the answer (w = 1.77).
  • A learning rate of 0.9 is far too big: every step overshoots further, and the numbers explode to around 10210. This is divergence.
  • grad_w and grad_b are the slopes of the MSE loss, worked out with calculus. In real networks you never write these yourself: Keras and PyTorch calculate them automatically (Module 5).

SummaryKey takeaways

  • A loss function turns a network's mistakes into one number. Training means making it as small as possible.
  • Use MSE (or MAE) for regression and cross-entropy for classification.
  • The loss landscape is like a mountain range; training looks for the lowest valley.
  • Gradient descent: new weight = weight − learning rate × gradient.
  • The learning rate sets step size: too small is slow, too big overshoots or explodes.
  • Mini-batches (e.g. 32 examples) are the standard; an epoch is one full pass through the data.
  • Momentum builds speed, Adam adapts step sizes; Adam is the usual default.

Check your understanding

Your neural networks roadmap

  1. 01
    Introduction to Neural NetworksWhat neural networks are and how they work
  2. 02
    The Perceptron and the Artificial NeuronBuild a single neuron from scratch
  3. 03
    Activation FunctionsSigmoid, tanh, ReLU and softmax
  4. 04
    Loss Functions and Gradient DescentYou are here
  5. 05
    BackpropagationHow every weight learns its share of the blame
  6. 06
    Building Networks with KerasYour first deep learning library
  7. 07
    Training Better NetworksOverfitting, dropout, batch normalisation, early stopping
  8. 08
    Convolutional Neural NetworksTeaching computers to see
  9. 09
    Recurrent Neural Networks and LSTMsWorking with sequences
  10. 10
    Transformers and AttentionThe architecture behind ChatGPT and Claude
  11. 11
    Transfer Learning and Pretrained ModelsStanding on the shoulders of giants
  12. 12
    Capstone ProjectBuild and present a deep learning project