Backpropagation Algorithm


Backpropagation Algorithm

Corey
Today we’re getting into the mathematical steps of backpropagation, especially what happens during forward propagation. The key idea is that a neural network first produces an output, compares it with the target, and then uses the error to guide weight updates. We’ll work through the forward-pass example and then clarify what the backward pass is meant to do.

Michael
And this builds on the earlier ideas: neural-network architecture, the cost function, gradient descent, and the sigmoid activation function. The example has an input layer, a hidden layer, and an output layer. The inputs are called i1 and i2, the hidden nodes h1 and h2, and the outputs o1 and o2.

Corey
Before following the connections, remember the two operations that happen inside each neuron. First, it calculates a weighted sum of its incoming values. For example, a neuron receiving x1 and x2 forms a sum like w1 times x1 plus w2 times x2, then adds a bias.

Michael
And the bias is treated as a weight on an extra input whose value is one. So a bias term b becomes b times 1. After calculating the weighted sum, the neuron passes it through an activation function—in this example, the sigmoid.

Corey
The sigmoid function is one over one plus e to the minus x. It squashes its input into a value between zero and one, which is why it can turn a weighted sum into a neuron’s output. The derivative we’ll need when discussing weight updates is sigma times one minus sigma.

Michael
That derivative matters because the backward pass needs to work out how changes in weights affect the error. The sigmoid is also important because it introduces nonlinearity into the network. The material also names tanh and ReLU as other activation functions; for tanh, the stated output range is minus one to one.

Corey
Let’s make the direction of travel clear. In forward propagation, information moves from the inputs through the hidden layer to the outputs. The network calculates predicted values at o1 and o2, then compares those predictions with the target values, which are 0.01 and 0.99 in this example.

Michael
Backward propagation goes in the opposite direction through the network. Its purpose is to use the error to update weights and biases with gradient descent, aiming to reduce the cost. Then the network can run forward again with the updated parameters, check the new error, and repeat.

Corey
So the overall training loop is forward pass, calculate cost, backward pass, update parameters, and repeat. The source describes repeating this process many times—perhaps thousands of iterations—until the error is sufficiently small. The detailed numerical calculation here, though, focuses on the forward pass.

Michael
For the example, the two inputs are 0.05 and 0.10. To calculate the hidden neuron h1, take each input, multiply it by the weight on its connection, and add the bias. The values shown are weights 0.15 and 0.20, with a bias of 0.35.

Corey
That gives net h1 equals 0.15 times 0.05, plus 0.20 times 0.10, plus 0.35 times 1. The weighted sum is 0.3775. We call it net h1 because it is the total input arriving at that neuron before the activation function.

Michael
Now apply the sigmoid to that sum: one over one plus e to the minus 0.3775. The result is about 0.59327, so that is the output of h1. It’s useful to keep the two quantities distinct: net h1 is the weighted sum, while the output of h1 is the sigmoid-transformed value.

Corey
Then do the same two operations for h2: calculate its weighted sum from the incoming inputs and bias, and pass that sum through the sigmoid. The example’s hidden-layer output for h2 is about 0.59688. Once both hidden outputs are available, they become the inputs to the output layer.

Michael
For o1, the weighted sum uses the outputs of h1 and h2, not the original inputs. Using the example’s values, net o1 is 0.40 times output h1, plus 0.45 times output h2, plus the output-layer bias 0.60 times 1. That gives approximately 1.10591.

Corey
And again, the neuron applies the sigmoid. So output o1 is one over one plus e to the minus net o1, which comes to about 0.75137. For o2, follow the same process with its own connection weights and bias; the example gives an output of about 0.77293.

Michael
Now compare those predictions with the targets: 0.01 for o1 and 0.99 for o2. The cost used in the example for each output is one half times the target minus the predicted output, squared. Because the difference is squared, writing predicted minus target instead gives the same result.

Corey
For o1, the target is 0.01 and the prediction is about 0.75137, so its squared-error contribution, including the one-half factor, is about 0.27481. For o2, the target is 0.99 and the prediction is about 0.77293, giving about 0.02356.

Michael
Add those two output-node costs and the total error is about 0.29837. That completes the forward pass: calculate the hidden activations, calculate the output activations, and use the predictions and targets to calculate the cost.

Corey
Then the backward pass uses that cost to determine how to adjust the network’s weights and biases. Gradient descent makes those updates in the direction intended to lower the cost. With the updated values, the network performs another forward pass and checks whether the error has decreased.

Michael
One important distinction: this example shows the forward-pass arithmetic and explains the purpose of the backward pass, but it doesn’t work through the individual backward-pass derivatives and updates. The essential sequence is still clear: predict, measure the cost, use the error to update parameters, and repeat until the model’s error is low enough for prediction.

Ask anything
We've read this whole study set, so questions about it get the best answers.