carfield.com.hk Test_Hopfield.class 2000-06-20T16:00:00Z 2000-06-20T16:00:00Z <br/><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2000-06-20T16:00:00Z Hopfield.class 2000-06-19T16:00:00Z 2000-06-19T16:00:00Z <br/><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2000-06-19T16:00:00Z Test_Hopfield.java 2000-06-19T16:00:00Z 2000-06-19T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100"> /** * Title: &lt;p&gt; * Description: &lt;p&gt; * Copyright: Copyright (c) &lt;p&gt; * Company: &lt;p&gt; * @author * @version 1.0 */ public class Test_Hopfield { static float [] data [] = { {1, 1, 1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, 1, 1, 1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, 1, 1, 1} }; static Hopfield test; public static void main(String[] args) { test = new Hopfield(10); test.addTrainingData(data[0]); test.addTrainingData(data[1]); test.addTrainingData(data[2]); test.train(); helper(test, &quot;pattern 0&quot;, data[0]); helper(test, &quot;pattern 1&quot;, data[1]); helper(test, &quot;pattern 2&quot;, data[2]); } private static void helper(Hopfield test, String s, float [] test_data) { float [] dd = new float[10]; for (int i=0; i&lt;10; i++) { dd[i] = test_data[i]; } int index = (int)(9.0f * (float)Math.random()); if (dd[index] &lt; 0.0f) dd[index] = 1.0f; else dd[index] = -1.0f; float [] rr = test.recall(dd, 5); System.out.println(s); for (int i=0; i&lt;10; i++) System.out.print(pp(rr[i]) + &quot; &quot;); System.out.println(); } private static int pp(float x) { if (x &gt; 0.1f) return 1; return 0; } } </TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2000-06-19T16:00:00Z misc.dfPackage 2000-06-19T16:00:00Z 2000-06-19T16:00:00Z <br/><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2000-06-19T16:00:00Z Hopfield.java 2000-06-18T16:00:00Z 2000-06-18T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100"> /** * Title: &lt;p&gt; * Description: &lt;p&gt; * Copyright: Copyright (c) &lt;p&gt; * Company: &lt;p&gt; * @author * @version 1.0 */ import java.util.*; public class Hopfield { public Hopfield(int numInputs) { this.numInputs = numInputs; weights = new float[numInputs][numInputs]; inputCells = new float[numInputs]; tempStorage = new float[numInputs]; } public void addTrainingData(float [] data) { trainingData.addElement(data); } public void train() { for (int j=1; j&lt;numInputs; j++) { for (int i=0; i&lt;j; i++) { for (int n=0; n&lt;trainingData.size(); n++) { float [] data = (float [])trainingData.elementAt(n); float temp1 = adjustInput(data[i]) * adjustInput(data[j]); float temp = truncate(temp1 + weights[j][i]); weights[i][j] = weights[j][i] = temp; } } } for (int i=0; i&lt;numInputs; i++) { tempStorage[i] = 0.0f; for (int j=0; j&lt;i; j++) { tempStorage[i] += weights[i][j]; } } } public float [] recall(float [] pattern, int numIterations) { for (int i=0; i&lt;numInputs; i++) inputCells[i] = pattern[i]; for (int ii = 0; ii&lt;numIterations; ii++) { for (int i=0; i&lt;numInputs; i++) { if (deltaEnergy(i) &gt; 0.0f) { inputCells[i] = 1.0f; } else { inputCells[i] = 0.0f; } } } return inputCells; } private float adjustInput(float x) { if (x &lt; 0.1f) return -1.0f; return 1.0f; } private float truncate(float x) { //return Math.round(x); int i = (int)x; return (float)i; } private float deltaEnergy(int index) { float temp = 0.0f; for (int j=0; j&lt;numInputs; j++) { temp += weights[index][j] * inputCells[j]; } return 2.0f * temp - tempStorage[index]; } int numInputs; Vector trainingData = new Vector(); float [][] weights; float [] tempStorage; float [] inputCells; } </TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2000-06-18T16:00:00Z Neural_1H.java 2000-06-18T16:00:00Z 2000-06-18T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">/** * Title: Neural_1H&lt;p&gt; * Description: A one hidden layer back propagtion neural network model&lt;p&gt; * Copyright: Copyright (c) 2000 Mark Watson. Artistic Open Source License&lt;p&gt; * @author Mark Watson * @version 1.2 */ import java.util.*; import java.io.*; class Neural_1H implements Serializable { protected int numInputs; protected int numHidden; protected int numOutputs; protected int numTraining; public float inputs[]; protected float hidden[]; public float outputs[]; protected float W1[][]; protected float W2[][]; protected float output_errors[]; protected float hidden_errors[]; transient protected Vector inputTraining = new Vector(); transient protected Vector outputTraining = new Vector(); Neural_1H(int num_in, int num_hidden, int num_output) { numInputs = num_in; numHidden = num_hidden; numOutputs = num_output; inputs = new float[numInputs]; hidden = new float[numHidden]; outputs = new float[numOutputs]; W1 = new float[numInputs][numHidden]; W2 = new float[numHidden][numOutputs]; randomizeWeights(); output_errors = new float[numOutputs]; hidden_errors = new float[numHidden]; } public void addTrainingExample(float [] inputs, float [] outputs) { if (inputs.length != numInputs || outputs.length != numOutputs) { System.out.println(&quot;addTrainingExample(): array size is wrong&quot;); return; } inputTraining.addElement(inputs); outputTraining.addElement(outputs); } public static Neural_1H Factory(String serialized_file_name) { Neural_1H nn = null; try { InputStream ins = ClassLoader.getSystemResourceAsStream(serialized_file_name); if (ins==null) { System.out.println(&quot;CachedExamples(): failed to open 'cache.dat' in JAR file&quot;); System.exit(1); } else { ObjectInputStream p = new ObjectInputStream(ins); nn = (Neural_1H)p.readObject(); nn.inputTraining = new Vector(); nn.outputTraining = new Vector(); ins.close(); } } catch (Exception e) { e.printStackTrace(); return null; } return nn; } public void save(String file_name) { try { FileOutputStream ostream = new FileOutputStream(file_name); ObjectOutputStream p = new ObjectOutputStream(ostream); p.writeObject(this); p.flush(); ostream.close(); } catch (Exception e) { e.printStackTrace(); } } public void randomizeWeights() { // Randomize weights here: for (int ii=0; ii&lt;numInputs; ii++) for (int hh=0; hh&lt;numHidden; hh++) W1[ii][hh] = 0.1f * (float)Math.random() - 0.05f; for (int hh=0; hh&lt;numHidden; hh++) for (int oo=0; oo&lt;numOutputs; oo++) W2[hh][oo] = 0.1f * (float)Math.random() - 0.05f; } public float [] recall(float [] in) { for (int i=0; i&lt;numInputs; i++) inputs[i] = in[i]; forwardPass(); float [] ret = new float[numOutputs]; for (int i=0; i&lt;numOutputs; i++) ret[i] = outputs[i]; return ret; } public void forwardPass() { int i, h, o; for (h=0; h&lt;numHidden; h++) { hidden[h] = 0.0f; } for (i=0; i&lt;numInputs; i++) { for (h=0; h&lt;numHidden; h++) { hidden[h] += inputs[i] * W1[i][h]; } } for (o=0; o&lt;numOutputs; o++) outputs[o] = 0.0f; for (h=0; h&lt;numHidden; h++) { for (o=0; o&lt;numOutputs; o++) { outputs[o] += sigmoid(hidden[h]) * W2[h][o]; } } for (o=0; o&lt;numOutputs; o++) outputs[o] = sigmoid(outputs[o]); } public float train() { return train(inputTraining, outputTraining); } // for debug graphics: train only one example at a time: private int current_example = 0; public float train(Vector v_ins, Vector v_outs) { int i, h, o; float error = 0.0f; int num_cases = v_ins.size(); //for (int example=0; example&lt;num_cases; example++) { // zero out error arrays: for (h=0; h&lt;numHidden; h++) hidden_errors[h] = 0.0f; for (o=0; o&lt;numOutputs; o++) output_errors[o] = 0.0f; // copy the input values: for (i=0; i&lt;numInputs; i++) { inputs[i] = ((float [])v_ins.elementAt(current_example))[i]; } // copy the ouytput values: float [] outs = (float [])v_outs.elementAt(current_example); // perform a forward pass through the network: forwardPass(); for (o=0; o&lt;numOutputs; o++) { output_errors[o] = (outs[o] - outputs[o]) *sigmoidP(outputs[o]); } for (h=0; h&lt;numHidden; h++) { hidden_errors[h] = 0.0f; for (o=0; o&lt;numOutputs; o++) { hidden_errors[h] += output_errors[o]*W2[h][o]; } } for (h=0; h&lt;numHidden; h++) { hidden_errors[h] = hidden_errors[h]*sigmoidP(hidden[h]); } // update the hidden to output weights: for (o=0; o&lt;numOutputs; o++) { for (h=0; h&lt;numHidden; h++) { W2[h][o] += 0.5 * output_errors[o] * hidden[h]; } } // update the input to hidden weights: for (h=0; h&lt;numHidden; h++) { for (i=0; i&lt;numInputs; i++) { W1[i][h] += 0.5 * hidden_errors[h] * inputs[i]; } } for (o=0; o&lt;numOutputs; o++) error += Math.abs(output_errors[o]); //} current_example++; if (current_example &gt;= num_cases) current_example = 0; return error; } protected float sigmoid(float x) { return (float)((1.0f/(1.0f+Math.exp((double)(-x))))-0.5f); } protected float sigmoidP(float x) { double z = sigmoid(x) + 0.5f; return (float)(z * (1.0f - z)); } } </TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2000-06-18T16:00:00Z Graph.java 2000-06-17T16:00:00Z 2000-06-17T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100"> import javax.swing.*; import java.awt.*; /** * Title: Graph&lt;p&gt; * Description: Simple program to produce a graph for the book&lt;p&gt; * Copyright: Copyright (c) Mark Watson&lt;p&gt; * Company: &lt;p&gt; * @author Mark Watson * @version 1.0 */ public class Graph extends JFrame { GraphPanel jPanel1; float [] data1; float [] data2; public Graph() { try { int size = 500; data1 = new float[size]; data2 = new float[size]; float xmin = -5; float xmax = 5; for (int i=0; i&lt;size; i++) { float x = i; x = xmin + x * (xmax - xmin) / (float)size; data1[i] = sigmoid(x); data2[i] = sigmoidP(x); } jbInit(); } catch(Exception e) { e.printStackTrace(); } } protected float sigmoid(float x) { return (float)((1.0f/(1.0f+Math.exp((double)(-x))))-0.5f); } protected float sigmoidP(float x) { double z = sigmoid(x) + 0.5f; return (float)(z * (1.0f - z)); } public static void main(String[] args) { Graph untitled11 = new Graph(); } private void jbInit() throws Exception { jPanel1 = new GraphPanel(data1, data2); jPanel1.setBackground(Color.white); this.setDefaultCloseOperation(3); this.getContentPane().add(jPanel1, BorderLayout.CENTER); setSize(550, 300); jPanel1.setVisible(true); setVisible(true); } } </TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2000-06-17T16:00:00Z GraphPanel.java 2000-06-17T16:00:00Z 2000-06-17T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">/** * Title: &lt;p&gt; * Description: &lt;p&gt; * Copyright: Copyright (c) &lt;p&gt; * Company: &lt;p&gt; * @author * @version 1.0 */ import javax.swing.*; import java.awt.*; public class GraphPanel extends java.awt.Canvas { // JPanel { public GraphPanel(float [] data1, float []data2) { super(); this.data1 = data1; this.data2 = data2; } Color black = new Color(0, 0, 0); float [] data1; float [] data2; public void paint(Graphics g) { if (data1 == null || data2 == null) return; int width = this.getWidth(); int height = this.getHeight(); System.out.println(&quot;height=&quot;+height); float min = 99999999.9f; float max = -min; int maxindex = 0; float maxval = 0.0f; for (int i=0; i&lt;data1.length; i++) { // assume length of data1 and data2 are the same if (min &gt; data1[i]) min = data1[i]; if (max &lt; data1[i]) max = data1[i]; if (min &gt; data2[i]) min = data2[i]; if (max &lt; data2[i]) max = data2[i]; } System.out.println(&quot;min=&quot; + min +&quot;, max=&quot; + max); g.setColor(Color.red); for (int i=0; i&lt;data1.length - 1; i++) { float y1 = height - 5 - 0.95f *height * ((data1[i] - min) / (max - min)); float y2 = height - 5 - 0.95f *height * ((data1[i+1] - min) / (max - min)); //System.out.println(&quot;data[&quot;+i+&quot;]=&quot;+data[i]+&quot;, y1=&quot;+y1+&quot;, y2=&quot;+y2); g.drawLine(i+20, (int)y1, i+21, (int)y2); y1 = height - 5 - 0.95f *height * ((data2[i] - min) / (max - min)); y2 = height - 5 - 0.95f *height * ((data2[i+1] - min) / (max - min)); //System.out.println(&quot;data[&quot;+i+&quot;]=&quot;+data[i]+&quot;, y1=&quot;+y1+&quot;, y2=&quot;+y2); g.drawLine(i+20, (int)y1, i+21, (int)y2); } float yzero = height - 5 - 0.95f *height * ((0.0f - min) / (max - min)); g.setColor(black); g.drawLine(20, (int)yzero, data2.length + 19, (int)yzero); g.drawLine(width / 2, height/2 - 118, width/2, height/2 + 118); g.drawString(&quot;Sigmoid&quot;, width / 2 - 100, 3 * height / 4 - 10); g.drawString(&quot;SigmoidP&quot;, width / 3 - 5, 1 * height / 4 + 10); g.drawString(&quot;-5&quot;, 4, (int)yzero); g.drawString(&quot;5&quot;, width - 19, (int)yzero); g.drawString(&quot;0.5&quot;, width/2 - 7, 12); g.drawString(&quot;-0.5&quot;, width/2 - 9, height - 3); } } </TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2000-06-17T16:00:00Z Test_1H.java 2000-05-09T16:00:00Z 2000-05-09T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">/** * Title: &lt;p&gt; * Description: &lt;p&gt; * Copyright: Copyright (c) &lt;p&gt; * Company: &lt;p&gt; * @author * @version 1.0 */ public class Test_1H { static float [] in1 = { -0.4f, -0.4f, +0.4f }; static float [] in2 = { -0.4f, +0.4f, -0.4f }; static float [] in3 = { +0.4f, -0.4f, -0.4f }; static float [] out1= { +0.4f, -0.4f, -0.4f}; static float [] out2= { -0.4f, -0.4f, +0.4f}; static float [] out3= { -0.4f, +0.4f, -0.4f}; static float [] test1 = { -0.2f, -0.45f, +0.35f }; static float [] test2 = { -0.33f, +0.41f, -0.38f }; static float [] test3 = { +0.33f, -0.41f, -0.23f }; public static void main(String[] args) { Neural_1H nn = new Neural_1H(3, 3, 3); nn.addTrainingExample(in1, out1); nn.addTrainingExample(in2, out2); nn.addTrainingExample(in3, out3); for (int i=0; i&lt;302; i++) { float error = nn.train(); if ((i + 19) % 20 == 0) System.out.println(&quot;cycle &quot; + i + &quot; error is &quot; + error); } test_recall(nn, test1); test_recall(nn, test2); test_recall(nn, test3); nn.save(&quot;test.neural&quot;); Neural_1H nn2 = Neural_1H.Factory(&quot;test.neural&quot;); nn2.addTrainingExample(in1, out1); nn2.addTrainingExample(in2, out2); nn2.addTrainingExample(in3, out3); for (int i=0; i&lt;102; i++) { float error = nn2.train(); if ((i + 19) % 20 == 0) System.out.println(&quot;cycle &quot; + i + &quot; error is &quot; + error); } test_recall(nn2, test1); test_recall(nn2, test2); test_recall(nn2, test3); } public static void test_recall(Neural_1H nn, float [] inputs) { float [] results = nn.recall(inputs); System.out.print(&quot;Test case: &quot;); for (int i=0; i&lt;inputs.length; i++) System.out.print(pp(inputs[i]) + &quot; &quot;); System.out.print(&quot; results: &quot;); for (int i=0; i&lt;results.length; i++) System.out.print(pp(results[i]) + &quot; &quot;); System.out.println(); } public static String pp(float x) { String s = new String(&quot;&quot; + x + &quot;00&quot;); int index = s.indexOf(&quot;.&quot;); if (index &gt; -1) s = s.substring(0, index + 3); if (s.startsWith(&quot;-&quot;) == false) s = &quot; &quot; + s; return s; } }</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2000-05-09T16:00:00Z GUITest_2H$1.class 2000-05-07T16:00:00Z 2000-05-07T16:00:00Z <br/><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2000-05-07T16:00:00Z GUITest_2H.class 2000-05-07T16:00:00Z 2000-05-07T16:00:00Z <br/><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2000-05-07T16:00:00Z neural.jpr 2000-05-07T16:00:00Z 2000-05-07T16:00:00Z <br/><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2000-05-07T16:00:00Z GUITest_2H.java 2000-04-18T16:00:00Z 2000-04-18T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">/** * Title: &lt;p&gt; * Description: &lt;p&gt; * Copyright: Copyright (c) &lt;p&gt; * Company: &lt;p&gt; * @author * @version 1.0 */ import javax.swing.*; import java.awt.*; import java.awt.event.*; public class GUITest_2H extends JFrame { static float [] in1 = { -0.4f, -0.4f, +0.4f }; static float [] in2 = { -0.4f, +0.4f, -0.4f }; static float [] in3 = { +0.4f, -0.4f, -0.4f }; static float [] out1= { +0.4f, -0.4f, -0.4f}; static float [] out2= { -0.4f, -0.4f, +0.4f}; static float [] out3= { -0.4f, +0.4f, -0.4f}; static float [] test1 = { -0.2f, -0.45f, +0.35f }; static float [] test2 = { -0.33f, +0.41f, -0.38f }; static float [] test3 = { +0.33f, -0.41f, -0.23f }; Neural_2H nn = new Neural_2H(3, 3, 3, 3); Plot1DPanel inputPanel = new Plot1DPanel(3, -1.0f, 1.0f, nn.inputs); Plot1DPanel hidden1Panel = new Plot1DPanel(3, -1.0f, 1.0f, nn.hidden1); Plot1DPanel hidden2Panel = new Plot1DPanel(3, -1.0f, 1.0f, nn.hidden2); Plot1DPanel outputPanel = new Plot1DPanel(3, -1.0f, 1.0f, nn.outputs); Plot2DPanel w1Panel = new Plot2DPanel(3, 3, -1.0f, 1.0f, nn.W1); Plot2DPanel w2Panel = new Plot2DPanel(3, 3, -1.0f, 1.0f, nn.W2); Plot2DPanel w3Panel = new Plot2DPanel(3, 3, -1.0f, 1.0f, nn.W3); JButton jButton1 = new JButton(); JLabel jLabel1 = new JLabel(); JLabel jLabel2 = new JLabel(); JLabel jLabel2b = new JLabel(); JLabel jLabel3 = new JLabel(); JLabel jLabel4 = new JLabel(); JLabel jLabel4b = new JLabel(); JLabel jLabel5 = new JLabel(); public GUITest_2H() { try { nn.addTrainingExample(in1, out1); nn.addTrainingExample(in2, out2); nn.addTrainingExample(in3, out3); jbInit(); this.setSize(450, 450); this.setVisible(true); } catch(Exception e) { e.printStackTrace(); } } public static void main(String[] args) { GUITest_2H GUITest_1H1 = new GUITest_2H(); } private void jbInit() throws Exception { this.getContentPane().setLayout(null); inputPanel.setBounds(new Rectangle(5, 30, 400, 20)); hidden1Panel.setBounds(new Rectangle(5, 138, 400, 20)); hidden2Panel.setBounds(new Rectangle(5, 238, 400, 20)); outputPanel.setBounds(new Rectangle(5, 340, 400, 20)); w1Panel.setBounds(new Rectangle(160, 50, 60, 60)); w2Panel.setBounds(new Rectangle(160, 158, 60, 60)); w3Panel.setBounds(new Rectangle(160, 258, 60, 60)); jButton1.setText(&quot;Rest and Run&quot;); jButton1.setBounds(new Rectangle(246, 380, 148, 28)); jButton1.addMouseListener(new java.awt.event.MouseAdapter() { public void mousePressed(MouseEvent e) { do_run_button(e); } }); this.setDefaultCloseOperation(3); jLabel1.setText(&quot;Input neurons:&quot;); jLabel1.setBounds(new Rectangle(4, 3, 144, 19)); jLabel2.setText(&quot;Hidden 1 neurons:&quot;); jLabel2.setBounds(new Rectangle(4, 102, 144, 19)); jLabel2b.setText(&quot;Hidden 2 neurons:&quot;); jLabel2b.setBounds(new Rectangle(4, 202, 144, 19)); jLabel3.setText(&quot;Output neurons:&quot;); jLabel3.setBounds(new Rectangle(4, 308, 240, 19)); jLabel4.setText(&quot;input to hidden 1 weights&quot;); jLabel4.setBounds(new Rectangle(230, 80, 170, 19)); jLabel4b.setText(&quot;hidden 1 to hidden 2 weights&quot;); jLabel4b.setBounds(new Rectangle(230, 180, 170, 19)); jLabel5.setText(&quot;hidden 2 to output weights&quot;); jLabel5.setBounds(new Rectangle(230, 280, 170, 19)); this.getContentPane().add(inputPanel, null); this.getContentPane().add(hidden1Panel, null); this.getContentPane().add(hidden2Panel, null); this.getContentPane().add(outputPanel, null); this.getContentPane().add(w1Panel, null); this.getContentPane().add(w2Panel, null); this.getContentPane().add(w3Panel, null); this.getContentPane().add(jButton1, null); this.getContentPane().add(jLabel1, null); this.getContentPane().add(jLabel2, null); this.getContentPane().add(jLabel2b, null); this.getContentPane().add(jLabel3, null); this.getContentPane().add(jLabel4, null); this.getContentPane().add(jLabel4b, null); this.getContentPane().add(jLabel5, null); } void do_run_button(MouseEvent e) { Graphics g1 = inputPanel.getGraphics(); Graphics g2 = hidden1Panel.getGraphics(); Graphics g3 = hidden2Panel.getGraphics(); Graphics g4 = outputPanel.getGraphics(); Graphics g5 = w1Panel.getGraphics(); Graphics g6 = w2Panel.getGraphics(); Graphics g7 = w3Panel.getGraphics(); for (int i=0; i&lt;5600; i++) { float error = nn.train(); //if ((i + 19) % 20 == 0) System.out.println(&quot;cycle &quot; + i + &quot; error is &quot; + error); inputPanel.paint(g1); hidden1Panel.paint(g2); hidden2Panel.paint(g3); outputPanel.paint(g4); w1Panel.paint(g5); w2Panel.paint(g6); w3Panel.paint(g7); if (i &gt; 5580) { try { Thread.sleep(300L); } catch (Exception e9) { } } else { try { Thread.sleep(10L); } catch (Exception e9) { } } } } } </TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2000-04-18T16:00:00Z GUITest_1H$1.class 2000-04-17T16:00:00Z 2000-04-17T16:00:00Z <br/><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2000-04-17T16:00:00Z GUITest_1H.class 2000-04-17T16:00:00Z 2000-04-17T16:00:00Z <br/><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2000-04-17T16:00:00Z Neural_1H.class 2000-04-17T16:00:00Z 2000-04-17T16:00:00Z <br/><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2000-04-17T16:00:00Z Neural_2H.class 2000-04-17T16:00:00Z 2000-04-17T16:00:00Z <br/><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2000-04-17T16:00:00Z Neural_2H.java 2000-04-17T16:00:00Z 2000-04-17T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">/** * Title: Neural_2H&lt;p&gt; * Description: Two hidden layer back propagtion neural network model&lt;p&gt; * Copyright: Copyright (c) 2000 Mark Watson. Artisitic Open Source License&lt;p&gt; * @author Mark Watson * @version 1.2 */ import java.util.*; import java.io.*; class Neural_2H implements Serializable { protected int numInputs; protected int numHidden1; protected int numHidden2; protected int numOutputs; protected int numTraining; public float inputs[]; protected float hidden1[]; protected float hidden2[]; public float outputs[]; protected float W1[][]; protected float W2[][]; protected float W3[][]; protected float output_errors[]; protected float hidden1_errors[]; protected float hidden2_errors[]; transient protected Vector inputTraining = new Vector(); transient protected Vector outputTraining = new Vector(); Neural_2H(int num_in, int num_hidden1, int num_hidden2, int num_output) { numInputs = num_in; numHidden1 = num_hidden1; numHidden2 = num_hidden2; numOutputs = num_output; inputs = new float[numInputs]; hidden1 = new float[numHidden1]; hidden2 = new float[numHidden2]; outputs = new float[numOutputs]; W1 = new float[numInputs][numHidden1]; W2 = new float[numHidden1][numHidden2]; W3 = new float[numHidden2][numOutputs]; randomizeWeights(); output_errors = new float[numOutputs]; hidden1_errors = new float[numHidden1]; hidden2_errors = new float[numHidden2]; } public void addTrainingExample(float [] inputs, float [] outputs) { if (inputs.length != numInputs || outputs.length != numOutputs) { System.out.println(&quot;addTrainingExample(): array size is wrong&quot;); return; } inputTraining.addElement(inputs); outputTraining.addElement(outputs); } public static Neural_2H Factory(String serialized_file_name) { Neural_2H nn = null; try { InputStream ins = ClassLoader.getSystemResourceAsStream(serialized_file_name); if (ins==null) { System.out.println(&quot;CachedExamples(): failed to open 'cache.dat' in JAR file&quot;); System.exit(1); } else { ObjectInputStream p = new ObjectInputStream(ins); nn = (Neural_2H)p.readObject(); nn.inputTraining = new Vector(); nn.outputTraining = new Vector(); ins.close(); } } catch (Exception e) { e.printStackTrace(); return null; } return nn; } public void save(String file_name) { try { FileOutputStream ostream = new FileOutputStream(file_name); ObjectOutputStream p = new ObjectOutputStream(ostream); p.writeObject(this); p.flush(); ostream.close(); } catch (Exception e) { e.printStackTrace(); } } public void randomizeWeights() { // Randomize weights here: for (int ii=0; ii&lt;numInputs; ii++) for (int hh=0; hh&lt;numHidden1; hh++) W1[ii][hh] = 0.1f * (float)Math.random() - 0.05f; for (int ii=0; ii&lt;numHidden1; ii++) for (int hh=0; hh&lt;numHidden2; hh++) W2[ii][hh] = 0.1f * (float)Math.random() - 0.05f; for (int hh=0; hh&lt;numHidden2; hh++) for (int oo=0; oo&lt;numOutputs; oo++) W3[hh][oo] = 0.1f * (float)Math.random() - 0.05f; } public float [] recall(float [] in) { for (int i=0; i&lt;numInputs; i++) inputs[i] = in[i]; forwardPass(); float [] ret = new float[numOutputs]; for (int i=0; i&lt;numOutputs; i++) ret[i] = outputs[i]; return ret; } public void forwardPass() { int i, h, o; for (h=0; h&lt;numHidden1; h++) { hidden1[h] = 0.0f; } for (h=0; h&lt;numHidden2; h++) { hidden2[h] = 0.0f; } for (i=0; i&lt;numInputs; i++) { for (h=0; h&lt;numHidden1; h++) { hidden1[h] += inputs[i] * W1[i][h]; } } for (i=0; i&lt;numHidden1; i++) { for (h=0; h&lt;numHidden2; h++) { hidden2[h] += hidden1[i] * W2[i][h]; } } for (o=0; o&lt;numOutputs; o++) outputs[o] = 0.0f; for (h=0; h&lt;numHidden2; h++) { for (o=0; o&lt;numOutputs; o++) { outputs[o] += sigmoid(hidden2[h]) * W3[h][o]; } } for (o=0; o&lt;numOutputs; o++) outputs[o] = sigmoid(outputs[o]); } public float train() { return train(inputTraining, outputTraining); } private int current_example = 0; public float train(Vector ins, Vector v_outs) { int i, h, o; float error = 0.0f; int num_cases = ins.size(); //for (int example=0; example&lt;num_cases; example++) { // zero out error arrays: for (h=0; h&lt;numHidden1; h++) hidden1_errors[h] = 0.0f; for (h=0; h&lt;numHidden2; h++) hidden2_errors[h] = 0.0f; for (o=0; o&lt;numOutputs; o++) output_errors[o] = 0.0f; // copy the input values: for (i=0; i&lt;numInputs; i++) { inputs[i] = ((float [])ins.elementAt(current_example))[i]; } // copy the output values: float [] outs = (float [])v_outs.elementAt(current_example); // perform a forward pass through the network: forwardPass(); for (o=0; o&lt;numOutputs; o++) { output_errors[o] = (outs[o] - outputs[o]) *sigmoidP(outputs[o]); } for (h=0; h&lt;numHidden2; h++) { hidden2_errors[h] = 0.0f; for (o=0; o&lt;numOutputs; o++) { hidden2_errors[h] += output_errors[o]*W3[h][o]; } } for (h=0; h&lt;numHidden1; h++) { hidden1_errors[h] = 0.0f; for (o=0; o&lt;numHidden2; o++) { hidden1_errors[h] += hidden2_errors[o]*W2[h][o]; } } for (h=0; h&lt;numHidden2; h++) { hidden2_errors[h] = hidden2_errors[h]*sigmoidP(hidden2[h]); } for (h=0; h&lt;numHidden1; h++) { hidden1_errors[h] = hidden1_errors[h]*sigmoidP(hidden1[h]); } // update the hidden2 to output weights: for (o=0; o&lt;numOutputs; o++) { for (h=0; h&lt;numHidden2; h++) { W3[h][o] += 0.5 * output_errors[o] * hidden2[h]; } } // update the hidden1 to hidden2 weights: for (o=0; o&lt;numHidden2; o++) { for (h=0; h&lt;numHidden1; h++) { W2[h][o] += 0.5 * hidden2_errors[o] * hidden1[h]; } } // update the input to hidden1 weights: for (h=0; h&lt;numHidden1; h++) { for (i=0; i&lt;numInputs; i++) { W1[i][h] += 0.5 * hidden1_errors[h] * inputs[i]; } } for (o=0; o&lt;numOutputs; o++) error += Math.abs(output_errors[o]); //} current_example++; if (current_example &gt;= num_cases) current_example = 0; return error; } protected float sigmoid(float x) { return (float)((1.0f/(1.0f+Math.exp((double)(-x))))-0.5f); } protected float sigmoidP(float x) { double z = sigmoid(x) + 0.5f; return (float)(z * (1.0f - z)); } } </TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2000-04-17T16:00:00Z Plot1DPanel.class 2000-04-17T16:00:00Z 2000-04-17T16:00:00Z <br/><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2000-04-17T16:00:00Z Plot2DPanel.class 2000-04-17T16:00:00Z 2000-04-17T16:00:00Z <br/><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2000-04-17T16:00:00Z Test_1H.class 2000-04-17T16:00:00Z 2000-04-17T16:00:00Z <br/><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2000-04-17T16:00:00Z Test_2H.class 2000-04-17T16:00:00Z 2000-04-17T16:00:00Z <br/><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2000-04-17T16:00:00Z GUITest_1H.java 2000-04-16T16:00:00Z 2000-04-16T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100"> import javax.swing.*; import java.awt.*; import java.awt.event.*; /** * Title: &lt;p&gt; * Description: &lt;p&gt; * Copyright: Copyright (c) &lt;p&gt; * Company: &lt;p&gt; * @author * @version 1.0 */ public class GUITest_1H extends JFrame { static float [] in1 = { -0.4f, -0.4f, +0.4f }; static float [] in2 = { -0.4f, +0.4f, -0.4f }; static float [] in3 = { +0.4f, -0.4f, -0.4f }; static float [] out1= { +0.4f, -0.4f, -0.4f}; static float [] out2= { -0.4f, -0.4f, +0.4f}; static float [] out3= { -0.4f, +0.4f, -0.4f}; static float [] test1 = { -0.2f, -0.45f, +0.35f }; static float [] test2 = { -0.33f, +0.41f, -0.38f }; static float [] test3 = { +0.33f, -0.41f, -0.23f }; Neural_1H nn = new Neural_1H(3, 3, 3); Plot1DPanel inputPanel = new Plot1DPanel(3, -1.0f, 1.0f, nn.inputs); Plot1DPanel hiddenPanel = new Plot1DPanel(3, -4.0f, 4.0f, nn.hidden); Plot1DPanel outputPanel = new Plot1DPanel(3, -1.0f, 1.0f, nn.outputs); Plot2DPanel w1Panel = new Plot2DPanel(3, 3, -1.0f, 1.0f, nn.W1); Plot2DPanel w2Panel = new Plot2DPanel(3, 3, -4.0f, 4.0f, nn.W2); JButton jButton1 = new JButton(); JLabel jLabel1 = new JLabel(); JLabel jLabel2 = new JLabel(); JLabel jLabel3 = new JLabel(); JLabel jLabel4 = new JLabel(); JLabel jLabel5 = new JLabel(); public GUITest_1H() { try { nn.addTrainingExample(in1, out1); nn.addTrainingExample(in2, out2); nn.addTrainingExample(in3, out3); jbInit(); this.setSize(450, 350); this.setVisible(true); } catch(Exception e) { e.printStackTrace(); } } public static void main(String[] args) { GUITest_1H GUITest_1H1 = new GUITest_1H(); } private void jbInit() throws Exception { this.getContentPane().setLayout(null); inputPanel.setBounds(new Rectangle(5, 30, 400, 20)); hiddenPanel.setBounds(new Rectangle(5, 138, 400, 20)); outputPanel.setBounds(new Rectangle(5, 240, 400, 20)); w1Panel.setBounds(new Rectangle(200, 50, 60, 60)); w2Panel.setBounds(new Rectangle(200, 158, 60, 60)); jButton1.setText(&quot;Rest and Run&quot;); jButton1.setBounds(new Rectangle(246, 290, 148, 28)); jButton1.addMouseListener(new java.awt.event.MouseAdapter() { public void mousePressed(MouseEvent e) { do_run_button(e); } }); this.setDefaultCloseOperation(3); jLabel1.setText(&quot;Input neurons:&quot;); jLabel1.setBounds(new Rectangle(4, 3, 144, 19)); jLabel2.setText(&quot;Hidden neurons:&quot;); jLabel2.setBounds(new Rectangle(4, 102, 144, 19)); jLabel3.setText(&quot;Output neurons:&quot;); jLabel3.setBounds(new Rectangle(4, 208, 240, 19)); jLabel4.setText(&quot;input to hidden weights&quot;); jLabel4.setBounds(new Rectangle(270, 80, 150, 19)); jLabel5.setText(&quot;hidden to output weights&quot;); jLabel5.setBounds(new Rectangle(270, 180, 150, 19)); this.getContentPane().add(inputPanel, null); this.getContentPane().add(hiddenPanel, null); this.getContentPane().add(outputPanel, null); this.getContentPane().add(w1Panel, null); this.getContentPane().add(w2Panel, null); this.getContentPane().add(jButton1, null); this.getContentPane().add(jLabel1, null); this.getContentPane().add(jLabel2, null); this.getContentPane().add(jLabel3, null); this.getContentPane().add(jLabel4, null); this.getContentPane().add(jLabel5, null); } void do_run_button(MouseEvent e) { Graphics g1 = inputPanel.getGraphics(); Graphics g2 = hiddenPanel.getGraphics(); Graphics g3 = outputPanel.getGraphics(); Graphics g4 = w1Panel.getGraphics(); Graphics g5 = w2Panel.getGraphics(); for (int i=0; i&lt;600; i++) { float error = nn.train(); //if ((i + 19) % 20 == 0) System.out.println(&quot;cycle &quot; + i + &quot; error is &quot; + error); inputPanel.paint(g1); hiddenPanel.paint(g2); outputPanel.paint(g3); w1Panel.paint(g4); w2Panel.paint(g5); if (i &gt; 580) { try { Thread.sleep(500L); } catch (Exception e9) { } } else { try { Thread.sleep(30L); } catch (Exception e9) { } } } } } </TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2000-04-16T16:00:00Z Plot1DPanel.java 2000-04-16T16:00:00Z 2000-04-16T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">/** * Title: &lt;p&gt; * Description: &lt;p&gt; * Copyright: Copyright (c) &lt;p&gt; * Company: &lt;p&gt; * @author * @version 1.0 */ import javax.swing.*; import java.awt.*; public class Plot1DPanel extends java.awt.Canvas { // JPanel { public Plot1DPanel(int num, float min, float max, float [] values) { super(); this.num = num; this.min = min; this.max = max; this.values = values; colors = new Color[100]; for (int i=0; i&lt;100; i++) { float x = 1.0f - ((float)i) * 0.0096f; colors[i] = new Color(x, x, x); } } private int num; private float min, max; private float temp; private float [] values = null; private Color [] colors; //public void plot(float [] values) { //} public void paint(Graphics g) { if (values == null) return; int delta_width = this.getWidth() / num; int delta_height = this.getHeight() / num; for (int i=0; i&lt;num; i++) { //System.out.println(this.toString() + &quot;, values[&quot; + i + &quot;]=&quot; + values[i]); temp = 100.0f * (values[i] - min) / (max - min); int ii = (int)temp; if (ii &lt; 0) ii = 0; if (ii &gt; 99) ii = 99; g.setColor(colors[ii]); g.fillRect(i * delta_width, 0, delta_width, delta_height); } } } </TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2000-04-16T16:00:00Z Plot2DPanel.java 2000-04-16T16:00:00Z 2000-04-16T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">/** * Title: &lt;p&gt; * Description: &lt;p&gt; * Copyright: Copyright (c) &lt;p&gt; * Company: &lt;p&gt; * @author * @version 1.0 */ import javax.swing.*; import java.awt.*; public class Plot2DPanel extends java.awt.Canvas { // JPanel { public Plot2DPanel(int num1, int num2, float min, float max, float [][] values) { super(); this.num1 = num1; this.num2 = num2; this.min = min; this.max = max; this.values = values; colors = new Color[100]; for (int i=0; i&lt;100; i++) { float x = 1.0f - ((float)i) * 0.0096f; colors[i] = new Color(x, x, x); } } private int num1; private int num2; private float min, max; private float temp; private float [][] values = null; private Color [] colors; public void paint(Graphics g) { if (values == null) return; int delta_width = this.getWidth() / num1; int delta_height = this.getHeight() / num2; for (int i=0; i&lt;num1; i++) { for (int j=0; j&lt;num2; j++) { //System.out.println(this.toString() + &quot;, values[&quot; + i + &quot;]=&quot; + values[i]); temp = 100.0f * (values[i][j] - min) / (max - min); int ii = (int)temp; if (ii &lt; 0) ii = 0; if (ii &gt; 99) ii = 99; g.setColor(colors[ii]); g.fillRect(i * delta_width, j * delta_height, delta_width, delta_height); } } } } </TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2000-04-16T16:00:00Z