Multilayer Perceptrons and Backpropagation › Example
Machine Learning from ScratchAll chapters Multilayer Perceptrons and Backpropagation › ExampleSection view · Sections in this chapter5 / 10 ↓
Reading tools Definitions, links & notes
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
To make the formula concrete, we compute it by hand. The following example is deliberately tiny: two input features, two hidden units, and one binary output. Its purpose is not to be realistic but to expose every number that the code will later compute. We will carry the forward pass, the backward pass, and one gradient step by hand, so that each matrix in the implementation has a concrete scalar counterpart. Calculations retain full precision; displayed results are rounded to four decimal places.
Suppose we have two input features, two hidden units, and one binary output. Let the input be
Let the hidden weights and biases be
and let the output weights and bias be
We use in the hidden layer and sigmoid at the output.
Forward pass
We compute the hidden preactivations one unit at a time. For the first hidden unit,
Then
For the second hidden unit,
so
The output logit is
The predicted probability is
where:
- : the sigmoid function mapping the logit to a probability
- : the predicted probability that the example belongs to class
The loss is
where:
- : the binary cross-entropy loss for this single example
- : the natural logarithm
Equivalently, using the stable logit form,
We can read this forward pass as a pipeline: the input is projected into two hidden numbers, each hidden number is squashed by tanh, and the two squashed numbers are combined into a logit. The predicted probability is above , so the network already leans toward class , and the observed label is . Since the assigned probability is below , its binary cross-entropy is positive.
Backward pass
We now reverse the forward pass. The output gradient is
The output weight gradients are
where:
- : the output delta for this single example, without the scaling used for a batch mean
- : the hidden activations computed in the forward pass
- : the output weights
and the output bias gradient is
For the hidden units, we first compute
The tanh derivatives are
Therefore the hidden preactivation gradients are
Now we can compute the weight gradients. Since ,
Similarly,
The hidden bias gradients are simply and :
At this point every gradient has been computed by following the chain rule one link at a time. The output delta is the derivative of the loss with respect to the logit; its sign tells us which local change in the logit would reduce the loss. Multiplying by the hidden activations tells us how to change the output weights. Multiplying the output delta by the output weights and tanh derivative gives the loss gradient with respect to each hidden preactivation. Finally, multiplying by the input tells us how to change the hidden weights.
One gradient step
Gradient descent subtracts a multiple of the gradient. With learning rate , the new first hidden weight is
where:
- : the updated hidden weight after the gradient step
- : the initial value of
- : the learning rate
- : the gradient
The new first output weight is
where:
- : the updated first output weight after the gradient step
- : the initial value of
- : the learning rate
- : the rounded gradient
Applying the same rule at full precision and rounding the displayed values gives
A new forward pass with these updated parameters gives , , , , , , and loss . The loss decreased from about to about , which is the expected effect of a step in the negative gradient direction. It is a small confirmation that the signs and scales in the backward pass are consistent with the goal of reducing the loss.
Comments
Add to the conversation
A question, a correction, or an example from your own work.
No comments yet. Be the first to share your thoughts!