Click here to Skip to main content
15,888,316 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 374.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
'------------------------------------------------------------------

Imports BrainNet.NeuralFramework
Imports BrainNet.NeuralFramework.NeuralXML



'<summary>Neural XML interpreter </summary>
Module ModuleMain

    '<summary>Execution starts from here</summary>
    Sub Main()
        Dim vars() As String = Split(Trim(Command))

        If vars.Length < 2 Then
            WriteUsage()
            End
        End If

        Try
            Dim count As Long
            Dim filename As String = ""


            If LCase(vars(0)) = "-gen" Then
                'Network generation
                Console.WriteLine("Started generating network at " & Now())
                Dim layers As String = vars(1)
                For count = 2 To vars.Length - 1
                    filename = filename & vars(count)
                    If count < vars.Length - 1 Then filename = filename & " "
                Next
                GenerateNetwork(vars(1), filename)
                Console.WriteLine("Network generated to " & filename & " at " & Now)

            ElseIf LCase(vars(0)) = "-start" Then

                    'Interpret an nxml file to train and run a network
                    For count = 1 To vars.Length - 1
                        filename = filename & vars(count)
                        If count < vars.Length - 1 Then filename = filename & " "
                    Next
                    Console.WriteLine("Started interpreting nxml file at " & Now())
                    Interpret(filename)
                    Console.WriteLine("Finished interpreting nxml file at " & Now())

                Else
                    Throw New Exception("Unknown command switch")
                End If
        Catch ex As Exception
            Console.WriteLine("Error: " & ex.Message)
            Console.WriteLine()
            Console.WriteLine("Detailed Error Information:" & vbCrLf & ex.ToString)


        End Try



    End Sub


    '<summary>Generate a network</summary>
    Sub GenerateNetwork(ByVal layers As String, ByVal file As String)

        Dim i As Long
        Dim neuronlist As New ArrayList()
        Dim mynn As INeuralNetwork
        Try

            Dim neuronlistStr() As String = Split(layers, ",")


            For i = 0 To neuronlistStr.Length - 1
                neuronlist.Add(CType(neuronlistStr(i), Long))
            Next
        Catch ex As Exception
            Throw New Exception("Invalid layer information. You should specify neurons in layers, separted by comma ','")

        End Try

        mynn = New BackPropNetworkFactory().CreateNetwork(neuronlist)

        'Serialize it to a file
        Dim ser As New NetworkSerializer()
        ser.SaveNetwork(file, mynn)

    End Sub

    '<summary>Start the interpretation</summary>
    Sub Interpret(ByVal file As String)
        Dim interpreter As New NeuralXML.NXMLInterpreter()
        interpreter.Interpret(file)

    End Sub

    '<summary>Write the usage</summary>
    Sub WriteUsage()
        Console.WriteLine("You can use this program to create a multi layer back prop network, train it and run it")
        Console.WriteLine("All Rights Reserved (C) 2005-2006, Anoop Madhusudanan, http://amazedsaint.blogspot.com")
        Console.WriteLine()

        Console.WriteLine("Syntax:")
        Console.WriteLine("-----------")
        Console.WriteLine()
        Console.WriteLine("Syntax1: nxml -gen NeuronsInLayer1,NeuronsInLayer2,NeuronsInLayerN filename")
        Console.WriteLine()
        Console.WriteLine("Syntax2: nxml -start filename")
        Console.WriteLine()
        Console.WriteLine("Use the -start switch to train an run a network using an nxml file.")
        Console.WriteLine("Use the -gen switch to create a back propagation network and save it to a file")
        Console.WriteLine()
        Console.WriteLine("Examples:")
        Console.WriteLine("-----------")
        Console.WriteLine()
        Console.WriteLine("Example1: nxml -gen 10,10,4 mynetwork.xml")
        Console.WriteLine("The above example will create a network with 3 layers - 10 neurons in input layer, 10 neurons in hidden layer, and 4 neurons in output layer.")
        Console.WriteLine()
        Console.WriteLine("Example2: nxml -gen 3,3,3,2 mynetwork.xml")
        Console.WriteLine("The above example will create a network with 4 layers - 3 neurons in input layer, 3 neurons in first hidden layer, 3 neurons in second hidden layer, and 2 neurons in output layer.")
        Console.WriteLine()
        Console.WriteLine("Example3: nxml -start mynetworktrian.nxml")
        Console.WriteLine("The above example will execute the mynetworktrain.nxml file to train and run the network")
        Console.WriteLine()

    End Sub

End Module

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