• No se han encontrado resultados

Revisión Biblio y Web-gráfica

BLOQUE IV: MARCO METODOLÓGICO

5.3.4. Revisión Biblio y Web-gráfica

We will now examine a simple program that trains a Kohonen neural network. As the network is trained you are shown a graphical display of the weights. The output from this program is shown in Figure 6.4.

Figure 6.4: Training a Kohonen neural network

You will this program contains two input neurons and seven output neurons. Each of the seven output neurons are plotted as white squares. The x-dimension shows their weight to the first input neuron and the y-dimension shows their weight to the second input neuron. You will see the boxes move as training progresses.

You will also see lines from select points on a grid drawn to each of the squares. These identify which output neuron is winning for the x and y coordinates of that point. You will see points with similar x and y coordinates being recognized by the same output neuron. We will now examine the example program. The program begins by defining certain properties that will be used by the program. This is shown in Listing 6.18.

Listing 6.18: Properties of the Kohonen example (TestKohonen.java)

import java.awt.*; import javax.swing.*; import java.text.*;

public class TestKohonen extends JFrame

implements NeuralReportable,Runnable { public static final int INPUT_COUNT=2; public static final int OUTPUT_COUNT=7; public static final int SAMPLE_COUNT=100; protected int unitLength;

protected int retry=1;

protected double totalError=0; protected double bestError = 0; protected KohonenNetwork net; protected TrainingSet ts; protected Image offScreen;

You can modify some of the final constants. You can choose s lager training set by setting the SAMPLE_COUNT variable to other values. You can change the OUTPUT_COUNT to values other than 7 as well. You may not change the INPUT_COUNT due to the fact that the x and y coordinates are tied to the inputs. Since you only have two dimensions you may not increase or decrease this value.

A constructor is provided to create the window and center it on the screen. This constructor is shown in Listing 6.19.

Listing 6.19: Construct the example (TestKohonen.java)

/**

* The constructor sets up the position and size of * the window.

*/

TestKohonen() {

setTitle("Training a Kohonen Neural Network"); setSize(400,450);

Toolkit toolkit = Toolkit.getDefaultToolkit(); Dimension d = toolkit.getScreenSize(); setLocation( (int)(d.width-this.getSize().getWidth())/2, (int)(d.height-this.getSize().getHeight())/2 ); setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); setResizable(false); }

The update method is provided because we implement the NeuralReportable interface. The update method will be called for each epoch during training. Here we record the status values and requires that the window be redrawn. This is shown in Listing 6.20.

Listing 6.20: Receive status information during training (TestKohonen.java)

/**

* Update is called by the neural network as the * network is trained.

*

* @param retry What retry number this is. * @param totalError The error for this retry. * @param bestError The best error so far. */

public void update(int retry,double totalError,double bestError) { this.retry = retry; this.totalError = totalError; this.bestError = bestError; this.paint(null); }

This example is a multithreaded program. A background thread is used to train the neural network. Multithreading was discussed in much greater detail in Chapter 5. The

background thread is shown in Listing 6.21.

Listing 6.21: The background thread (TestKohonen.java)

/**

* Called to run the background thread. The background thread * sets up the neural network and training data and begins * training the network.

*/

public void run() {

// build the training set

ts = new TrainingSet(INPUT_COUNT,OUTPUT_COUNT); ts.setTrainingSetCount(SAMPLE_COUNT);

for ( int i=0;i<SAMPLE_COUNT;i++ ) { for ( int j=0;j<INPUT_COUNT;j++ ) { ts.setInput(i,j,Math.random()); }

}

// build and train the neural network

net = new KohonenNetwork(INPUT_COUNT,OUTPUT_COUNT,this); net.setTrainingSet(ts);

net.learn(); }

The first thing that the run method does is create random training sets. This will give the neural network a random sampling of training items that it must attempt to classify. Next the learn method of the neural network is called to begin the training process.

As the program runs a graph must be displayed of the neuron weights. Listing 6.22 shows the method that is responsible for drawing this graph.

Listing 6.22: Graph the neuron weights (TestKohonen.java)

/**

* Display the progress of the neural network. *

* @param g A graphics object. */

public void paint(Graphics g) { if ( net==null ) return; if ( offScreen==null ) { offScreen = this.createImage( (int)getBounds().getWidth(), (int)getBounds().getHeight()); } g = offScreen.getGraphics();

int width = (int)getContentPane().bounds().getWidth(); int height = (int)getContentPane().bounds().getHeight(); unitLength = Math.min(width,height);

g.setColor(Color.black); g.fillRect(0,0,width,height);

// plot the weights of the output neurons g.setColor(Color.white);

for ( int y=0;y<net.outputWeights.length;y++ ) {

g.fillRect((int)(net.outputWeights[y][0]*unitLength),

(int)(net.outputWeights[y][1]*unitLength),10,10); }

// plot a grid of samples to test the net with g.setColor(Color.green);

for ( int y=0;y<unitLength;y+=50 ) { for ( int x=0;x<unitLength;x+=50 ) { g.fillOval(x,y,5,5);

double d[] = new double[2]; d[0]=x;

d[1]=y;

double normfac[] = new double[1]; double synth[] = new double[1]; int c = net.winner(d,normfac,synth); int x2=(int)(net.outputWeights[c][0]*unitLength); int y2=(int)(net.outputWeights[c][1]*unitLength); g.drawLine(x,y,x2,y2); } }

// display the status info g.setColor(Color.white); NumberFormat nf = NumberFormat.getInstance(); nf.setMaximumFractionDigits(2); nf.setMinimumFractionDigits(2); g.drawString( "retry = "

+ retry + ",current error = " + nf.format(totalError*100) + "%, best error = " + nf.format(bestError*100) +"%", 0, (int)getContentPane().getBounds().getHeight()); getContentPane().getGraphics().drawImage(offScreen,0,0,this); }

