Backpropagation

What Is Backpropagation?

Backpropagation is the process a neural network uses to learn from its mistakes. It sends error information backward through the network so weights can be updated.

01

Prediction is made

02

Loss is calculated

03

Error is sent backward through the network

04

Weights are updated

05

Future predictions improve

Backpropagation is one of the main reasons neural networks can learn.

Why Backpropagation Matters

Neural networks can have many weights. Backpropagation helps the model figure out which weights contributed to an error and how those weights should change.

Example

Prediction — Dog   Correct Answer — Cat   Loss — High

Backpropagation Helps Answer:

  • Which weights caused this error?
  • How should those weights change?
  • How can the model do better next time?

Backpropagation and Training

Backpropagation happens during training after the model makes a prediction and calculates loss.

01

Input data enters the model

02

Forward propagation produces a prediction

03

Loss function measures the error

04

Backpropagation sends the error backward

05

Gradient descent updates the weights

06

The process repeats many times

Forward Propagation Review

Forward propagation moves data from the input layer to the output layer to create a prediction.

01

Input layer

02

Hidden layers

03

Output layer

04

Prediction produced

Question It Answers

What does the model predict?

Loss Function Review

A loss function measures how wrong the model's prediction is.

Wrong Prediction

Prediction: Dog

Correct Answer: Cat

Loss: High

Correct Prediction

Prediction: Cat

Correct Answer: Cat

Loss: Low

The loss value tells the model how much it needs to improve.

Backpropagation Uses Loss

Backpropagation starts with the loss value. It uses that loss to calculate gradients for the model's weights.

How Loss Guides Backpropagation

If loss is high — the model needs larger corrections

If loss is low — the model may need smaller adjustments

Backpropagation calculates — how each weight affects the loss

What Is a Gradient?

A gradient tells the model the direction and size of a needed change.

Gradient Idea

Gradient — How should this weight change to reduce loss?

Positive gradient — The weight may need to decrease

Negative gradient — The weight may need to increase

The exact update depends on the optimizer and learning rate.

Gradient Descent Review

Backpropagation calculates gradients. Gradient descent uses those gradients to update weights.

ProcessMain Job
BackpropagationCalculates gradients
Gradient DescentUpdates weights

Weights

Weights are learned numbers inside a neural network. They control how strongly inputs affect outputs.

Spam Detection Example

Feature: Suspicious links — Weight: High

Feature: Normal greeting — Weight: Low

The model learns which inputs matter most through backpropagation.

Weights Are Updated Based on Error

If a weight contributed strongly to a wrong prediction, it may be changed more. If it had little effect on the error, it may be changed less.

Update Size

Large contribution to error — Bigger weight update

Small contribution to error — Smaller weight update

This allows the model to improve gradually.

Learning Rate

The learning rate controls how big each weight update is.

Too High

Model may jump past good weights.

Training may become unstable.

Too Low

Model may learn very slowly.

Training may take too long.

A good learning rate helps loss decrease steadily.

Backpropagation Step by Step

01

The model makes a prediction

02

The loss function calculates the error

03

The error is sent backward from the output layer

04

Gradients are calculated for each weight

05

Weights are updated using gradient descent

06

The next prediction should be slightly better

Output Layer Error

Backpropagation starts at the output layer because that is where the final prediction is produced.

Example

Correct Class — Cat

Model Output — Dog: 0.80, Cat: 0.10, Bird: 0.10

Result — Output layer error is high because the model gave low probability to the correct class.

Hidden Layer Error

Hidden layers do not directly produce the final answer, but they still affect the prediction. Backpropagation calculates how much they contributed to the output error.

Hidden Layers and Backpropagation

Hidden layers — Learn intermediate patterns

Backpropagation — Calculates how those hidden patterns contributed to the final mistake

Result — Weights throughout the network can be updated, not just the output layer.

Chain Rule Idea

Backpropagation uses a calculus idea called the chain rule. The main idea is that earlier weights can affect the final loss through a chain of layers.

01

A weight affects a neuron

02

That neuron affects the next layer

03

The next layer affects the output

04

The output affects the loss

Backpropagation traces this chain backward to figure out how each weight contributed to the final error.

Backpropagation Through Layers

In a deep network, backpropagation moves backward through many layers.

Forward Propagation

Input layer

↓ Hidden layer 1

↓ Hidden layer 2

↓ Output layer

Backpropagation

Output layer

↓ Hidden layer 2

↓ Hidden layer 1

↓ Input-side weights

Simple Weight Update Example

