|
The Neural Network Source code found on this page is in Visual Basic (version 6). I hope that the code is clear enough for anyone to convert it into their programming language of choice.
AddConnection()
Friend Sub AddConnection(ToNeuron As AditNeuron, ByVal WeightVal As Single) ConnectionCount = ConnectionCount + 1 ReDim Preserve ConnectionWeight(ConnectionCount) ReDim Preserve ConnectionID(ConnectionCount) ReDim Preserve ConnectionNeuron(ConnectionCount)
ConnectionWeight(ConnectionCount) = WeightVal Set ConnectionNeuron(ConnectionCount) = ToNeuron ConnectionID(ConnectionCount) = ToNeuron.NeuronID
End Sub
N.B. AditNeuron is the name of
our neuron class and so the ToNeuron variable just
caries a reference to the relevant instance of that
class.
If you are not into VB classes then don’t worry about the code above.
All it is doing is adding a reference to another neuron and a weighting to represent a connection between the two. You could manage this with an array of user defined types or with a pair of arrays.
NeuronEvent()
Friend Sub NeuronEvent() Dim ConLoop As Integer
On Error GoTo NeuronEventErr 'if this is an input Neuron the input should already be set 'otherwise it has to be accumulated from the connected Neurons If mType <> NT_INPUT Then mInput = mBias For ConLoop = 0 To ConnectionCount mInput = mInput + (ConnectionWeight(ConLoop) * ConnectionNeuron(ConLoop).State) Next 'push the threshold into the step function as a scaling factor mState = StepOutput(mInput, mThreshold) Else mState = mInput End If
Exit Sub NeuronEventErr: 'your structure will determine the right code here Exit Sub
End Sub
There is no rule that your input neurons simply pass on their inputs as outputs – it might depend upon the data. You could always add in a Bias value and/or push the output through the Sigmoid function.
Mutate()
Friend Sub Mutate() 'This process mutates the Bias and Threshold values together with the 'incoming Connection Weights (if appropriate) Dim ConLoop As Long
For ConLoop = 0 To ConnectionCount ConnectionWeight(ConLoop) = ConnectionWeight(ConLoop) + GetGausse(0.5) Next ConLoop mBias = mBias + GetGausse(0.5) mThreshold = mThreshold + GetGausse(2) 'threshold mutation is constrained here but please choose your own values ‘ – I think it should always be positive If mThreshold < 0.0001 Then mThreshold = 0.0001 ElseIf mThreshold > 2 Then mThreshold = 2 End If
End Sub
CreateNetwork()
Public Sub CreateNetwork(InputCount As Long, HiddenCount as Long, OutputCount As Long) 'used to create and initialise a network of artificial neurons Dim CreateLoop As Long Dim HiddenNeuron As AditNeuron, InputNeuron As AditNeuron, OutputNeuron As AditNeuron Dim Conweight As Single
'first let us add neurons to the network For CreateLoop = 1 To InputCount AddNeuron NT_INPUT Next CreateLoop For CreateLoop = 1 To HiddenCount AddNeuron NT_HIDDEN Next CreateLoop For CreateLoop = 1 To OutputCount AddNeuron NT_OUTPUT Next CreateLoop 'now we need to create the connections between the neurons 'any automated options could be implemented here ‘this is an example of a forward only setup For Each HiddenNeuron In HiddenNeurons ‘For each neuron in the HiddenNeurons collection For Each InputNeuron In InputNeurons Conweight = Rnd - 0.5 HiddenNeuron.AddConnection InputNeuron, Conweight Next Next For Each OutputNeuron In OutputNeurons For Each HiddenNeuron In HiddenNeurons Conweight = Rnd - 0.5 OutputNeuron.AddConnection HiddenNeuron, Conweight Next Next End Sub
The above method was made just a little more complex when we decided to introduce multiple hidden layers and apply some flexibility in the way neurons were connected. However these were just extensions of the processes shown here.
MutateNeurons() and TriggerEvent() have a very similar structure. they just loop through the relevant collection classes calling the methods in the Neuron classes – easy coding with collection classes. A sample code segment is shown below:
For Each WorkNeuron In OutputNeurons WorkNeuron.Mutate Next
Visual Basic collection classes can make life very straightforward. Just think there are people out there who look down on VB but we know it’s classy and the syntax is
cool*.
If you are implementing a neural network without classes then you will be involved in looping through your arrays and passing the relevant values to a subroutine or function.
InputValue()
Public Sub InputValue(InputNeuron As Long, InputValue As Single) Dim WorkNeuron As AditNeuron If InputNeuron >= 1 And InputNeuron <= InputNeurons.Count Then Set WorkNeuron = InputNeurons.Item(InputNeuron) WorkNeuron.InputValue = InputValue Set WorkNeuron = Nothing End If End Sub
This code sets the Input property of the relevant Input neuron class.
OutputValue()
Public Function OutPutValue(OutputNeuron As Long) As Single Dim WorkNeuron As AditNeuron If OutputNeuron >= 1 And OutputNeuron <= OutputNeurons.Count Then Set WorkNeuron = OutputNeurons.Item(OutputNeuron) OutPutValue = WorkNeuron.State End If
End Function
The function above just reads the State property of the relevant neuron class. It could be changed to return all of the output values in one go as a variant array stored in a variant. I suppose this will become a bit of a historical footnote but just as variants were beginning to look like the data structure they got dumped and did not make VB.NET. There again – what’s that all about anyway?
Note: Values that start with a lower case “m” are private copies of public properties.
* If only
Microsoft would get on and fix the VB6
bugs and omissions then the tool would properly
reflect it's popularity with developers.
|