1. Home
  2. Explore
  3. Computer Science
  4. Backpropagation Algorithm

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Backpropagation Algorithm

Forward Propagation and the Purpose of Backpropagation

How an artificial neuron works

An artificial neuron, or perceptron, performs two main operations:

  1. Calculate a weighted sum of its inputs, including a bias: $$ z = \sum_i w_i x_i + b $Here,Here,Here,x_iaretheinputs,are the inputs,aretheinputs,w_iaretheirweights,andare their weights, andaretheirweights,andbisthebias.Thebiascanberepresentedasaweightmultipliedbyaconstantinputofis the bias. The bias can be represented as a weight multiplied by a constant input ofisthebias.Thebiascanberepresentedasaweightmultipliedbyaconstantinputof1$.

  2. Apply an activation function to the weighted sum: $$ a = \sigma(z) $TheresultThe resultTheresulta$ is the neuron's output, also called its activation.

The activation function helps introduce nonlinearity into the network. Without nonlinear activations, stacking layers would still produce a function equivalent to a single linear transformation, limiting what the network could learn.

Sigmoid activation

The example network uses the sigmoid function:

σ(x)=11+e−x\sigma(x) = \frac{1}{1 + e^{-x}}σ(x)=1+e−x1​

Sigmoid maps any real-valued input to a value strictly between 000 and 111. Its derivative is:

dσdx=σ(x)(1−σ(x))\frac{d\sigma}{dx} = \sigma(x)\bigl(1-\sigma(x)\bigr)dxdσ​=σ(x)(1−σ(x))

Equivalently, if a=σ(z)a=\sigma(z)a=σ(z), then:

dadz=a(1−a)\frac{da}{dz} = a(1-a)dzda​=a(1−a)

This derivative is useful during backpropagation because it describes how a neuron's output changes when its weighted sum changes.

Other activation functions include hyperbolic tangent, whose outputs range from −1-1−1 to 111, and ReLU. The choice of activation function affects the behavior and learning of the network.


Example network and parameters

The network has three layers:

  • Input layer: two inputs, i1i_1i1​ and i2i_2i2​.

  • Hidden layer: two neurons, h1h_1h1​ and h2h_2h2​.

  • Output layer: two neurons, o1o_1o1​ and o2o_2o2​.

The arrows between neurons represent trainable weights. Each hidden and output neuron also has a bias.

For the worked example, the inputs and targets are:

i1=0.05,i2=0.10i_1=0.05,\qquad i_2=0.10i1​=0.05,i2​=0.10
t1=0.01,t2=0.99t_1=0.01,\qquad t_2=0.99t1​=0.01,t2​=0.99

Here, t1t_1t1​ and t2t_2t2​ are the target values—the desired outputs against which the network’s predictions are compared.

The example uses these weights and biases:

  • From the inputs to the hidden layer:

    w1=0.15,w2=0.20,w3=0.25,w4=0.30,b1=0.35w_1=0.15,\quad w_2=0.20,\quad w_3=0.25,\quad w_4=0.30,\quad b_1=0.35w1​=0.15,w2​=0.20,w3​=0.25,w4​=0.30,b1​=0.35
  • From the hidden layer to the output layer:

    w5=0.40,w6=0.45,w7=0.50,w8=0.55,b2=0.60w_5=0.40,\quad w_6=0.45,\quad w_7=0.50,\quad w_8=0.55,\quad b_2=0.60w5​=0.40,w6​=0.45,w7​=0.50,w8​=0.55,b2​=0.60

The bias value is added to each neuron’s weighted input. In this example, the same bias value is used for the neurons within a layer.


Forward propagation

Forward propagation sends information from the input layer through the hidden layer to the output layer. At each hidden or output neuron, the network first calculates a weighted sum and then applies the activation function. Once the output layer produces predictions, those predictions can be compared with the targets to calculate the error.

1. Calculate the hidden-layer weighted sums

For the first hidden neuron, the weighted sum is:

zh1=w1i1+w2i2+b1z_{h_1}=w_1i_1+w_2i_2+b_1zh1​​=w1​i1​+w2​i2​+b1​

Substituting the example values:

zh1=(0.15)(0.05)+(0.20)(0.10)+0.35=0.3775z_{h_1}=(0.15)(0.05)+(0.20)(0.10)+0.35=0.3775zh1​​=(0.15)(0.05)+(0.20)(0.10)+0.35=0.3775

The second hidden neuron receives its own weights:

zh2=w3i1+w4i2+b1z_{h_2}=w_3i_1+w_4i_2+b_1zh2​​=w3​i1​+w4​i2​+b1​
zh2=(0.25)(0.05)+(0.30)(0.10)+0.35=0.3925z_{h_2}=(0.25)(0.05)+(0.30)(0.10)+0.35=0.3925zh2​​=(0.25)(0.05)+(0.30)(0.10)+0.35=0.3925

2. Apply sigmoid to the hidden-layer sums