First an off-screen image is created using the createImage method provided by Java. All of our drawing will be to this off-screen image. Once the graph is complete, the off-screen image will be drawn to the window. This prevents flicker as the program constantly redraws itself.

First the output neurons are drawn as white rectangles. The x and y coordinates for these neurons are obtained from the weight between the output neuron and one of the two input neurons.

Next a grid of samples is drawn onto the screen. This is just a typical grid with each dot being 50 pixels from the other dots. Lines are then drawn from each of these dots to the winning neuron when the network is presented with the x and y coordinates from this grid dot.

To start this program a simple main method is provided as seen in Listing 6.22. Listing 6.22: Start the program (TestKohonen.java)

/**

* Startup the program. *

* @param args Not used. */

public static void main(String args[]) {

TestKohonen app = new TestKohonen(); app.show();

Thread t = new Thread(app);

t.setPriority(Thread.MIN_PRIORITY); t.start();

} }

Chapter 6: Understanding the Kohonen Neural Network

Article Title: Chapter 6: Understanding the Kohonen Neural Network Category: Artificial Intelligence Most Popular

From Series: Programming Neural Networks in Java

Posted: Wednesday, November 16, 2005 05:15 PM Author: JeffHeaton

Page: 5/5

Summary

In this chapter we learned about the Kohonen neural network. The Kohonen neural network differs from the feed forward back propagation network in several ways. The Kohonen neural network is trained in an unsupervised way. This means that the Kohonen neural network is given input data but no anticipated output. The Kohonen neural network then begins to map the training samples to each of its output neurons during training. A Kohonen neural network contains only two levels. The network is presented with an input pattern that is given to the input layer. This input pattern must be normalized to numbers in the range between -1 and 1. The output from this neural network will be one single winning output neuron. The output neurons can be thought of as groups that the Kohonen neural network has classified the input as part of.

Training a Kohonen neural network is considerably different than the back propagation algorithm that we examined in Chapter 5. To train the Kohonen neural network we present it with the training elements and see which output neuron "wins". This winning neuron's weights are them modified so that it will activate higher on the pattern that caused it to win.

There is also a case where there may be one or more neurons that fail to ever win. Such neurons are dead-weight to the neural network. We must identify such neurons and cause them to recognize patterns that are already recognized by other more "overworked" neurons. This causes the burden of recognition to fall more evenly over the output neurons.

This chapter presented only a simple example of the Kohonen neural network. In the next chapter we will apply the Kohonen neural network to a more real-world application. We will see how to use the Kohonen neural network to recognize handwriting.

Chapter 7: OCR with the Kohonen Neural Network

Article Title: Chapter 7: OCR with the Kohonen Neural Network Category: Artificial Intelligence Most Popular

From Series: Programming Neural Networks in Java

Posted: Wednesday, November 16, 2005 05:15 PM Author: JeffHeaton

Page: 1/5

Introduction

In the previous chapter you learned how to construct a Kohonen neural network. You learned that a Kohonen neural network can be used to classify samples into several groups. In this chapter we will closely examine a specific application of the Kohonen neural network. The Kohonen neural network will be applied to Optical Character Recognition (OCR).

OCR programs are capable of reading printed text. This could be text that was scanned in from a document, or hand written text that was drawn to a hand-held device, such as a Personal Digital Assistant (PDA). OCR programs are used widely in many industries. One of the largest uses of OCR systems is the United States Postal Service. In the 1980's the US Postal Service had many Letter Sorting Machines (LSMs). These machines were manned by human clerks that would key the zip codes of sixty letters per minute.

These human letter sorters have now been completely replaced by computerized letter sorting machine. This new generation of letter sorting machines is enabled with OCR technology. These machines scan the incoming letters and read the zip code. Using the ZIP code these letters can be routed to their correct destination cities.

This chapter will develop an example program that can be trained to recognize human handwriting. We will not create a program that can scan pages of text. Rather this program will read character by character, as the user draws them. This function will be similar to the handwriting recognition used by many PDA's.

Chapter 7: OCR with the Kohonen Neural Network

Article Title: Chapter 7: OCR with the Kohonen Neural Network Category: Artificial Intelligence Most Popular

From Series: Programming Neural Networks in Java

Posted: Wednesday, November 16, 2005 05:15 PM Author: JeffHeaton

Page: 2/5