{"slug": "a-step-by-step-backpropagation-example", "title": "A Step by Step Backpropagation Example", "summary": "Matt Mazur's tutorial 'A Step by Step Backpropagation Example' provides a concrete numerical example of the backpropagation algorithm for training a neural network, using a network with two inputs, two hidden neurons, two output neurons, and biases. The tutorial walks through the forward pass, calculating the total error (0.298371109), and the backward pass to update weights, with a Python implementation available on GitHub. Mazur also promotes his new website Emergent Mind, which uses GPT-4 to explain AI/ML papers.", "body_md": "## Background\n\nBackpropagation is a common method for training a neural network. There is [no shortage of papers](https://www.google.com/search?q=backpropagation+algorithm) online that attempt to explain how backpropagation works, but few that include an example with actual numbers. This post is my attempt to explain how it works with a concrete example that folks can compare their own calculations to in order to ensure they understand backpropagation correctly.\n\n## Backpropagation in Python\n\nYou can play around with a Python script that I wrote that implements the backpropagation algorithm in [this Github repo](https://github.com/mattm/simple-neural-network).\n\n## Continue learning with Emergent Mind\n\nIf you find this tutorial useful and want to continue learning about AI/ML, **I encourage you to check out **[ Emergent Mind,](https://www.emergentmind.com/) a new website I’m working on that uses GPT-4 to surface and explain cutting-edge AI/ML papers:\n\nIn time, I hope to use AI to explain complex AI/ML topics on Emergent Mind in a style similar to what you’ll find in the tutorial below.\n\nNow, on with the backpropagation tutorial…\n\n## Overview\n\nFor this tutorial, we’re going to use a neural network with two inputs, two hidden neurons, two output neurons. Additionally, the hidden and output neurons will include a bias.\n\nHere’s the basic structure:\n\nIn order to have some numbers to work with, here are the initial weights, the biases, and training inputs/outputs:\n\nThe goal of backpropagation is to optimize the weights so that the neural network can learn how to correctly map arbitrary inputs to outputs.\n\nFor the rest of this tutorial we’re going to work with a single training set: given inputs 0.05 and 0.10, we want the neural network to output 0.01 and 0.99.\n\n## The Forward Pass\n\nTo begin, lets see what the neural network currently predicts given the weights and biases above and inputs of 0.05 and 0.10. To do this we’ll feed those inputs forward though the network.\n\nWe figure out the *total net input* to each hidden layer neuron, *squash* the total net input using an *activation function* (here we use the *logistic function*), then repeat the process with the output layer neurons.\n\n*net input*by\n\n[some sources](http://web.cs.swarthmore.edu/~meeden/cs81/s10/BackPropDeriv.pdf).\n\nHere’s how we calculate the total net input for :\n\nWe then squash it using the logistic function to get the output of :\n\nCarrying out the same process for we get:\n\nWe repeat this process for the output layer neurons, using the output from the hidden layer neurons as inputs.\n\nHere’s the output for :\n\nAnd carrying out the same process for we get:\n\n### Calculating the Total Error\n\nWe can now calculate the error for each output neuron using the [squared error function](http://en.wikipedia.org/wiki/Backpropagation#Derivation) and sum them to get the total error:\n\n[1](http://en.wikipedia.org/wiki/Backpropagation#Derivation)].\n\nFor example, the target output for is 0.01 but the neural network output 0.75136507, therefore its error is:\n\nRepeating this process for (remembering that the target is 0.99) we get:\n\nThe total error for the neural network is the sum of these errors:\n\n## The Backwards Pass\n\nOur goal with backpropagation is to update each of the weights in the network so that they cause the actual output to be closer the target output, thereby minimizing the error for each output neuron and the network as a whole.\n\n### Output Layer\n\nConsider . We want to know how much a change in affects the total error, aka .\n\nBy applying the [chain rule](http://en.wikipedia.org/wiki/Chain_rule) we know that:\n\nVisually, here’s what we’re doing:\n\nWe need to figure out each piece in this equation.\n\nFirst, how much does the total error change with respect to the output?\n\nis sometimes expressed as\n\nWhen we take the partial derivative of the total error with respect to , the quantity becomes zero because does not affect it which means we’re taking the derivative of a constant which is zero.\n\nNext, how much does the output of change with respect to its total net input?\n\nThe partial [derivative of the logistic function](http://en.wikipedia.org/wiki/Logistic_function#Derivative) is the output multiplied by 1 minus the output:\n\nFinally, how much does the total net input of change with respect to ?\n\nPutting it all together:\n\nYou’ll often see this calculation combined in the form of the [delta rule](http://en.wikipedia.org/wiki/Delta_rule):\n\nAlternatively, we have and which can be written as , aka (the Greek letter delta) aka the *node delta*. We can use this to rewrite the calculation above:\n\nTherefore:\n\nSome sources extract the negative sign from so it would be written as:\n\nTo decrease the error, we then subtract this value from the current weight (optionally multiplied by some learning rate, eta, which we’ll set to 0.5):\n\n[Some](http://en.wikipedia.org/wiki/Delta_rule)\n\n[sources](http://aima.cs.berkeley.edu/)use (alpha) to represent the learning rate,\n\n[others use](https://www4.rgu.ac.uk/files/chapter3%20-%20bp.pdf)(eta), and\n\n[others](http://web.cs.swarthmore.edu/~meeden/cs81/s10/BackPropDeriv.pdf)even use (epsilon).\n\nWe can repeat this process to get the new weights , , and :\n\nWe perform the actual updates in the neural network *after* we have the new weights leading into the hidden layer neurons (ie, we use the original weights, not the updated weights, when we continue the backpropagation algorithm below).\n\n### Hidden Layer\n\nNext, we’ll continue the backwards pass by calculating new values for , , , and .\n\nBig picture, here’s what we need to figure out:\n\nVisually:\n\nWe’re going to use a similar process as we did for the output layer, but slightly different to account for the fact that the output of each hidden layer neuron contributes to the output (and therefore error) of multiple output neurons. We know that affects both and therefore the needs to take into consideration its effect on the both output neurons:\n\nStarting with :\n\nWe can calculate using values we calculated earlier:\n\nAnd is equal to :\n\nPlugging them in:\n\nFollowing the same process for , we get:\n\nTherefore:\n\nNow that we have , we need to figure out and then for each weight:\n\nWe calculate the partial derivative of the total net input to with respect to the same as we did for the output neuron:\n\nPutting it all together:\n\nYou might also see this written as:\n\nWe can now update :\n\nRepeating this for , , and\n\nFinally, we’ve updated all of our weights! When we fed forward the 0.05 and 0.1 inputs originally, the error on the network was 0.298371109. After this first round of backpropagation, the total error is now down to 0.291027924. It might not seem like much, but after repeating this process 10,000 times, for example, the error plummets to 0.0000351085. At this point, when we feed forward 0.05 and 0.1, the two outputs neurons generate 0.015912196 (vs 0.01 target) and 0.984065734 (vs 0.99 target).\n\nIf you’ve made it this far and found any errors in any of the above or can think of any ways to make it clearer for future readers, don’t hesitate to [drop me a note](https://mattmazur.com/contact/). Thanks!\n\n### And while I have you…\n\nAgain, if you liked this tutorial, please check out [Emergent Mind](https://www.emergentmind.com), a site I’m building with an end goal of explaining AI/ML concepts in a similar style as this post. Feedback very much welcome!", "url": "https://wpnews.pro/news/a-step-by-step-backpropagation-example", "canonical_source": "https://mattmazur.com/2015/03/17/a-step-by-step-backpropagation-example/", "published_at": "2026-08-22 12:14:53+00:00", "updated_at": "2026-08-22 12:43:28.232044+00:00", "lang": "en", "topics": ["machine-learning", "neural-networks", "artificial-intelligence"], "entities": ["Matt Mazur", "Emergent Mind", "GPT-4", "GitHub"], "alternates": {"html": "https://wpnews.pro/news/a-step-by-step-backpropagation-example", "markdown": "https://wpnews.pro/news/a-step-by-step-backpropagation-example.md", "text": "https://wpnews.pro/news/a-step-by-step-backpropagation-example.txt", "jsonld": "https://wpnews.pro/news/a-step-by-step-backpropagation-example.jsonld"}}