The hidden neurons’ outputs are:

ah1=σ(zh1)=11+e−0.3775≈0.593270a_{h_1}=\sigma(z_{h_1}) =\frac{1}{1+e^{-0.3775}} \approx 0.593270ah1​​=σ(zh1​​)=1+e−0.37751​≈0.593270
ah2=σ(zh2)=11+e−0.3925≈0.596884a_{h_2}=\sigma(z_{h_2}) =\frac{1}{1+e^{-0.3925}} \approx 0.596884ah2​​=σ(zh2​​)=1+e−0.39251​≈0.596884

The values ah1a_{h_1}ah1​​ and ah2a_{h_2}ah2​​ become the inputs to the output layer.

3. Calculate the output-layer weighted sums

The first output neuron receives signals from both hidden neurons:

zo1=w5ah1+w6ah2+b2z_{o_1}=w_5a_{h_1}+w_6a_{h_2}+b_2zo1​​=w5​ah1​​+w6​ah2​​+b2​
zo1=(0.40)(0.593270)+(0.45)(0.596884)+0.60≈1.105906z_{o_1}=(0.40)(0.593270)+(0.45)(0.596884)+0.60 \approx 1.105906zo1​​=(0.40)(0.593270)+(0.45)(0.596884)+0.60≈1.105906

The second output neuron uses its own incoming weights:

zo2=w7ah1+w8ah2+b2z_{o_2}=w_7a_{h_1}+w_8a_{h_2}+b_2zo2​​=w7​ah1​​+w8​ah2​​+b2​
zo2=(0.50)(0.593270)+(0.55)(0.596884)+0.60≈1.224921z_{o_2}=(0.50)(0.593270)+(0.55)(0.596884)+0.60 \approx 1.224921zo2​​=(0.50)(0.593270)+(0.55)(0.596884)+0.60≈1.224921

4. Apply sigmoid to obtain predictions

The network’s predicted outputs are:

y^1=σ(zo1)≈0.751365\hat{y}_1=\sigma(z_{o_1}) \approx 0.751365y^​1​=σ(zo1​​)≈0.751365
y^2=σ(zo2)≈0.772928\hat{y}_2=\sigma(z_{o_2}) \approx 0.772928y^​2​=σ(zo2​​)≈0.772928

The hats indicate that these are predictions, not target values. The targets remain t1=0.01t_1=0.01t1​=0.01 and t2=0.99t_2=0.99t2​=0.99.


Measuring prediction error

A cost function measures how far the network’s predictions are from the target values. For each output neuron, the example uses half the squared error:

Ej=12(tj−y^j)2E_j=\frac{1}{2}(t_j-\hat{y}_j)^2Ej​=21​(tj​−y^​j​)2

Squaring the difference makes the contribution nonnegative, whether the prediction is above or below the target. The factor 12\frac{1}{2}21​ is convenient when differentiating the squared-error expression.

For the first output:

E1=12(0.01−0.751365)2≈0.274811E_1=\frac{1}{2}(0.01-0.751365)^2 \approx 0.274811E1​=21​(0.01−0.751365)2≈0.274811

For the second output:

E2=12(0.99−0.772928)2≈0.023560E_2=\frac{1}{2}(0.99-0.772928)^2 \approx 0.023560E2​=21​(0.99−0.772928)2≈0.023560

The total cost is the sum of the two output errors:

Etotal=E1+E2≈0.298371E_{\text{total}}=E_1+E_2 \approx 0.298371Etotal​=E1​+E2​≈0.298371

The sign convention for the difference does not change these squared-error values: squaring either tj−y^jt_j-\hat{y}_jtj​−y^​j​ or y^j−tj\hat{y}_j-t_jy^​j​−tj​ gives the same result.

Forward propagation produces predictions and uses the cost function to quantify their difference from the targets.


Backpropagation and weight updates

Backpropagation uses the output error to determine how the network’s weights and biases should change. It works backward from the output layer toward the input layer, tracing how the parameters contributed to the error.

The learning cycle is:

  1. Perform forward propagation to compute predictions.

  2. Compare predictions with targets to calculate the cost.

  3. Use backpropagation to determine how the weights and biases contributed to that cost.

  4. Use gradient descent to adjust the weights and biases in directions that reduce the cost.

  5. Perform forward propagation again with the updated parameters and measure the new cost.

  6. Repeat the process until the cost is sufficiently small or another stopping condition is met.

Gradient descent uses information about the cost’s rate of change with respect to each parameter. The sigmoid derivative, σ(z)(1−σ(z))\sigma(z)(1-\sigma(z))σ(z)(1−σ(z)), is one component used when calculating these changes for sigmoid neurons.

The process is iterative: each update aims to reduce the cost, and the updated parameters are tested by another forward pass. Repetition can yield a network that predicts more accurately for the training examples. Gradient descent aims to minimize the cost, though it does not guarantee that every run will find a global minimum.

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent