Click here to Skip to main content
15,885,365 members
Articles / Programming Languages / UML

Designing And Implementing A Neural Network Library For Handwriting Detection, Image Analysis etc.- The BrainNet Library - Full Code, Simplified Theory, Full Illustration, And Examples

Rate me:
Please Sign up or sign in to vote.
4.76/5 (97 votes)
21 Oct 2009CPOL26 min read 373.2K   7.8K   356  
This article will explain the actual concepts and implementation of Backward Propagation Neural Networks very easily - see project code and samples, like a simple pattern detector, a hand writing detection pad, an xml based neural network processing language etc in the source zip.
'------------------------------------------------------------------
' License Notice:
'------------------------------------------------------------------
' All Rights Reserved - Anoop Madhusudanan, 
' Mail: amazedsaint@gmail.com
' Website: http://amazedsaint.blogspot.com

' See my articles about BrainNet at 
' http://amazedsaint-articles.blogspot.com for details
'
' You can use this code (or part of it), for non 
' commercial and academic uses, as long as 
'   - You are keeping this notice along with it
'   - You are not making any profit out of this
'------------------------------------------------------------------

'''<summary> Exception that may be thrown when the strategy is not initialized </summary>
Public Class StrategyNotInitializedException
    Inherits NeuralFrameworkException

    Sub New(ByVal Message As String, ByVal e As Exception)
        MyBase.New(Message, e)
    End Sub


End Class


'''<summary>A concrete implementation of INeuron </summary>
Public Class Neuron
    Implements INeuron


    Dim _bias As Single = Utility.Rand

    Dim _output As Single
    Dim _delta As Single
    Dim _forwardConnections As New NeuronCollection()


    Dim _inputs As NeuronConnections = New NeuronConnections()
    Dim _strategy As INeuronStrategy = Nothing



    '''<summary> Default constructor</summary>
    Public Sub New()
    End Sub

    '''<summary> A constructor to initialize a neuron with its strategy </summary>
    Public Sub New(ByVal strategy As INeuronStrategy)
        _strategy = strategy
    End Sub


    '''<summary> Gets the current bias this neuron</summary>
    Public Property BiasValue() As Single Implements NeuralFramework.INeuron.BiasValue
        Get
            Return _bias
        End Get
        Set(ByVal Value As Single)
            _bias = Value
        End Set
    End Property

    '''<summary> Gets the current output of this neuron</summary>
    Public Property OutputValue() As Single Implements NeuralFramework.INeuron.OutputValue
        Get
            Return _output
        End Get
        Set(ByVal Value As Single)
            _output = Value

        End Set
    End Property

    '''<summary> Gets the current delta value of this neuron</summary>
    Public Property DeltaValue() As Single Implements NeuralFramework.INeuron.DeltaValue
        Get
            Return _delta
        End Get
        Set(ByVal Value As Single)
            _delta = Value
        End Set
    End Property

    '''<summary> Gets a list of neurons connected to this neuron </summary>
    Public ReadOnly Property Inputs() As NeuralFramework.NeuronConnections Implements NeuralFramework.INeuron.Inputs
        Get
            Return _inputs
        End Get
    End Property

    '''<summary> Gets or Sets the strategy of this neuron </summary>
    Public Property Strategy() As NeuralFramework.INeuronStrategy Implements NeuralFramework.INeuron.Strategy
        Get
            Return _strategy
        End Get
        Set(ByVal Value As NeuralFramework.INeuronStrategy)
            _strategy = Value
        End Set
    End Property

    '''Methods

    '''<summary> Calculate the error value </summary>
    Public Sub UpdateDelta(ByVal errorFactor As Single) Implements NeuralFramework.INeuron.UpdateDelta

        If _strategy Is Nothing Then Throw New StrategyNotInitializedException("Strategy of the neuron not initialized. Assign a proper strategy", Nothing)

        '''Error factor can be target - output for output layer
        DeltaValue = Strategy.FindDelta(OutputValue, errorFactor)
    End Sub

    '''<summary> Calculate the output </summary>
    Public Sub UpdateOutput() Implements NeuralFramework.INeuron.UpdateOutput

        If _strategy Is Nothing Then Throw New StrategyNotInitializedException("Strategy of the neuron not initialized. Assign a proper strategy", Nothing)


        Dim netValue As Single = Strategy.FindNetValue(Inputs, BiasValue)
        OutputValue = Strategy.Activation(netValue)
    End Sub

    '''<summary> Calculate the free parameters </summary>
    Public Sub UpdateFreeParams() Implements NeuralFramework.INeuron.UpdateFreeParams

        If _strategy Is Nothing Then Throw New StrategyNotInitializedException("Strategy of the neuron not initialized. Assign a proper strategy", Nothing)


        BiasValue = Strategy.FindNewBias(BiasValue, DeltaValue)
        Strategy.UpdateWeights(Inputs, DeltaValue)

    End Sub


    '''<summary> Returns a list of all neurons to which this neuron is connected </summary>
    Public ReadOnly Property ForwardConnections() As NeuralFramework.NeuronCollection Implements NeuralFramework.INeuron.ForwardConnections
        Get
            Return _forwardConnections
        End Get
    End Property
End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
India India
Architect, Developer, Speaker | Wannabe GUT inventor & Data Scientist | Microsoft MVP in C#

Comments and Discussions