Suppose a model predicts too low for a house price. Backpropagation can help adjust weights connected to useful features.

Regression Example

Actual Price — $500,000   Prediction — $400,000

Error — Prediction is too low

Possible Updates — Increase weights for: square footage, location score, bedrooms

Classification Example

In classification, backpropagation helps the model adjust probabilities for the correct class.

Spam Model

Spam probability — 0.20   Correct Answer — Spam

Problem — Model should have predicted higher spam probability

Possible Updates — Increase weights for: suspicious links, urgent wording, unknown sender, unusual formatting

Image Classification Example

In image classification, backpropagation updates weights across many layers.

Image Model

Prediction — Cat   Correct Answer — Dog

Early layers — Adjust how they detect edges or colors

Middle layers — Adjust how they detect shapes or textures

Later layers — Adjust how they detect object parts

Backpropagation and Activation Functions

Activation functions are used during forward propagation, but they also affect how gradients move backward.

Common Activation Functions
  • ReLU
  • Sigmoid
  • Tanh
  • Softmax

Backpropagation must calculate how changes pass through these functions. Some activation functions can make learning easier or harder.

ReLU and Backpropagation

ReLU is commonly used in hidden layers. It allows gradients to pass through positive inputs, but negative inputs may stop gradients.

ReLU Rule

Positive input — Pass it forward; gradient flows backward

Negative input — Output 0; gradient may become 0

Possible problem — Inactive neurons, also called dying ReLU

Sigmoid and Backpropagation

Sigmoid outputs values between 0 and 1, but it can cause very small gradients when outputs are close to 0 or 1.

01

Output is close to 0 or 1

02

Gradient becomes very small

03

Learning slows down — can contribute to vanishing gradients

Vanishing Gradients

A vanishing gradient happens when gradients become very small as they move backward through the network.

01

Gradients become small

02

Weight updates become tiny

03

Earlier layers learn very slowly or not at all

Vanishing gradients can make deep networks hard to train. Sigmoid and tanh can sometimes contribute to this issue.

Exploding Gradients

An exploding gradient happens when gradients become too large, causing unstable training.

01

Gradients become very large

02

Weight updates become huge

03

Training becomes unstable — loss may jump around or fail

Careful learning rates, normalization, and gradient clipping can help prevent exploding gradients.

Gradient Clipping

Gradient clipping limits how large gradients can become, helping prevent unstable training.

Gradient Clipping

If a gradient is too large, it is reduced to a safer size.

Useful for — Deep networks, sequence models, training with unstable gradients

Backpropagation and Optimizers

An optimizer decides how weights are updated using gradients from backpropagation.

Common Optimizers
  • Stochastic Gradient Descent (SGD)
  • Adam
  • RMSprop

All optimizers use gradients from backpropagation to improve the model.

Stochastic Gradient Descent

Stochastic Gradient Descent, or SGD, updates weights using small batches of data instead of the full dataset every time.

01

Use one batch of examples

02

Calculate loss for the batch

03

Backpropagation calculates gradients

04

Update weights — repeat for next batch

Adam Optimizer

Adam is a popular optimizer in deep learning. It adjusts learning behavior for different weights automatically.

Adam
  • Uses gradients from backpropagation
  • Adapts updates for different weights automatically
  • Often works well as a default optimizer

Adam still depends on backpropagation to calculate gradients.

Backpropagation with Batches

Neural networks often train using batches. A batch is a group of examples processed together.

01

Batch size — 32 examples predicted together

02

Loss is averaged across all 32 examples

03

Backpropagation calculates gradients from batch loss

04

Weights are updated

Epochs and Backpropagation

An epoch is one full pass through the training dataset. Backpropagation usually happens many times in one epoch.

Example

Dataset — 1,000 examples

Batch size — 100

Batches per epoch — 10

Backpropagation updates — 10 per epoch

More epochs mean more chances for the model to update weights.

Backpropagation Before and After Training

Before training, weights may be random and predictions may be poor. Backpropagation helps turn random weights into useful learned weights.

At the Beginning

Loss may be high

Predictions may be weak

Weights are not yet meaningful

After Many Updates

Loss may decrease

Predictions may improve

Weights capture useful patterns

Backpropagation After Many Updates

After many training updates, the model should make better predictions. Backpropagation is repeated many times to gradually reduce errors.

01

Early Training — Model predicts randomly

02

Middle Training — Model starts learning simple patterns

03

Later Training — Model learns stronger patterns

Backpropagation and Loss Curves

A loss curve helps show whether backpropagation and training are working well.

Loss Curve Patterns

Healthy — Training loss decreases, validation loss also decreases

