Neural Network
Backpropagation
2023-12-27
In the blog "Fundamentals of NN in Excel", we show the diagram from the input layer to the output layer and its formulation as follows:

\(y'=\phi_v\lbrace V_j\phi(X_iw_{ij}-b_j)-b_v\rbrace \)
It was also noted that there are three key building components to obtain the coefficients (wij, bj, Vj, bv), the weights and the bias, and they are (1) the formulation of the Cost function (2) Stochastic Gradient Descent, (3) back propagation algorithm.
The cost function, J, to be minimized is defined as the total sum square error between the measurements and the predictions,
\(J=\dfrac{1}{2}\sum _{\ }^{\ }(y-y')^2\)
and we use the Excel's Solver feature to find the weights and biases. In this blog, we will directly use the (2) Gradient descent method and (3) back propagation algorithm to calculate those coefficients.
The difference between Gradient descent and the Stochastic Gradient Descent is how we use the training data. When the sample size is small, then use the gradient descent for the accuracy, but if the sample size is large, grouping the data into many of mini batches and pick any random batch to update the weights (rather than using all the dataset), which will dramatically improve the performance due to far less amount of the computing time.
In summary, we use the gradient descent to update the coefficients (b, V, w) iteratively until they get converged, and use the back propagation to calculate the partial derivatives.
Following are the gradient descent formulae for the coefficients to update:
\(w_{ij}^{new}=w_{ij}^{old}-\eta(\partial J/\partial w_{ij})\)
\(b_j^{new}=b_j^{old}-\eta(\partial J/\partial b_j)\)
\(V_j^{new}=V_j^{old}-\eta(\partial J/\partial V_j)\)
\(b_v^{new}=b_v^{old}-\eta(\partial J/\partial b_v)\)
where η is the learning rate between 0 and 1 and is an empirical number.
To calculate partial derivatives, the chain rule is used. The chain rule is a method for finding the derivative of nested functions. If function f is a function of g, denoted as f = f(g), and function g is a function of x, denoted as g = g(x), then the derivative of f(g(x)) with respect to x is
\(\dfrac{df}{dx}=\dfrac{df}{dg}\dfrac{dg}{dx}\)
In the case of the neural network, first break down the functions between the layers like below:
- Input layer to the dot product of hidden layer: \(S_j=X_iw_{ij}-b_j \)
- Output of the hidden layer: \(h_j=h(S_j)=\phi(S_j)\)
- Output layer of the hidden layer to the dot product of output layer: \(T=V_jh_j-b_v\)
- Output of the output layer: \(y'=g(T)=\phi_v(T)\)
Now using the chain rule, the partial derivative of wij, ∂J/∂wij can be expressed as
\(\partial J/\partial w_{ij}=(\partial J/\partial y')(\partial y'/\partial T)(\partial T/\partial h_j)(\partial h_j/\partial S_j)(\partial S_j/\partial w_{ij})\)
Here, wij and Vj, bi, bv are independent each other, and their derivates to them become 0, and all the individual terms are defined as follows:
- \(\partial S_j/\partial w_{ij}=X_i\)
- \(\partial h_j/\partial S_j=\phi_j(1-\phi_j)\) when the Sigmoid is used as the activation function in the hidden layer.
- \(\partial T/\partial h_j=V_j\)
- \(\partial y'/\partial T=1\) when the RELU is used as the activation function for the output layer, or
- \(\partial y'/\partial T=\phi_v(1-\phi_v)\) if the Sigmoid or Softmax is used as the activation function.
- \(\partial J/\partial y'=\sum _{\ }^{\ }(y-y')(-1)\)
If we put them together, assuming the Sigmoid is the activation function of the output layer, the ∂J/∂wij becomes
\(\partial J/\partial w_{ij}=\sum _{\ }^{\ }(y-y')(-1)\phi_v(1-\phi_v)V_j\phi_j(1-\phi_j)X_i\). Here, \(\sum_{\ }^{\ }\) means the sum over the all the training samples.
Likewise, the other terms can be derived in the same manner.
\(\partial J/\partial b_j=(\partial J/\partial y')(\partial y'/\partial T)(\partial T/\partial h_j)(\partial h_j/\partial S_j)(\partial S_j/\partial b_j)=\sum _{\ }^{\ }(y-y')(-1)\phi_v(1-\phi_v)V_j\phi_j^h(1-\phi_j^h)(-1)\)
\(\partial J/\partial V_j=(\partial J/\partial y')(\partial y'/\partial T)(\partial T/\partial V_j)=\sum _{\ }^{\ }(y-y')(-1)\phi_v(1-\phi_v)\phi_j^h\)
\(\partial J/\partial b_v=(\partial J/\partial y')(\partial y'/\partial T)(\partial T/\partial b_v)=\sum _{\ }^{\ }(y-y')(-1)\phi_v(1-\phi_v)(-1)\)
Each weight and threshold start with the error of the output layer at that moment Σ(y-y'), and the derivative is obtained by multiplying each term backward as follows. This process of obtaining the derivative is called backpropagation.

Usually, the above derivatives are represented with two error terms, δh and δo, as follows.
\(\delta^o=(y-y')(-1)\phi_v(1-\phi_v)\)
\(\delta_j^h=(y-y')(-1)\phi_v(1-\phi_v)V_j\phi_j(1-\phi_j)=\delta^oV_j\phi_j(1-\phi_j)\)
- \(\partial J/\partial w_{ij}=\sum _{\ }^{\ }\delta_j^hX_i\)
- \(\partial J/\partial b_j=\sum _{\ }^{\ }\delta_j^h(-1)\)
- \(\partial J/\partial V_j=\sum _{\ }^{\ }\delta^o\phi_j\)
- \(\partial J/\partial b_v=\sum _{\ }^{\ }\delta^o(-1)\)
At each step, calculate the residual and use it to find the derivative, then use the iterative calculation function in Excel to repeat this process until all coefficients converge.
To implement the backpropagation algorithm in Excel, the Iris data was used. The neural network structure used is a single hidden layer neural network with 5 hidden nodes as follows. The w and V are coefficients of matrix multiplication, and b and c are threshold values for the hidden layer and the output layer.

The input layer variables are the sepal length, sepal width, petal length, and petal width, and the output layer consists of the probabilities for Setosa, Versicolor, and Virginica.
The cost function, J, is the sum of the differences of the three outputs for each data.
\(J = \text{SUMXMY2}(y, y') \)
The sequence for performing the backpropagation algorithm in Excel is as follows.
(Step 0) Normalize input data X with the mean and standard deviation. Be cautious that without normalization, it may not converge.
(Step 1) Initialize coefficients W, V, b, and c
(Step 2) Using the current W, V, b, and c, calculate the error terms for each output term and find the partial derivative for each input data.
\(\delta_i^o=(y-y')(-1)\phi_i^o(1-\phi_i^o), \text{ where }i=1,2,3\)
Here, \(\phi_j^o=Sigmoid(SUMPRODUCT(h_i,V_{ij})-c_j)\)
\(\delta_i^h = \delta _{j}^{o} V_{ji} \phi_i (1- \phi_i)\)
Here, \(\phi_i=Sigmoid(SUMPRODUCT(X_j,w_{ji})-b_i)\)
\(\partial J/\partial w_{ij}=\sum _{\ }^{\ }\delta_j^hX_i\)
\(\partial J/\partial b_i=\sum _{\ }^{\ }\delta_i^h(-1)\)
\(\partial J/\partial V_{ij}=\sum _{\ }^{\ }\delta_i^o\phi_j\)
\(\partial J/\partial c_i=\sum _{\ }^{\ }\delta_i^o(-1)\)
The size of each coefficient and its partial derivative matrix is as follows: 20+5+15+3+5+3+20+15+5+3 = 94
- W matrix = [4x5]
- b vector = [1x5]
- V matrix = [5x3]
- c vector = [1x3]
- \(\delta^h\) vector = [1x5]
- \(\delta^o\) vector = [1x3]
- \(\partial J/\partial w_{ji}\) matrix = 4x5
- \(\partial J/\partial V_{ji}\) matrix = 5x3
- \(\partial J/\partial b_j\) vector = 1x5
- \(\partial J/\partial c_j\) vector = 1x3
(Step 3) Update each coefficient by summing the partial derivatives of each data obtained in (Step 2) using the learning rate η = 0.2.
\(w_{ij}^{new}=w_{ij}^{old}-\eta\sum _{\ }^{\ }(\partial J/\partial w_{ij})\)
\(b_j^{new}=b_j^{old}-\eta\sum _{\ }^{\ }(\partial J/\partial b_j)\)
\(V_{ij}^{new}=V_{ij}^{old}-\eta\sum _{\ }^{\ }(\partial J/\partial V_{ij})\)
\(c_j^{new}=c_j^{old}-\eta\sum _{\ }^{\ }(\partial J/\partial c_j)\)
Using the "Formula" → "Iterative Calculation" feature in Excel options automates iterative calculations.
The actual Excel work goes through the following process.
(Step 0) Standardization of input data and labeling of output values.
Using the STANDARDIZE function with the original input data below, standardize using the mean and standard deviation, and label the value of the output node.

(Step 1) Allocate Cells for the old and new coefficient matrices as below, and initialize the matrices of W, b, V, c with random numbers between 0 and 1 using the RAND() function, and set η = 0.2. Here, set Wijnew, Vijnew as follows.
\(W_{ij}^{new}=W_{ij}^{old}-\eta dW_{ij}\)
\(V_{ij}^{new}=V_{ij}^{old}-\eta dV_{ij}\)

(Step 2) For each sample, calculate Φi, Φi(1-Φi), Φio, Φio(1-Φio), -(yi-y'i), δio, ∂J/∂Vij, ∂J/∂ci, δih, ∂J/∂wij, and ∂J/∂bi
- \(\phi_i=1/(1+EXP(-SUMPRODUCT(W_{ij},X_j)+b_i)), \text{ where } i=1,2,3,4,5\)
- \(\delta_i^o=-(y_i-y'_i)\phi_i^o(1-\phi_i^o), \text{ where }i=1,2,3\)
- \(\partial J/\partial V_{ij}=\sum _{\ }^{\ }\delta_i^o\phi_j\)
- \(\delta_i^h=\delta_j^oV_{ji}\phi_i(1-\phi_i)=\delta_1^oV_{1i}\phi_i(1-\phi_i)+\delta_2^oV_{2i}\phi_i(1-\phi_i)+\delta_3^oV_{3i}\phi_i(1-\phi_i), \text{ where }i=1,2,3,4,5\)
- \(\partial J/\partial w_{ij}=\sum _{\ }^{\ }\delta_j^hX_i\)
- \(\partial J/\partial b_i=\sum _{\ }^{\ }\delta_i^h(-1)\)
(Step 3) Assemble dW, db, dV, and dc.
- \(dV_{ij}=\partial J/\partial V_{ij}\)
- \(dc_i=\partial J/\partial c_i\)
- \(dW_{ij}=\partial J/\partial w_{ij}\)
- \(db_i=\partial J/\partial b_i\)

(Step 4) To perform "Iterative Calculations", the Wijold and Vijold values are circularly referenced to Wijnew and Vijnew as follows. Ignore any errors that may arise.

(Step 5) Select "File" from the Excel main menu, then go to "Options" -> "Formulas" and choose to enable iterative calculation. Set the maximum number of iterations to a sufficiently large value and then execute.

(Step 6) Once the execution is completed, you can verify the improvement in the cost function (e.g. 101->5.5) and validate the model using the test dataset.