'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).DendriteCount) 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 each 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).DendriteCount) 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 each dendrite
Next k
End If
Next j
Next i
CreateNet = 1
End Function