Você está na página 1de 4

Option Base 1

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

Private Type Layer 'Layer containing number of neurons


Neurons() As Neuron 'Neurons in the layer
NeuronCount As Long 'Number of neurons
End Type
Private Type NeuralNetwork
Layers() As Layer 'Layers in the network
LayerCount As Long 'Number of layers
LearningRate As Double 'The learning rate of the network
End Type
Dim Network As NeuralNetwork ' Our main network

-----------------------------------------------------------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

For i = 1 To UBound(ArrayOfLayers) ' Initialize all layers


DoEvents
Network.Layers(i).NeuronCount = ArrayOfLayers(i)
ReDim Network.Layers(i).Neurons(Network.Layers(i).NeuronCount) As Neuron
For j = 1 To ArrayOfLayers(i) 'Initialize all neurons
DoEvents
'We will not init dendrites for it because output layers doesn't have any
If i = UBound(ArrayOfLayers) Then
Network.Layers(i).Neurons(j).Bias = GetRand 'Set the bias to random value
Network.Layers(i).Neurons(j).DendriteCount = ArrayOfLayers(i - 1)
ReDim Network.Layers(i).Neurons(j).Dendrites(Network.Layers(i).Neurons(j).Dendri
teCount) As Dendrite 'Redim the dendrite var
For k = 1 To ArrayOfLayers(i - 1)
DoEvents
Network.Layers(i).Neurons(j).Dendrites(k).Weight = GetRand 'Set the weight of ea
ch dendrite
Next k
ElseIf i = 1 Then 'Only init dendrites not bias
DoEvents 'Do nothing coz it is input layer
Else
Network.Layers(i).Neurons(j).Bias = GetRand 'Set the bias to random value
Network.Layers(i).Neurons(j).DendriteCount = ArrayOfLayers(i - 1)
ReDim Network.Layers(i).Neurons(j).Dendrites(Network.Layers(i).Neurons(j).Dendri
teCount) As Dendrite 'Redim the dendrite var
For k = 1 To ArrayOfLayers(i - 1)
DoEvents
Network.Layers(i).Neurons(j).Dendrites(k).Weight = GetRand 'Set the weight of ea
ch dendrite
Next k
End If
Next j
Next i
CreateNet = 1
End Function
-----------------------------------------------------------------------Ejecucin de la red:
-----------------------------------------------------------------------Function Run(ArrayOfInputs As Variant) As Variant 'It returns the output in form
of array
Dim i, j, k As Integer
If UBound(ArrayOfInputs) <> Network.Layers(1).NeuronCount Then
Run = 0
Exit Function
End If
For i = 1 To Network.LayerCount
DoEvents
For j = 1 To Network.Layers(i).NeuronCount
DoEvents
If i = 1 Then

Network.Layers(i).Neurons(j).Value = ArrayOfInputs(j) 'Set the value of input la


yer
Else
Network.Layers(i).Neurons(j).Value = 0 'First set the value to zero
For k = 1 To Network.Layers(i - 1).NeuronCount
DoEvents
'Calculating the value
Network.Layers(i).Neurons(j).Value = Network.Layers(i).Neurons(j).Value + (contd
on next line) + Network.Layers(i - 1).Neurons(k).Value * Network.Layers(i).Neu
rons(j).Dendrites(k).Weight
Next k
Network.Layers(i).Neurons(j).Value = Activation(Network.Layers(i).Neurons(j).Val
ue + (contd on next line)
+ Network.Layers(i).Neurons(j).Bias) 'Calculatin
g the real value of neuron
End If
Next j
Next i
ReDim OutputResult(Network.Layers(Network.LayerCount).NeuronCount) As Double
For i = 1 To (Network.Layers(Network.LayerCount).NeuronCount)
DoEvents
OutputResult(i) = (Network.Layers(Network.LayerCount).Neurons(i).Value) 'The arr
ay of output result
Next i
Run = OutputResult
End Function
-----------------------------------------------------------------------------------------Funcin de activacin:
-----------------------------------------------------------------------------------------Private Function Activation(Value As Double)
'To crunch a number between 0 and 1
Activation = (1 / (1 + Exp(Value * -1)))
End Function
-----------------------------------------------------------------------------------------Entrenamiento de la red:
-----------------------------------------------------------------------------------------Function SupervisedTrain(inputdata As Variant, outputdata As Variant) As Integer
'0=unsuccessful and 1 = successful
Dim i, j, k As Integer
If UBound(inputdata) <> Network.Layers(1).NeuronCount Then 'Check if correct amo
unt of input is given
SupervisedTrain = 0
Exit Function
End If
If UBound(outputdata) <> Network.Layers(Network.LayerCount).NeuronCount Then 'Ch
eck if correct amount of output is given
SupervisedTrain = 0

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

Você também pode gostar