1
/**/
/*
2 * JOONE - Java Object Oriented Neural Engine
3 * http://joone.sourceforge.net
4 *
5 * XOR_using_NeuralNet.java
6 *
7 */
8 package study;
9
10 import org.joone.engine. * ;
11 import org.joone.engine.learning. * ;
12 import org.joone.io. * ;
13 import org.joone.net. * ;
14 import java.util.Vector;
15
16 public class XOR_using_NeuralNet implements NeuralNetListener {
17 private NeuralNet nnet = null ;
18 private MemoryInputSynapse inputSynapse, desiredOutputSynapse;
19 private MemoryOutputSynapse outputSynapse;
20 LinearLayer input;
21 SigmoidLayer hidden, output;
22 boolean singleThreadMode = true ;
23
24 // XOR input
25 private double [][] inputArray = new double [][] {
26 { 0.0 , 0.0 } ,
27 { 0.0 , 1.0 } ,
28 { 1.0 , 0.0 } ,
29 { 1.0 , 1.0 }
30 } ;
31
32 // XOR desired output
33 private double [][] desiredOutputArray = new double [][] {
34 { 0.0 } ,
35 { 1.0 } ,
36 { 1.0 } ,
37 { 0.0 }
38 } ;
39
40 /** */ /**
41 * @param args the command line arguments
42 */
43 public static void main(String args[]) {
44 XOR_using_NeuralNet xor = new XOR_using_NeuralNet();
45
46 xor.initNeuralNet();
47 xor.train();
48 xor.interrogate();
49 }
50
51 /** */ /**
52 * Method declaration
53 */
54 public void train() {
55
56 // set the inputs
57 inputSynapse.setInputArray(inputArray);
58 inputSynapse.setAdvancedColumnSelector( " 1,2 " );
59 // set the desired outputs
60 desiredOutputSynapse.setInputArray(desiredOutputArray);
61 desiredOutputSynapse.setAdvancedColumnSelector( " 1 " );
62
63 // get the monitor object to train or feed forward
64 Monitor monitor = nnet.getMonitor();
65
66 // set the monitor parameters
67 monitor.setLearningRate( 0.8 );
68 monitor.setMomentum( 0.3 );
69 monitor.setTrainingPatterns(inputArray.length);
70 monitor.setTotCicles( 5000 );
71 monitor.setLearning( true );
72
73 long initms = System.currentTimeMillis();
74 // Run the network in single-thread, synchronized mode
75 nnet.getMonitor().setSingleThreadMode(singleThreadMode);
76 nnet.go( true );
77 System.out.println( " Total time= " + (System.currentTimeMillis() - initms) + " ms " );
78 }
79
80 private void interrogate() {
81 // set the inputs
82 inputSynapse.setInputArray(inputArray);
83 inputSynapse.setAdvancedColumnSelector( " 1,2 " );
84 Monitor monitor = nnet.getMonitor();
85 monitor.setTrainingPatterns( 4 );
86 monitor.setTotCicles( 1 );
87 monitor.setLearning( false );
88 MemoryOutputSynapse memOut = new MemoryOutputSynapse();
89 // set the output synapse to write the output of the net
90
91 if (nnet != null ) {
92 nnet.addOutputSynapse(memOut);
93 System.out.println(nnet.check());
94 nnet.getMonitor().setSingleThreadMode(singleThreadMode);
95 nnet.go();
96
97 for ( int i = 0 ; i < 4 ; i ++ ) {
98 double [] pattern = memOut.getNextPattern();
99 System.out.println( " Output pattern # " + (i + 1 ) + " = " + pattern[ 0 ]);
100 }
101 System.out.println( " Interrogating Finished " );
102 }
103 }
104
105 /** */ /**
106 * Method declaration
107 */
108 protected void initNeuralNet() {
109
110 // First create the three layers
111 input = new LinearLayer();
112 hidden = new SigmoidLayer();
113 output = new SigmoidLayer();
114
115 // set the dimensions of the layers
116 input.setRows( 2 );
117 hidden.setRows( 3 );
118 output.setRows( 1 );
119
120 input.setLayerName( " L.input " );
121 hidden.setLayerName(
2 * JOONE - Java Object Oriented Neural Engine
3 * http://joone.sourceforge.net
4 *
5 * XOR_using_NeuralNet.java
6 *
7 */
8 package study;
9
10 import org.joone.engine. * ;
11 import org.joone.engine.learning. * ;
12 import org.joone.io. * ;
13 import org.joone.net. * ;
14 import java.util.Vector;
15
16 public class XOR_using_NeuralNet implements NeuralNetListener {
17 private NeuralNet nnet = null ;
18 private MemoryInputSynapse inputSynapse, desiredOutputSynapse;
19 private MemoryOutputSynapse outputSynapse;
20 LinearLayer input;
21 SigmoidLayer hidden, output;
22 boolean singleThreadMode = true ;
23
24 // XOR input
25 private double [][] inputArray = new double [][] {
26 { 0.0 , 0.0 } ,
27 { 0.0 , 1.0 } ,
28 { 1.0 , 0.0 } ,
29 { 1.0 , 1.0 }
30 } ;
31
32 // XOR desired output
33 private double [][] desiredOutputArray = new double [][] {
34 { 0.0 } ,
35 { 1.0 } ,
36 { 1.0 } ,
37 { 0.0 }
38 } ;
39
40 /** */ /**
41 * @param args the command line arguments
42 */
43 public static void main(String args[]) {
44 XOR_using_NeuralNet xor = new XOR_using_NeuralNet();
45
46 xor.initNeuralNet();
47 xor.train();
48 xor.interrogate();
49 }
50
51 /** */ /**
52 * Method declaration
53 */
54 public void train() {
55
56 // set the inputs
57 inputSynapse.setInputArray(inputArray);
58 inputSynapse.setAdvancedColumnSelector( " 1,2 " );
59 // set the desired outputs
60 desiredOutputSynapse.setInputArray(desiredOutputArray);
61 desiredOutputSynapse.setAdvancedColumnSelector( " 1 " );
62
63 // get the monitor object to train or feed forward
64 Monitor monitor = nnet.getMonitor();
65
66 // set the monitor parameters
67 monitor.setLearningRate( 0.8 );
68 monitor.setMomentum( 0.3 );
69 monitor.setTrainingPatterns(inputArray.length);
70 monitor.setTotCicles( 5000 );
71 monitor.setLearning( true );
72
73 long initms = System.currentTimeMillis();
74 // Run the network in single-thread, synchronized mode
75 nnet.getMonitor().setSingleThreadMode(singleThreadMode);
76 nnet.go( true );
77 System.out.println( " Total time= " + (System.currentTimeMillis() - initms) + " ms " );
78 }
79
80 private void interrogate() {
81 // set the inputs
82 inputSynapse.setInputArray(inputArray);
83 inputSynapse.setAdvancedColumnSelector( " 1,2 " );
84 Monitor monitor = nnet.getMonitor();
85 monitor.setTrainingPatterns( 4 );
86 monitor.setTotCicles( 1 );
87 monitor.setLearning( false );
88 MemoryOutputSynapse memOut = new MemoryOutputSynapse();
89 // set the output synapse to write the output of the net
90
91 if (nnet != null ) {
92 nnet.addOutputSynapse(memOut);
93 System.out.println(nnet.check());
94 nnet.getMonitor().setSingleThreadMode(singleThreadMode);
95 nnet.go();
96
97 for ( int i = 0 ; i < 4 ; i ++ ) {
98 double [] pattern = memOut.getNextPattern();
99 System.out.println( " Output pattern # " + (i + 1 ) + " = " + pattern[ 0 ]);
100 }
101 System.out.println( " Interrogating Finished " );
102 }
103 }
104
105 /** */ /**
106 * Method declaration
107 */
108 protected void initNeuralNet() {
109
110 // First create the three layers
111 input = new LinearLayer();
112 hidden = new SigmoidLayer();
113 output = new SigmoidLayer();
114
115 // set the dimensions of the layers
116 input.setRows( 2 );
117 hidden.setRows( 3 );
118 output.setRows( 1 );
119
120 input.setLayerName( " L.input " );
121 hidden.setLayerName(