jueves, 21 de mayo de 2009

HOPFIELD





/**
* Hopfield.java
*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

/**
* Example: The Hopfield Neural Network
*
* This is an example that implements a Hopfield neural
* network. This example network contains four fully
* connected neurons. This file, Hopfield, implements a
* Swing interface into the other two neural network
* classes: Layer and Neuron.
*
* @author Jeff Heaton
* @version 1.0
*/
public class Hopfield extends JFrame implements ActionListener {

/**
* The number of neurons in this neural network.
*/
public static final int NETWORK_SIZE = 8;

/**
* The weight matrix for the four fully connected
* neurons.
*/
JTextField matrix[][] =
new JTextField[NETWORK_SIZE][NETWORK_SIZE];


/**
* The input pattern, used to either train
* or run the neural network. When the network
* is being trained, this is the training
* data. When the neural network is to be ran
* this is the input pattern.
*/
JComboBox input[] = new JComboBox[NETWORK_SIZE];

/**
* The output from each of the four neurons.
*/
JTextField output[] = new JTextField[NETWORK_SIZE];

/**
* The clear button. Used to clear the weight
* matrix.
*/
JButton btnClear = new JButton("Clear");

/**
* The train button. Used to train the
* neural network.
*/
JButton btnTrain = new JButton("Train");

/**
* The run button. Used to run the neural
* network.
*/
JButton btnRun = new JButton("Run");

/**
* Constructor, create all of the components and position
* the JFrame to the center of the screen.
*/
public Hopfield()
{
setTitle("Hopfield Neural Network");

// create connections panel
JPanel connections = new JPanel();
connections.setLayout(
new GridLayout(NETWORK_SIZE,NETWORK_SIZE) );
for ( int row=0;row for ( int col=0;col matrix[row][col] = new JTextField(3);
matrix[row][col].setText("0");
connections.add(matrix[row][col]);
}
}

Container content = getContentPane();

GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
content.setLayout(gridbag);

c.fill = GridBagConstraints.NONE;
c.weightx = 1.0;

// Weight matrix label
c.gridwidth = GridBagConstraints.REMAINDER; //end row
c.anchor = GridBagConstraints.NORTHWEST;
content.add(
new JLabel(
"Weight Matrix for the Hopfield Neural Network:"),c);

// Weight matrix
c.anchor = GridBagConstraints.CENTER;
c.gridwidth = GridBagConstraints.REMAINDER; //end row
content.add(connections,c);
c.gridwidth = 1;

// Input pattern label
c.anchor = GridBagConstraints.NORTHWEST;
c.gridwidth = GridBagConstraints.REMAINDER; //end row
content.add(
new JLabel(
"Click \"Train\" to train the following pattern:"),c);

// Input pattern

String options[] = { "0","1"};

JPanel inputPanel = new JPanel();
inputPanel.setLayout(new FlowLayout());
for ( int i=0;i input[i] = new JComboBox(options);
inputPanel.add(input[i]);

}

c.gridwidth = GridBagConstraints.REMAINDER; //end row
c.anchor = GridBagConstraints.CENTER;
content.add(inputPanel,c);

// Output pattern label
c.anchor = GridBagConstraints.NORTHWEST;
c.gridwidth = GridBagConstraints.REMAINDER; //end row
content.add(
new JLabel("Click \"Run\" to see output pattern:"),c);

// Output pattern

JPanel outputPanel = new JPanel();
outputPanel.setLayout(new FlowLayout());
for ( int i=0;i output[i] = new JTextField(3);
output[i].setEditable(false);
outputPanel.add(output[i]);
}
c.gridwidth = GridBagConstraints.REMAINDER; //end row
c.anchor = GridBagConstraints.CENTER;
content.add(outputPanel,c);

// Buttons

JPanel buttonPanel = new JPanel();
btnClear = new JButton("Clear");
btnTrain = new JButton("Train");
btnRun = new JButton("Run");
btnClear.addActionListener(this);
btnTrain.addActionListener(this);
btnRun.addActionListener(this);
buttonPanel.setLayout(new FlowLayout());
buttonPanel.add(btnClear);
buttonPanel.add(btnTrain);
buttonPanel.add(btnRun);
content.add(buttonPanel,c);

// adjust size and position
pack();
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);


}

/**
* Used to dispatch events from the buttons
* to the handler methods.
*
* @param e The event
*/
public void actionPerformed(ActionEvent e)
{
if ( e.getSource()==btnRun )
run();
else if ( e.getSource()==btnClear )
clear();
else if ( e.getSource()==btnTrain )
train();
}

