Visualizing the Weight Matrix
This second example is essentially the same as the first; however, this example uses an application. Therefore, it has a GUI that allows the user to interact with it. The user interface for this program can be seen in Figure 3.2.
Figure 3.2: A Hopfield Application

The 4x4 grid is the weight matrix. The four “0/1” fields allow you to input four-part patterns into the neural network. This pattern can then be presented to the network to be recognized or to be used for training.
The Hopfield application is shown in Listing 3.3.
Listing 3.3: Visual Hopfield Weight Matrix Application (HopfieldApp.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.Windows.Forms; namespace Chapter03App { static class HopfieldApp { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new HopfieldForm()); } } }
To use the application, follow these steps:
1. Notice the activation weight matrix is empty (contains all zeros). This neural network has no knowledge. Let’s teach it to recognize the pattern 1001. Enter 1001 under the “Input pattern to run or train:” prompt. Click the “Train” button. Notice the weight matrix adjusts to absorb the new knowledge. 2. Now test it. Enter the pattern 1001 under the “Input pattern to run or train:” prompt. (It should still be there from your training.) Now click “Run.” The output will be “1001.” This is an autoassociative network, therefore it echos the input if it recognizes it. 3. Let’s test it some more. Enter the pattern 1000 and click “Run.” The output will now be “1001.” The neural network did not recognize “1000;” the closest thing it knew was “1001.” It figured you made an error typing and attempted a correction! 4. Now, notice a side effect. Enter “0110,” which is the binary inverse of what the network was trained with (“1001”). Hopfield networks ALWAYS get trained for a pattern’s binary inverse. So, if you enter “0110,” the network will recognize it. 5. Likewise, if you enter “0100,” the neural network will output “0110” thinking that is what you meant. 6. One final test—let’s try “1111,” which is totally off base and not at all close to anything the neural network knows. The neural network responds with “0000.” It did not try to correct you. It has no idea what you mean! 7. Play with it some more. It can be taught multiple patterns. As you train new patterns, it builds upon the matrix already in memory. Pressing “Clear,” clears out the memory.
The Hopfield network works exactly the same way it did in the first example. Patterns are presented to a four-neuron Hopfield neural network for training and recognition. All of the extra code shown in Listing 3.3 simply connects the Hopfield network to a GUI using the C#'s forms technology.




