← All algorithms

Neural Network

Fundamentals of Neural Networks in Excel

2023-12-24

Artificial neural networks basically tackle complex nonlinear problems by stacking layers of linear systems. Many ML books or blogs introduce the neural networks in analogy with the way the neurons and synapses work to send the information, but mathematical perspective, it is no more than the combination of the many inner products followed by a sort of step functions.

To explain a little more detail, let's recall the methods used in regression and classification. The formula for regression derived through the method of least squares, y' = Xw, involves finding the coefficients or weights (w) that best represent the measured values (y). On the other hand, logistic regression involves finding the coefficients or weights (w) that best represent the measured values based on the log-likelihood of the probability P = Φ(y') = 1/(1+e-y'), where Φ uses the sigmoid activation function.

When this process is represented in a diagram, it looks like this.

​

\(y'=X_1w_1+X_2w_2+X_3w_3-b=Xw-b\)

\(P=\phi (y')=\dfrac{1}{1+e^{-y'}}=\dfrac{1}{1+e^{-(X_1w_1+X_2w_2+X_3w_3-b)}}=\phi (Xw-b)\)

​The independent variable X at this point refers to the nodes in the input layer, Σ represents vector addition, Φ is the activation function, and b is the threshold for adjusting the output of the activation function to 0 or 1 or the intercept for adjusting the average value of regression.

When summarized in function form, this can be simplified as

\(Y'=\phi(Xw-b)\)

and when expressed in vector components,

\(y'=\phi(\sum _j^{\ }X_jw_j-b)\)

Here, only one weight is assigned to one independent variable, and weights wj and threshold b are obtained by minimizing or maximizing the objective function using the training dataset.

When you add a hidden layer with two nodes between the input layer and the output function, the diagram looks as follows.

Their mathematical relationships between layers are formulated as follows:

if we break down the diagram from input layer to hidden layer, they are

\(\phi (Xw-b)=\phi_j(X_iw_{ij}-b_j)\)

and from the hidden layer to the output layer,

\(y'=\phi_v(V\phi-b_v)=\phi_v(V_j\phi_j-b_v)\)

In the end, from the input layer all the way to the output layer, the formulation becomes

\(y'=\phi_v\left{ V_j\phi_j(X_iw_{ij}-b_j)-b_v\right} \).

As a result of the addition of the hidden layer, the coefficients to be obtained increase to wij, bj, Vj, and bv.

​It took a very long time to establish the mathematical foundation necessary to calculate such a large number of coefficients, and after solving this problem, artificial neural networks have been experiencing explosive growth.

The methods which make it possible are (1) the cost function, (2) Stochastic Gradient Descent which uses derivatives to effectively optimize the training data, and (3) the backpropagation algorithm is used to update the coefficients.


Let's go through the derivation of the cost function.

For the case of the regression, we need to find the coefficients, w, to minimize the sum square error between the measurements and predictions.

\(\arg \min _w^{ }\sum _{\ }^{\ }(y-y')^2\)

For the case of the classification, the binary classification is required to find the coefficients, w, to maximize the cross entropy between the measurements and predictions.

\(\arg \max _w^{ }\sum _{\ }^{\ }y\log (y')+(1-y)\log (1-y')\)

Meanwhile, the multiclass classification uses the Softmax as the activation function defined as follows:

\(\text{softmax}=\dfrac{\exp (z_k)}{\sum _i^{\ }\exp (z_i)}\)

and the sum square error can be used as the cost function

\(\arg \min _w^{ }\sum _{\ }^{\ }(y-y')^2\)

​

Frequently used activation functions are listed below ​

​In Excel, the process to find the coefficients is easily taken care using the Excel's optional Solver feature.

Step 0. From the given size of the features and nature of the problem (i.e. Regression or Classification), the number of input layer nodes and the number of output layer nodes, as well as the activation function of the output layer, are determined. Typically, for regression problems, RELU is used as the activation function, and for classification problems, Sigmoid or Hyperbolic Tangent is used as the activation function.

Step 1. Determine the number of hidden layers and the number of nodes in each layer. If there are two or more hidden layers used, it is called a Deep neural network, otherwise it is called Vanila Network

Step 2. Allocate the corresponding Excel Cell to the number of input layer nodes, hidden layer nodes, and output layer nodes, and initialize the coefficients wij, bj, Vj, and bv with random numbers.

Step 3. Calculate y' according to the neural network structure and calculate the cost function based on the nature of the problem.

Step 4. Find the coefficients in the above cell through the optimization or maximization of the cost function using the Solver function. All coefficients must be positive at all times.

Example 1. The following is data representing nine test results and the severity of illness (4 and 2) with medical data.

​

​

To build a model for classification, an artificial neural network with 9 input nodes, 5 hidden layers, and 1 output node is considered. The activation function for the hidden layers is Sigmoid, and the output layer also uses Sigmoid as the activation function.

​

The total number of data is 685, with the first 500 being used as training data and the remaining 185 as validation data.

(1) Data Preprocessing. Standardize the source data with the mean and standard deviation, and encode the response data as 0 or 1.

= Standardize(X, average(X), stdev(X))

​

​

Let's encoding Class 4 = 1 and Class 2 = 0 and assign the value to the measurement Y.

​

(2) Construct the coefficient matrices for the hidden layer and the output layer.

The size of the coefficient matrix of the output layer is the number of nodes in the hidden layer x the number of nodes in the output layer + the number of output layer nodes = 5x1+1 = 6

The size of the coefficient matrix of the hidden layer is the number of nodes in the input layer x the number of nodes in the hidden layer + the number of hidden layer nodes = 9x5+5 = 50

The total 56 coefficients are obtained through the Solver.

​

(3) Calculate the sum of the hidden layer, (Xw-b), and the output of the hidden layer, Φj(Xiwij-bj).

The sum of the hidden layer, z = SUMPRODUCT(X,W) - bw

The output of the hidden layer = 1/(1+EXP(-z))

​

​

(4) Calculate the sum of the output layer, (VΦ-bv), and the output of the output layer, Φv(VΦ-bv).

​(5) Calculate the cost function to maximize the sum of the cross entropy losses for all the training data.

​

(6) Perform the main menu → Data → Solver, and obtain the coefficient matrix that maximizes the objective function as follows. All coefficient matrices are assumed to have positive values.

​

​The results evaluated on the validation data after the optimization were represented in the following confusion matrix.

Prediction
24
Measurements21252
4155
Confusion Matrix

​The accuracy is 180/183 = 98.3%.

Refer to the attached Excel file for an example of a multiclass classification problem using two hidden layers and the Softmax output layer with activation functions.

​In the following blogs, we will introduce a series of artificial neural networks to solve various problems.