/**
* Called when the neural network is to be ran against
* the input.
*/
protected void run()
{
boolean pattern[] = new boolean[NETWORK_SIZE];
int wt[][] = new int[NETWORK_SIZE][NETWORK_SIZE];

for ( int row=0;row for ( int col=0;col wt[row][col]=Integer.parseInt(matrix[row][col].getText());
for ( int row=0;row int i = input[row].getSelectedIndex();
if ( i==0 )
pattern[row] = false;
else
pattern[row] = true;
}

Layer net = new Layer(wt);
net.activation(pattern);

for ( int row=0;row if ( net.output[row] )
output[row].setText("1");
else
output[row].setText("0");
if ( net.output[row]==pattern[row] )
output[row].setBackground(java.awt.Color.green);
else
output[row].setBackground(java.awt.Color.red);
}

}

/**
* Called to clear the weight matrix.
*/
protected void clear()
{
for ( int row=0;row for ( int col=0;col matrix[row][col].setText("0");
}

/**
* Called to train the weight matrix based on the
* current input pattern.
*/
protected void train()
{

int work[][] = new int[NETWORK_SIZE][NETWORK_SIZE];
int bi[] = new int[NETWORK_SIZE];

for ( int row=0;row for ( int col=0;col work[row][col] = 0;
}

for ( int x=0;x if ( input[x].getSelectedIndex()==0 )
bi[x] = -1;
else
bi[x] = 1;
}

for ( int row=0;row for ( int col=0;col work[row][col] = bi[row]*bi[col];
}

for ( int x=0;x work[x][x] =0;

for ( int row=0;row for ( int col=0;col int i = Integer.parseInt(matrix[row][col].getText());
matrix[row][col].setText( "" + (work[row][col]));
}
}

/**
* Main program entry point, display the
* frame.
*
* @param args Command line arguments are not used
*/
static public void main(String args[])
{
JFrame f = new Hopfield();
f.show();
}
}
 
/**
* Layer.java
*/
public class Layer {

/**
* An array of neurons.
*/
protected Neuron neuron[] = new Neuron[8];

/**
* The output of the neurons.
*/
protected boolean output[] = new boolean[8];

/**
* The number of neurons in this layer. And because this is a
* single layer neural network, this is also the number of
* neurons in the network.
*/
protected int neurons;

/**
* A constant to multiply against the threshold function.
* This is not used, and is set to 1.
*/
public static final double lambda = 1.0;

/**
* The constructor. The weight matrix for the
* neurons must be passed in. Because this is
* a single layer network the weight array should
* always be perfectly square(i.e. 4x4). These
* weights are used to initialize the neurons.
*
* @param weights A 2d array that contains the weights
* between each
* neuron and the other neurons.
*/
Layer(int weights[][])
{
neurons = weights[0].length;

neuron = new Neuron[neurons];
output = new boolean[neurons];

for ( int i=0;i neuron[i]=new Neuron(weights[i]);
}

/**
* The threshold method is used to determine if the neural
* network will fire for a given pattern. This threshold
* uses the hyperbolic tangent (tanh).
*
* @param k The product of the neuron weights and the input
* pattern.
* @return Whether to fire or not to fire.
*/
public boolean threshold(int k)
{
double kk = k * lambda;
double a = Math.exp( kk );
double b = Math.exp( -kk );
double tanh = (a-b)/(a+b);
return(tanh>=0);
}

/**
* This method is called to actually run the neural network.
*
* @param pattern The input pattern to present to the
* neural network.
*/
void activation(boolean pattern[])
{
int i,j;
for ( i=0;i<8;i++ ) {
neuron[i].activation = neuron[i].act(pattern);
output[i] = threshold(neuron[i].activation);
}
}
}
 
/**
* Neuron.java
*/
public class Neuron {

/**
* The weights between this neuron and the other neurons on
* the layer.
*/
public int weightv[];

/**
* Activation results for this neuron.
*/
public int activation;

/**
* The constructor. The weights between this neuron and
* every other neuron(including itself) is passed in as
* an array. Usually the weight between this neuron and
* itself is zero.
*
* @param in The weight vector.
*/
public Neuron(int in[])
{
weightv = in;
}

/**
* This method is called to determine if the neuron would
* activate, or fire.
*
* @param x Neuron input
* @return If the neuron would activate, or fire
*/
public int act(boolean x[] )
{
int i;
int a=0;

for ( i=0;i if ( x[i] )
a+=weightv[i];
return a;
}

}




todo el codigo fue tomado de la pagina
mejorado para manejar 8 neuronas y controlar al momento de entrenar la neurana
http://www.heatonresearch.com/articles/series/1/

2 comentarios:

  1. el programa es comprensible y describe bien la implementacion de las clases realizadas en clases

    adkje

    ResponderEliminar
  2. La modificacion de Hopfield a Hopfiled Asociativo esta realizda de una muy buena manera.

    ResponderEliminar