Backpropagation is an efficient method for calculating the derivatives of a neural network’s cost function with respect to its weights and biases. The method is based on the chain rule of differentiation, which allows these derivatives to be calculated recursively, starting from the final layer and proceeding backward through the network.
The following derivation considers a fully connected neural network trained using cross-entropy loss. The final layer uses sigmoid activation, while the hidden layers use a differentiable activation function denoted by \(\sigma\).
Mathematical Description of the Network
Consider a neural network consisting of \(L\) layers of neurons, numbered \(1,2,\ldots,L\), with layer \(L\) being the final layer. The input is designated as layer \(0\).
Let \(Q\) denote the number of training examples. For training example \(q\), the preactivation of neuron \(i\) in layer \(k\) is \[\boxed{z_i^{k,q}=\sum_j w_{ij}^k a_j^{k-1,q}+b_i^k}\tag{1}\] where \(w_{ij}^k\) is the weight connecting neuron \(j\) in layer \(k-1\) to neuron \(i\) in layer \(k\), and \(b_i^k\) is the bias of neuron \(i\).
For the hidden layers \(k=1,\ldots,L-1\), the output of each neuron is \[\boxed{a_i^{k,q}=\sigma(z_i^{k,q})}\tag{2}\] where \(\sigma\) is a differentiable activation function.
For the input, \[a_i^{0,q}=x_i^q.\tag{3}\]
For the final layer, the activation function is the sigmoid, \[\boxed{a_i^{L,q}=\frac{1}{1+e^{-z_i^{L,q}}}}\tag{4}\]
The Cost Function
Let \(l_i^q\) denote the desired output of neuron \(i\) for training example \(q\). The total cross-entropy cost is defined as \[\boxed{ C=-\sum_{q=1}^{Q}\sum_{i=1}^{M} \left[ l_i^q\log a_i^{L,q} +(1-l_i^q)\log(1-a_i^{L,q}) \right] }\tag{5}\] where \(M\) denotes the number of neurons in the final layer.
The cost is a sum over all training examples and all output neurons. It measures the discrepancy between the desired outputs \(l_i^q\) and the network outputs \(a_i^{L,q}\).
This form is called binary cross-entropy. It treats each output neuron as an independent binary prediction.
Definition of Gamma
The central quantities in backpropagation are the derivatives of the cost function with respect to the preactivations of the neurons.
Define \[\boxed{\Gamma_i^{k,q}=\frac{\partial C}{\partial z_i^{k,q}}}\tag{6}\]
The quantity \(\Gamma_i^{k,q}\) measures how a small change in the preactivation \(z_i^{k,q}\) affects the total cost.
Once the gammas have been calculated, the derivatives with respect to the weights and biases can be obtained directly.
The computation of gammas begins at the final layer, where the cost function is explicitly defined in terms of the neuron outputs. The gammas of the preceding layers are then calculated recursively.
Gammas of the Final Layer
The final layer uses sigmoid activation, \[a_i^{L,q}=\frac{1}{1+e^{-z_i^{L,q}}}.\]
The gamma of an output neuron is \[\Gamma_i^{L,q}=\frac{\partial C}{\partial z_i^{L,q}}.\]
Since the sigmoid outputs are independent, the chain rule gives \[\Gamma_i^{L,q} =\frac{\partial C}{\partial a_i^{L,q}} \frac{\partial a_i^{L,q}}{\partial z_i^{L,q}}.\tag{7}\]
Differentiation of the cross-entropy cost yields \[\frac{\partial C}{\partial a_i^{L,q}} =-\frac{l_i^q}{a_i^{L,q}} +\frac{1-l_i^q}{1-a_i^{L,q}}.\tag{8}\]
The derivative of the sigmoid function is \[\frac{\partial a_i^{L,q}}{\partial z_i^{L,q}} =a_i^{L,q}(1-a_i^{L,q}).\tag{9}\]
Substituting these derivatives into (7), \[\Gamma_i^{L,q} =\left[-\frac{l_i^q}{a_i^{L,q}} +\frac{1-l_i^q}{1-a_i^{L,q}}\right] a_i^{L,q}(1-a_i^{L,q}).\]
Expanding, \[\Gamma_i^{L,q} =-l_i^q(1-a_i^{L,q})+(1-l_i^q)a_i^{L,q}.\]
Simplification gives \[\boxed{\Gamma_i^{L,q}=a_i^{L,q}-l_i^q}\tag{10}\]
Calculation rule — Final layer
To calculate the gamma of an output neuron, subtract its desired output from its actual output.
Backpropagation Through the Hidden Layers
Once the gammas of the final layer have been calculated, the gammas of the preceding layers can be determined recursively.
Consider neuron \(i\) in layer \(k\), where \(1\leq k\leq L-1\). Its preactivation \(z_i^{k,q}\) influences the cost through the neurons in layer \(k+1\).
Applying the multivariable chain rule, \[\boxed{ \Gamma_i^{k,q} =\sum_j \frac{\partial C}{\partial z_j^{k+1,q}} \frac{\partial z_j^{k+1,q}}{\partial z_i^{k,q}} }\tag{11}\]
The preactivation of neuron \(j\) in the following layer is \[z_j^{k+1,q}=\sum_m w_{jm}^{k+1}a_m^{k,q}+b_j^{k+1}.\]
Since \(a_m^{k,q}=\sigma(z_m^{k,q})\), this becomes \[z_j^{k+1,q}=\sum_m w_{jm}^{k+1}\sigma(z_m^{k,q})+b_j^{k+1}.\]
Differentiating with respect to \(z_i^{k,q}\), \[\boxed{ \frac{\partial z_j^{k+1,q}}{\partial z_i^{k,q}} =w_{ji}^{k+1}\sigma'(z_i^{k,q}) }\tag{12}\]
Substituting into (11), \[\Gamma_i^{k,q} =\sum_j\Gamma_j^{k+1,q}w_{ji}^{k+1}\sigma'(z_i^{k,q}).\]
Since \(\sigma'(z_i^{k,q})\) is independent of \(j\), it can be taken outside the summation: \[\boxed{ \Gamma_i^{k,q} =\sigma'(z_i^{k,q})\sum_j w_{ji}^{k+1}\Gamma_j^{k+1,q} }\tag{13}\]
Equation (13) is the fundamental backpropagation recurrence.
Calculation rule — Hidden layers
To calculate the gamma of a neuron, take the gammas of all neurons in the following layer, multiply each by the weight connecting the current neuron to that neuron, and add the results. Multiply this sum by the derivative of the current neuron’s activation function.
Starting with the final-layer gammas, equation (13) can be applied successively to layers \(L-1,L-2,\ldots,1\).
The recurrence does not depend on the particular cost function. The choice of cost function affects the calculation of the final-layer gammas, but not the recurrence used for the hidden layers.
Derivatives with Respect to the Weights
After calculating the gammas for all training examples, the derivatives of the total cost with respect to the network parameters can be obtained.
Consider the weight \(w_{ij}^k\), connecting neuron \(j\) in layer \(k-1\) to neuron \(i\) in layer \(k\).
Since the total cost is a sum over the training examples, \[C=\sum_{q=1}^{Q}C^q,\] its derivative with respect to the weight is \[\frac{\partial C}{\partial w_{ij}^k} =\sum_{q=1}^{Q}\frac{\partial C^q}{\partial w_{ij}^k}.\tag{14}\]
Applying the chain rule, \[\frac{\partial C}{\partial w_{ij}^k} =\sum_{q=1}^{Q} \frac{\partial C^q}{\partial z_i^{k,q}} \frac{\partial z_i^{k,q}}{\partial w_{ij}^k}.\tag{15}\]
The preactivation equation is \[z_i^{k,q}=\sum_m w_{im}^k a_m^{k-1,q}+b_i^k.\]
Differentiation with respect to \(w_{ij}^k\) gives \[\boxed{ \frac{\partial z_i^{k,q}}{\partial w_{ij}^k}=a_j^{k-1,q} }\tag{16}\]
By the definition of gamma, \[\frac{\partial C^q}{\partial z_i^{k,q}}=\Gamma_i^{k,q}.\]
Consequently, \[\boxed{ \frac{\partial C}{\partial w_{ij}^k} =\sum_{q=1}^{Q}\Gamma_i^{k,q}a_j^{k-1,q} }\tag{17}\]
This expression gives the derivative of the total cost with respect to any weight in the network.
Each training example contributes a term equal to the gamma of the receiving neuron multiplied by the activation of the sending neuron. These contributions are summed over the entire dataset.
Derivatives with Respect to the Biases
The derivative with respect to the bias \(b_i^k\) is obtained similarly.
Applying the chain rule, \[\frac{\partial C}{\partial b_i^k} =\sum_{q=1}^{Q} \frac{\partial C^q}{\partial z_i^{k,q}} \frac{\partial z_i^{k,q}}{\partial b_i^k}.\tag{18}\]
Since \[\frac{\partial z_i^{k,q}}{\partial b_i^k}=1,\] we obtain \[\boxed{ \frac{\partial C}{\partial b_i^k}=\sum_{q=1}^{Q}\Gamma_i^{k,q} }\tag{19}\]
Thus, the derivative of the total cost with respect to a bias is simply the sum of the corresponding gammas over all training examples.
Gradient Descent Update Rules
Gradient descent modifies the weights and biases in the direction opposite to the gradient of the cost function.
Let \(\epsilon>0\) denote the learning rate.
The weight update rule is \[w_{ij}^k\leftarrow w_{ij}^k-\epsilon\frac{\partial C}{\partial w_{ij}^k}.\]
Substituting (17), \[\boxed{ w_{ij}^k\leftarrow w_{ij}^k -\epsilon\sum_{q=1}^{Q}\Gamma_i^{k,q}a_j^{k-1,q} }\tag{20}\]
Similarly, the bias update rule is \[\boxed{ b_i^k\leftarrow b_i^k-\epsilon\sum_{q=1}^{Q}\Gamma_i^{k,q} }\tag{21}\]
Calculation rule — Weight update
To update a weight, multiply the gamma of the receiving neuron by the output of the sending neuron for each training example, and sum these products. Multiply the sum by the learning rate \(\epsilon\) and subtract it from the current weight.
Calculation rule — Bias update
To update a bias, sum the gammas of its neuron over all training examples, multiply the result by the learning rate \(\epsilon\), and subtract it from the current bias.
These rules apply to every neuron in every trainable layer, \(k=1,\ldots,L\). All derivatives are evaluated at the current weights and biases before the parameters are updated.
Because the cost function is defined as a sum rather than an average, the update rules contain sums over the training examples. If the mean cost \(C/Q\) is used instead, the gradients and corresponding update terms are divided by \(Q\).
Full-Batch and Stochastic Gradient Descent
The preceding equations describe full-batch gradient descent: all \(Q\) training examples contribute to each parameter update.
Stochastic gradient descent instead uses one randomly selected training example per update. If that example is \(q\), the update rules become \[\boxed{ w_{ij}^k\leftarrow w_{ij}^k-\epsilon\Gamma_i^{k,q}a_j^{k-1,q} }\tag{22}\] and \[\boxed{ b_i^k\leftarrow b_i^k-\epsilon\Gamma_i^{k,q} }\tag{23}\]
Mini-batch gradient descent uses a subset of the training examples and typically averages their contributions.
The backpropagation equations used to calculate the gammas are identical in all three cases. Only the number of training examples contributing to each parameter update changes.
Summary of the Backpropagation Algorithm
Backpropagation consists of three principal operations.
First, calculate the final-layer gammas: \[\boxed{\Gamma_i^{L,q}=a_i^{L,q}-l_i^q}\]
Second, propagate the gammas backward through the hidden layers: \[\boxed{ \Gamma_i^{k,q}=\sigma'(z_i^{k,q})\sum_j w_{ji}^{k+1}\Gamma_j^{k+1,q} }\]
Third, calculate the parameter gradients and update the weights and biases: \[\boxed{ \begin{aligned} w_{ij}^k&\leftarrow w_{ij}^k -\epsilon\sum_{q=1}^{Q}\Gamma_i^{k,q}a_j^{k-1,q},\\[8pt] b_i^k&\leftarrow b_i^k -\epsilon\sum_{q=1}^{Q}\Gamma_i^{k,q}. \end{aligned} }\]
The computational advantage of backpropagation is that each gamma is calculated once and subsequently reused in the calculation of the preceding layer’s gammas and the corresponding parameter gradients. This avoids repeatedly differentiating the entire network separately with respect to every weight and bias.
The resulting algorithm computes the gradient of the total cost efficiently, even for networks containing many layers and parameters.
9. Backpropagation Pseudocode
function forward(x):
a[0] = x
for k = 1 to L:
z[k] = W[k] * a[k-1] + b[k]
if k == L:
a[k] = sigmoid(z[k])
else:
a[k] = sigma(z[k])
return a, z
<p>
The backpropagation algorithm consists of two procedures:
<code>forward</code>, which calculates the activations
of the neurons, and <code>backward</code>, which calculates
the gradients of the cost function.
</p>
<h3>9.1. Forward Propagation</h3>
<p>
For each training example q, the forward procedure
calculates the preactivations and activations of all
neurons, proceeding from layer 1 to layer L.
</p>
<pre><code>function forward(x):
a[0] = x
for k = 1 to L:
z[k] = W[k] * a[k-1] + b[k]
if k == L:
a[k] = sigmoid(z[k])
else:
a[k] = sigma(z[k])
return a, z</code></pre>
<h3>9.2. Backward Propagation</h3>
<p>
The backward procedure begins by calculating the
gammas of the final layer. It then propagates these
gammas backward through the remaining layers and
calculates the gradients with respect to the weights
and biases.
</p>
<pre><code>function backward(a, z, l):
Gamma[L] = a[L] – l
for k = L-1 downto 1:
Gamma[k] = sigma_prime(z[k]) *
(transpose(W[k+1]) * Gamma[k+1])
for k = 1 to L:
dW[k] = Gamma[k] * transpose(a[k-1])
db[k] = Gamma[k]
return dW, db</code></pre>
<p>
In the gamma recurrence, multiplication by
<code>sigma_prime</code> is elementwise. The calculation
of <code>dW[k]</code> is an outer product.
</p>
<h3>9.3. Training Algorithm</h3>
<p>
The complete training algorithm applies forward and
backward propagation to every training example,
accumulates the gradients, and updates the weights
and biases.
</p>
<pre><code>initialize W, b
repeat for each epoch:
for k = 1 to L:
total_dW[k] = 0
total_db[k] = 0
for q = 1 to Q:
a, z = forward(x[q])
dW, db = backward(a, z, l[q])
for k = 1 to L:
total_dW[k] += dW[k]
total_db[k] += db[k]
for k = 1 to L:
W[k] = W[k] – epsilon * total_dW[k]
b[k] = b[k] – epsilon * total_db[k]</code></pre>
<p>
The algorithm performs full-batch gradient descent,
since the weights and biases are updated after
processing all \(Q\) training examples.
</p>
<p>
For stochastic gradient descent, the parameter
updates are instead performed immediately after
each call to <code>backward</code>, without accumulating
gradients over the entire dataset.
</p>