Escolar Documentos
Profissional Documentos
Cultura Documentos
Option Explicit
Const e = 2.7183 'Mathematical const, used in sigmod function
'Dendrite connects one neuron to another and allows signal to pass from it
Private Type Dendrite
Weight As Double 'Weight it has
End Type
Private Type Neuron 'The main thing
Dendrites() As Dendrite 'Array of Dendrites
DendriteCount As Long 'Number of dendrites
Bias As Double 'The bias
Value As Double 'The value to be passed to next layer of neurons
Delta As Double 'The delta of neuron (used while learning)
End Type
-----------------------------------------------------------Creacin de la red:
-----------------------------------------------------------'0 = Unsuccessful and 1 = Successful
Function CreateNet(LearningRate As Double, ArrayOfLayers As Variant) As Integer
Dim i, j, k As Integer
Network.LayerCount = UBound(ArrayOfLayers) 'Init number of layers
If Network.LayerCount < 2 Then 'Input and output layers must be there
CreateNet = 0 'Unsuccessful
Exit Function
End If
Network.LearningRate = LearningRate 'The learning rate
ReDim Network.Layers(Network.LayerCount) As Layer 'Redim the layers variable
Exit Function
End If
Call Run(inputdata) 'Calculate values of all neurons and set the input
'Calculate delta's
For i = 1 To Network.Layers(Network.LayerCount).NeuronCount
DoEvents
'Deltas of Output layer
Network.Layers(Network.LayerCount).Neurons(i).Delta = Network.Layers(Network.Lay
erCount).Neurons(i).Value * (contd)
* (1 - Network.Layers(Network.LayerCount
).Neurons(i).Value) * (contd) * (outputdata(i) - Network.Layers(Network.LayerC
ount).Neurons(i).Value)
For j = Network.LayerCount - 1 To 2 Step -1
DoEvents
For k = 1 To Network.Layers(j).NeuronCount
DoEvents
Network.Layers(j).Neurons(k).Delta = Network.Layers(j).Neurons(k).Value * (contd
)
* (1 - Network.Layers(j).Neurons(k).Value) * Network.Layers(j + 1).Neurons(i).De
ndrites(k).Weight * (contd)
* Network.Layers(j + 1).Neurons(i).Delta 'Deltas of Hidden Layers
Next k
Next j
Next i
For i = Network.LayerCount To 2 Step -1
DoEvents
For j = 1 To Network.Layers(i).NeuronCount
DoEvents
Network.Layers(i).Neurons(j).Bias = Network.Layers(i).Neurons(j).Bias + (contd)
+ (Network.LearningRate * 1 * Network.Layers(i).Neurons(j).Delta) 'Calculate new
bias
For k = 1 To Network.Layers(i).Neurons(j).DendriteCount
DoEvents
'Calculate new weights
Network.Layers(i).Neurons(j).Dendrites(k).Weight = Network.Layers(i).Neurons(j).
Dendrites(k).Weight + (contd) (Network.LearningRate * Network.Layers(i - 1).Ne
urons(k).Value * Network.Layers(i).Neurons(j).Delta)
Next k
Next j
Next i
SupervisedTrain = 1
End Function