Loop in a neural network

Kartik27

New Member
Joined
Jun 9, 2016
Messages
25
I'm trying to make an Artificial Neural Network in excel. I've built the basic framework but I'm stuck on the loop part.
So what I need is to calculate the Error between output and desired value such that it remains in loop until error reaches 0.01. There are 1012 data points (From 2 to 1013) from which the inputs will be taken.

Private Sub CommandButton1_Click() 'Clicking the command _
button initiates the following code


Dim ANNInput1 As Double 'Define the variable that will store _
the input to the artificial neural network (ANN)
Dim ANNInput2 As Double


Dim Target As Double 'Define the variable the will store the _
desired output value of the ANN, thus the y value in the _
spreadsheet


Dim W13 As Double 'Define the variable that will store the _
value of the weight that is connected to neuron1 and neuron2 _
Neuron1 is the input neuron thats output is equal to ANNInput
Dim W14 As Double 'Define the variable that will store the _
value of the weight that is connected to neuron1 and neuron2
Dim W15 As Double '...


Dim W23 As Double
Dim W24 As Double
Dim W25 As Double


'The variable that will store the input of neurons 3 - 5 _
are defined here
Dim HIDIN3 As Double
Dim HIDIN4 As Double
Dim HIDIN5 As Double


'The following weights are given random values that was _
calculated using the Excel RAND() function
W13 = 0.306
W14 = 0.554
W15 = 0.253
W23 = 0.225
W24 = 0.445
W25 = 0.336




'The variables that will store the output of neurons 3 - 5 are defined here
Dim HIDOUT3 As Double
Dim HIDOUT4 As Double
Dim HIDOUT5 As Double


Dim W36 As Double 'Define the variable that will store the value of the weight that is connected to neurons 3 and 6
Dim W46 As Double '...
Dim W56 As Double '...


'The following weights are given random values that was calculated using the Excel RAND() function
W36 = 0.453
W46 = 0.37
W56 = 0.372


Dim OUTIN As Double 'The variable that will store the input to neuron 6, that will be called the output neuron, is defined here
Dim OUTOUT As Double 'The variable that will store the output of the output neuron is defined here._
'This is also the final output of the artificial neural network


Dim delW13 As Double 'The variable that stores the value with _
which weight W13 has to be updated during backpropagation is _
defined here
Dim delW14 As Double '...
Dim delW15 As Double '...
Dim delW23 As Double
Dim delW24 As Double
Dim delW25 As Double




Dim delW36 As Double 'The variable that stores the value with _
which weight W36 has to be updated during backpropagation is _
defined here
Dim delW46 As Double '...
Dim delW56 As Double '...


'Define the variable that will store the error of the neural _
network output is defined here.
Dim delOP As Double


'The variable p will serve as a counter in the do until loop
Dim p As Integer
p = 0 'The variable p is set to a value of 0


Do Until p = 0.01 'This loop will continue until p will be _
equal to 0.01 of error




For i = 2 To 1013 'This nested for loop will go through _
rows 2 to 1013
ANNInput1 = Sheets("Sheet5").Cells(i, 1) 'The input to _
the ANN is obtained from the spreadsheet
ANNInput2 = Sheets("Sheet5").Cells(i, 2) 'The input to _
the ANN is obtained from the spreadsheet
Target = Sheets("Sheet5").Cells(i, 2) 'The target value _
for the specific input value is obtained from the spread- _
sheet

''''''''''''''''Feed forward'''''''''''''''''''

HIDIN3 = (ANNInput1 * W13) + (ANNInput2 * W23) 'The input to neuron2 is calculated _
by multiplying the input of the ANN with the value of _
W13 added to input2 of ANN times W23
HIDIN4 = (ANNInput1 * W14) + (ANNInput2 * W24) '...
HIDIN5 = (ANNInput1 * W15) + (ANNInput2 * W25) '...

'The outputs of nodes 2 - 4 are calculated with the sigmoid _
transfer function
HIDOUT3 = 1 / (1 + Exp(-HIDIN3))
HIDOUT4 = 1 / (1 + Exp(-HIDIN4))
HIDOUT5 = 1 / (1 + Exp(-HIDIN5))

'The input to the output neuron, neuron6, is calculated by _
multiplying the the output weights, W36, W46 and W56 with _
the output of the neurons to which they are connected and _
then adding these values together
OUTIN = (HIDOUT3 * W36) + (HIDOUT4 * W46) + (HIDOUT5 * W56)

'The output of the output neuron is calculated by making _
use of the sigmoid transfer function
OUTOUT = 1 / (1 + Exp(-OUTIN))

''''''''''''''Backpropagation'''''''''''''''

'The error of the neural network output is calculated. As _
you can see, it is not as easy as just subtracting the target _
value from the output value
delOP = (Target - OUTOUT) * OUTOUT * (1 - OUTOUT)

delW36 = HIDOUT2 * delOP 'The value with which W25 _
has to be updated to get a smaller error is calculated
W36 = W36 + delW36 'W25 is updated with delW25
delW46 = HIDOUT3 * delOP '...
W46 = W46 + delW46 '...
delW56 = HIDOUT4 * delOP '...
W56 = W56 + delW56 '...

'The value with which W13 has to be updated to minimise the _
difference between the target value and the ANN output is cal- _
culated here
delW13 = delOP * W36 * ANNInput1 * (1 - HIDOUT2) * HIDOUT2
W13 = W13 + delW13 'W13 is updated with delW13
delW14 = delOP * W46 * ANNInput1 * (1 - HIDOUT3) * HIDOUT3
W14 = W14 + delW14 '...
delW15 = delOP * W56 * ANNInput1 * (1 - HIDOUT4) * HIDOUT4
W15 = W15 + delW15 '...

delW23 = delOP * W36 * ANNInput2 * (1 - HIDOUT2) * HIDOUT2
W23 = W23 + delW23 'W23 is updated with delW23
delW24 = delOP * W46 * ANNInput2 * (1 - HIDOUT3) * HIDOUT3
W24 = W24 + delW24 '...
delW25 = delOP * W56 * ANNInput2 * (1 - HIDOUT4) * HIDOUT4
W25 = W25 + delW25 '...

Sheets("Sheet5").Cells(i, 3) = OUTOUT 'The output of the ANN is _
transfered to spreadsheet for the user to see

Next i 'The next training data point is inserted into the ANN

Right now I'm using this code. Please help me to solve this Problem. Thank you. :biggrin:
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.

Forum statistics

Threads
1,216,028
Messages
6,128,399
Members
449,447
Latest member
M V Arun

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top