Overfitting — Training loss decreases, validation loss increases

Underfitting — Both losses stay high

Backpropagation and Overfitting

Backpropagation can reduce training loss, but reducing training loss too much can lead to overfitting.

Overfitting Pattern

Training Loss — Very low   Validation Loss — High

The model may be memorizing training data.

Possible Solutions — Early stopping, regularization, dropout, more data, simpler model

Backpropagation and Underfitting

If backpropagation does not reduce loss enough, the model may underfit.

Underfitting Pattern

Training Loss — High   Validation Loss — High

Possible Causes — Model is too simple, learning rate is too low, not enough training epochs, poor features, too much regularization

Backpropagation and Regularization

Regularization helps prevent overfitting by controlling how weights are learned.

Regularization Methods

L1 Regularization — Can push some weights toward zero

L2 Regularization — Shrinks large weights

Dropout — Randomly turns off some neurons during training

These methods affect how the model learns during weight updates.

Backpropagation and Dropout

Dropout randomly turns off some neurons during training. During backpropagation, only active neurons are updated in that step.

01

Some neurons are randomly turned off

02

Model cannot rely too much on any one pathway

03

Only active neurons receive updates from backpropagation

04

Model may generalize better as a result

Backpropagation and Deep Learning

Deep learning relies heavily on backpropagation because deep networks can have many layers and many weights.

Why Backpropagation Is Essential

Deep networks can have many layers, many neurons, and millions or billions of weights.

Backpropagation makes it possible to update weights throughout the whole network based on error.

Without backpropagation, training large neural networks would be much harder.

Backpropagation for Regression

In regression, backpropagation updates weights so numerical predictions become closer to actual values.

Regression Example

Prediction — $420,000   Correct Answer — $450,000

Loss — Based on numerical error

Goal — Update weights so future predictions are closer to actual prices

Backpropagation for Classification

In classification, backpropagation updates weights so the model gives higher probability to the correct class.

Classification Example

Correct Class — Dog

Model Output — Cat: 0.70, Dog: 0.20, Bird: 0.10

Loss — High because Dog received low probability

Goal — Increase probability for Dog in similar examples later

Backpropagation for Language Models

Language models use backpropagation to improve next-token predictions.

Language Model Example

Input — "The cat sat on the"

Correct Next Token — "mat"

If the model gives low probability to "mat", loss is high.

Backpropagation — Updates weights so similar contexts are predicted better in the future

Backpropagation During Inference

Backpropagation is not usually used during inference. Inference means using a trained model to make predictions.

During Inference

Input goes forward

Prediction is produced

Weights do not update

During Training

Forward propagation

Loss calculation

Backpropagation

Weight update

Backpropagation vs Forward Propagation

ProcessDirectionPurpose
Forward PropagationInput to outputProduces a prediction
BackpropagationError moves backwardHelps update weights

Common Misconceptions

Misconceptions vs Reality

Misconception — Backpropagation is the same as gradient descent.

Reality — Backpropagation calculates gradients. Gradient descent uses gradients to update weights.

Misconception — Backpropagation happens during every prediction.

Reality — It usually happens during training, not normal inference.

Misconception — Backpropagation only updates the output layer.

Reality — It can update weights across many layers.

Common Mistakes

Common Mistakes
  • Confusing backpropagation with forward propagation
  • Thinking weights update before loss is calculated
  • Ignoring the role of the loss function
  • Forgetting that gradients guide weight updates
  • Thinking backpropagation only affects the last layer
  • Using a learning rate that is too high or too low
  • Ignoring vanishing gradients
  • Ignoring exploding gradients
  • Looking only at training loss
  • Forgetting to check validation performance

Summary

Key Takeaways
  • Backpropagation updates weights based on error.
  • Forward propagation produces a prediction.
  • The loss function measures how wrong the prediction is.
  • Backpropagation sends error information backward through the network.
  • Gradients show how weights affect loss.
  • Gradient descent uses gradients to update weights.
  • Learning rate controls the size of weight updates.
  • Backpropagation helps train hidden layers and output layers.
  • Backpropagation is used during training, not normal inference.
  • Backpropagation is essential for training deep neural networks.

Practice Prompt

A neural network predicts that an image is a cat, but the correct answer is dog. Explain how forward propagation, loss, backpropagation, and weight updates work together to help the model improve.

Need Help?

Ask the AI if you need help understanding backpropagation, gradients, weight updates, loss functions, gradient descent, learning rate, vanishing gradients, exploding gradients, or the difference between forward propagation and backpropagation.