Simple Hopfield Example
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 Java applet. Finally, the third example uses a Java applet 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.java)
/** * Introduction to Neural Networks with Java, 2nd Edition * Copyright 2008 by Heaton Research, Inc. * http://www.heatonresearch.com/books/java-neural-2/ * * ISBN13: 978-1-60439-008-7 * ISBN: 1-60439-008-5 * * This class is released under the: * GNU Lesser General Public License (LGPL) * http://www.gnu.org/copyleft/lesser.html */ package com.heatonresearch.book.introneuralnet.ch3.console; import com.heatonresearch.book.introneuralnet.neural.hopfield.HopfieldNetwork; /** * Chapter 3: Using a Hopfield Neural Network * * ConsoleHopfield: Simple console application that shows how to * use a Hopfield Neural Network. * * @author Jeff Heaton * @version 2.1 */ public class ConsoleHopfield { /** * Convert a boolean array to the form [T,T,F,F] * * @param b * A boolen array. * @return The boolen array in string form. */ public static String formatBoolean(final boolean b[]) { final 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()); } /** * A simple main method to test the Hopfield neural network. * * @param args * Not used. */ public static void main(final String args[]) { // Create the neural network. final HopfieldNetwork network = new HopfieldNetwork(4); // This pattern will be trained final boolean[] pattern1 = { true, true, false, false }; // This pattern will be presented final boolean[] pattern2 = { true, false, false, false }; boolean[] result; // train the neural network with pattern1 System.out.println("Training Hopfield network with: " + formatBoolean(pattern1)); network.train(pattern1); // present pattern1 and see it recognized result = network.present(pattern1); System.out.println("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); System.out.println("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.
final 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.
final boolean[] 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.
final boolean[] pattern2 = { true, false, false, false };A Boolean array named result is created to hold the results of presenting patterns to the network.
boolean[] result;
The user is then informed that we are training the network with pattern1. The formatBoolean method is used to display pattern1.
System.out.println("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);
System.out.println("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);
System.out.println("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 applet that will allow you to see the weight matrix as the network is trained.
