jeffheaton's picture

    Now you will see how to make use of the HopfieldNetwork class that was created in the last section. The first example implements a simple console application that demonstrates basic pattern recognition. The second example graphically displays the weight matrix using a C# application. Finally, the third example uses a C# application to illustrate how a Hopfield neural network can be used to recognize a grid pattern.

    The first example, which is a simple console application, is shown in Listing 3.2.

Listing 3.2: Simple Console Example (ConsoleHopfield.cs)

// Introduction to Neural Networks for C#, 2nd Edition
// Copyright 2008 by Heaton Research, Inc. 
// http://www.heatonresearch.com/online/introduction-neural-networks-cs-edi...
//         
// ISBN13: 978-1-60439-009-4  	 
// ISBN:   1-60439-009-3
//   
// This class is released under the:
// GNU Lesser General Public License (LGPL)
// http://www.gnu.org/copyleft/lesser.html

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using HeatonResearchNeural.Hopfield;

namespace Chapter03Console
{

    /// <summary>
    /// Chapter 3: Using a Hopfield Neural Network
    ///
    /// ConsoleHopfield: Simple console application that shows how to
    /// use a Hopfield Neural Network.
    /// </summary>
    public class ConsoleHopfield
    {
        /// <summary>
        /// Convert a boolean array to the form [T,T,F,F]
        /// </summary>
        /// <param name="b">A boolen array.</param>
        /// <returns>The boolen array in string form.</returns>
        public static String FormatBoolean(bool[] b)
        {
            StringBuilder result = new StringBuilder();
            result.Append('[');
            for (int i = 0; i < b.Length; i++)
            {
                if (b[i])
                {
                    result.Append("T");
                }
                else
                {
                    result.Append("F");
                }
                if (i != b.Length - 1)
                {
                    result.Append(",");
                }
            }
            result.Append(']');
            return (result.ToString());
        }

        /// <summary>
        /// A simple main method to test the Hopfield neural network.
        /// </summary>
        /// <param name="args">Not used</param>
        static void Main(string[] args)
        {

            // Create the neural network.
            HopfieldNetwork network = new HopfieldNetwork(4);
            // This pattern will be trained
            bool[] pattern1 = { true, true, false, false };
            // This pattern will be presented
            bool[] pattern2 = { true, false, false, false };
            bool[] result;

            // train the neural network with pattern1        
            Console.WriteLine("Training Hopfield network with: "
                    + FormatBoolean(pattern1));
            network.Train(pattern1);
            // present pattern1 and see it recognized
            result = network.Present(pattern1);
            Console.WriteLine("Presenting pattern:" + FormatBoolean(pattern1)
                    + ", and got " + FormatBoolean(result));
            // Present pattern2, which is similar to pattern 1. Pattern 1
            // should be recalled.
            result = network.Present(pattern2);
            Console.WriteLine("Presenting pattern:" + FormatBoolean(pattern2)
                    + ", and got " + FormatBoolean(result));

        }
    }
}

    There are two methods provided in Listing 3.2. The first method, named FormatBoolean, is used to format Boolean arrays as follows:

[T,T,F,F]

    This method allows the program to easily display both input and output for the neural network. The FormatBoolean function is relatively simple. It loops through each element in the array and displays either a T or an F depending upon whether the array element is true or false. This can be seen in Listing 3.2.

    The second method, Main, is used to set up the Hopfield network and use it. First, a new HopfieldNetwork is created with four neurons.

HopfieldNetwork network = new HopfieldNetwork(4);            

    Next, an input pattern named pattern1 is created. This is the pattern that the Hopfield network will be trained on. Since there are four neurons in the network, there must also be four values in the training pattern.

bool[] pattern1 = { true, true, false, false };

    A second input pattern, named pattern2, is then created. This pattern is slightly different than pattern1. This pattern will allow the network to be tested to see if it still recognizes pattern1, even though this pattern is slightly different.

bool[] pattern2 = { true, false, false, false };            

    A Boolean array named result is created to hold the results of presenting patterns to the network.

bool[] result;

    The user is then informed that we are training the network with pattern1. The FormatBoolean method is used to display pattern1.

Console.WriteLine("Training Hopfield network with: "                    + FormatBoolean(pattern1)); 

    The network is called and trained with pattern1.

network.Train(pattern1);

    Now, pattern1 is presented to the network to see if it will be recognized. We tell the user that we are doing this and display the result.

result = network.Present(pattern1);            
Console.WriteLine("Presenting pattern:" + FormatBoolean(pattern1)                    + ", and got " + FormatBoolean(result));            

    Next, pattern2, which is similar to pattern1, is presented. The same values should be recalled for pattern2 as were recalled for pattern1.

result = network.Present(pattern2);            
Console.WriteLine("Presenting pattern:" + FormatBoolean(pattern2)+ ", and got " + FormatBoolean(result));

    The results are displayed to the user. The output from this program is as follows:

Training Hopfield network with: [T,T,F,F]
Presenting pattern:[T,T,F,F] and got [T,T,F,F]
Presenting pattern:[T,F,F,F] and got [T,T,F,F]

    This program shows how to instantiate and use a Hopfield neural network without any bells or whistles. The next program is an application that will allow you to see the weight matrix as the network is trained.


Copyright 2005 - 2012 by Heaton Research, Inc.. Heaton Research™ and Encog™ are trademarks of Heaton Research. Click here for copyright, license and trademark information.