The backpropagation process contains reasonably complex mathematics (at least for people without an extensive background in maths). Even if you can grasp the individual formulas and equations involved, their overall, high level effect can be difficult to see, as the examples given in machine learning courses often involve large matrices of weights and activation values (e.g with written character recognition). This means that the reason weights change by certain values can be hard to see and understand. To compound this, the implementation of the formulas involved is often abstracted inside large matrix multiplications, so it can be hard to follow individual granular steps in a given equation.
The SimpleML.BasicFeedForwardNeuralNetwork class implements gradient descent using backpropagation in its Train() method. Its implementation of backpropagation is kept simple and easy to understand by the following features...
Throughout this page I'll refer to Peter Sadowski's document 'Notes on Backpropagation' which is one of the most straightforward explanations of backpropagation I've found.
This section explains the core entities and mathematical functions involved in the backpropagation process...
The cost function is a fundamental element in backpropagation, and any gradient descent process. It's used to measure the difference between the actual result values of a set of training data, and the activation values produced by the neural network on the same set of training data. There are several different cost functions which can be applied to neural networks and other machine learning techniques. BackPropagationVisualization uses the cross-entropy cost function. Squared error and quadratic are other types of cost function.
The derivative of the cost function is used in backpropagation to find the amount/degree that a change in one of the weights, produces a change in the overall cost. During the backpropagation process this amount of change is used to update the weights.
This is a mathematical function that produces the output value of a single neuron/unit in the network, based on the input and weight values which feed into that neuron. The sigmoid function is used in BasicFeedForwardNeuralNetwork, but there are many other variations.
These are the known correct results for each of the training examples. In the context of BackPropagationVisualization, the target values are always 0 or 1, representing logical false and true.
A neural network is often depicted with the neurons/units in each layer as circles and the weights between them as lines. What's sometimes not clear from these diagrams is that the line representing each weight has a value attached to it, and is equally important as the neurons it connects. The diagram below shows how the elements in a typical neural network diagram map to the UI elements in BackPropagationVisualization.
Firstly we'll define some of the parameters used in the training...
Batch Size - This is the number of training examples that are pushed through the backpropagation process before the weights are updated. The batch size must be less than or equal to the number of training examples.
Number of Epochs - Completing 1 epoch means pushing all the training examples through the process once. Since there are always 4 training examples in BackPropagationVisualization, 1 epoch is completed each time all 4 of these training examples have been processed.
Note that the batch size can be less than the total number of training examples, and it's possible to set a batch size not equally divisible into the number of training examples. For example, using a batch size of 3, with 4 epochs will result in 6 total updates of the weights and a final 'partial' batch as depicted below...
The following section describes the backpropagation process implemented in the SimpleML.BasicFeedForwardNeuralNetwork class step by step...
At the start of the Train() method, 2-dimensional arrays 'inputToHiddenLayerGradients' and 'hiddenToOutputLayerGradients' are declared, which hold the gradients corresponding to each of the weights in the network...
During the forward pass, the input values are multiplied forward through the layers of the network, producing a logit (n.b. not 100% sure if I'm using the correct terminology here) and then activation value at each unit in the hidden layer, and then the output layer. The following nested for loops calculate the activation (sigmoid function) value for each of the hidden layer units...
The for loops map to the following UI and equation components (below equations are taken from page 1 of 'Notes on Backpropagation')...
Then a similar routine is used to calculate the activation value for the output unit
Finally the method CalculateCost() is used to calculate the cost difference between the output unit activation value, and the actual result of the current training example (again, the cost function equation is taken from page 1 of 'Notes on Backpropagation')...
In the backward pass the derivative of the cost function (gradient) is calculated for each neuron/unit in the network. This shows how much a change in each unit will affect the cost function. First the hidden to output layer gradients are calculated according to the equation below (see page 2 of 'Notes on Backpropagation')...
Then the input to hidden layer gradients are calculated as per below (again page 2 of 'Notes on Backpropagation') ...
The above forward and backward pass process is repeated for the specified batch size. The gradient values for each training example in the batch are accumulated into arrays 'inputToHiddenLayerGradients' and 'hiddenToOutputLayerGradients'. When the batch is completed, the gradients are averaged over the batch size by the DivideArrayElements() method.
Finally, the weight layers are updated by simply subtracting the corresponding gradient, multiplied by the learning rate...