You are here

Hopfield Pattern Recognition Applet

    Hopfield networks can be much larger than four neurons. For the third example, we will examine a 64-neuron Hopfield network. This network is connected to an 8x8 grid, which an applet allows you to draw upon. As you draw patterns, you can either train the network with them or present them for recognition.

    The user interface for the applet can be seen in Figure 3.3.

Figure 3.3: A pattern recognition Hopfield applet.

A pattern recognition Hopfield applet.

    The source code for this example is provided in Listing 3.4.

Listing 3.4: Hopfield Pattern Recognition (PatternApplet.java)

[[Insert Listing]]

    This applet can be run online from most browsers by accessing the following URL:

http://www.heatonresearch.com/articles/61/page1.html

    To make use of the applet, draw some sort of pattern on the grid and click “Train.” Now draw another pattern and also click “Train.” Finally, draw a pattern similar to one of the two previous patterns in the same location, and click “Go.” The network will attempt to recognize what you drew. You can also click “Clear” to clear the grid. Clicking “Clear Matrix” will clear the training matrix.

    The following sections explain how the main methods of this program were constructed.

Drawing the Grid

    The grid is drawn using the applet’s paint method. The signature for this method is shown here:

public void paint(final Graphics g)

    The grid size is defined by several constants. Unless you change these, the grid itself will be 8x8 and the cells will be 20 pixels square. This means that the buttons will take up more space than the grid; therefore, it looks better if the grid is centered. A margin is calculated to center the grid.

    The margin is the actual width minus the grid's width, divided by two.

this.margin = (this.getWidth() - (this.CELL_WIDTH * this.GRID_X)) / 2;

    Next, the index variable is created. This variable holds the current position in the grid boolean array. Even though the grid appears visually as an 8x8 matrix, it must be presented to the neural network as a flat 64-part Boolean pattern.

int index = 0;

    Two loops are then established to loop through the x and y coordinates of the grid.

for (int y = 0; y < this.GRID_Y; y++) {
  for (int x = 0; x < this.GRID_X; x++) {

    If the grid element for this cell is true, then draw a rectangle and fill it in.

    if (this.grid[index++]) {
      g.fillRect(this.margin + (x * this.CELL_WIDTH), y
      * this.CELL_HEIGHT, this.CELL_WIDTH,
      this.CELL_HEIGHT);

    If the grid element for this cell is false, then draw an empty rectangle.

    } else {
      g.drawRect(this.margin + (x * this.CELL_WIDTH), y
      * this.CELL_HEIGHT, this.CELL_WIDTH,
      this.CELL_HEIGHT);
    }
  }
}

    The paint method is called by Java whenever the grid needs to be redrawn. Additionally, other methods will force the paint method to be called using the repaint method.

Toggling Grid Positions

    The mouseReleased method is called by Java whenever the mouse has been released. Of course, the mouse must have been pressed first. The signature for the mouseReleased method is shown here:

public void mouseReleased(final MouseEvent e) 

    First, the x and y coordinates are calculated in terms of the grid position, and not the pixel coordinates that are passed to the mouseReleased event.

final int x = ((e.getX() - this.margin) / this.CELL_WIDTH);
final int y = e.getY() / this.CELL_HEIGHT;

    These coordinates must fall on the grid. If the click was outside of the grid, then the click will be ignored.

if (((x >= 0) && (x < this.GRID_X)) && ((y >= 0) && (y < this.GRID_Y))) {

    The index into the one-dimensional grid array is calculated, then it is toggled using the Boolean ! operator.

  final int index = (y * this.GRID_X) + x;
  this.grid[index] = !this.grid[index];
}

    Finally, the applet repaints the grid.

repaint();

    Thus, the grid can be drawn by tracking when the mouse is released.

Training and Presenting Patterns

    The “Train” and “Go” buttons allow you to train and recognize patterns respectively. The train method is called whenever the “Train” button is clicked. The train method’s signature is shown here:

public void train() 

    The train method of the neural network is called with the grid variable.

		try {
			this.hopfield.train(this.grid);

    It is highly unlikely that a HopfieldException will be thrown, since the applet is designed to send grids of the correct size. However, since the HopfieldException is a checked exception, it must be handled.

		} catch (final HopfieldException e) {
			e.printStackTrace();
		}
	}

    Minimal error handling is performed for the exception. The stack is dumped to the console.

    The go method is called whenever the “Go” button is clicked. The signature for the go method is shown here:

public void go()

    The grid is presented to the neural network for processing. The results from the present method are copied to the grid variable. Calling repaint displays the results from the network recognition.

		try {
			this.grid = this.hopfield.present(this.grid);
			repaint();

    Again, it is highly unlikely that a HopfieldException will be thrown, since the applet is designed to send grids of the correct size, but it must be handled.

		} catch (final HopfieldException e) {
			e.printStackTrace();
		}
	}

    And again, minimal error handling is performed for the exception. The stack is dumped to the console.

Technology: 
Calais Document Category: 

Comments

qistoph's picture

The link to the applet links to the applet of the previous page.
Couldn't find the correct one changing some numbers in the URL...

Also, the source is missing.

So there is, unfortunately, no way to try this applet.

Theme by Danetsoft and Danang Probo Sayekti inspired by Maksimer