/* * JOONE - Java Object Oriented Neural Engine * http://joone.sourceforge.net * * XORMemory.java */ import org.joone.engine.*; import org.joone.engine.learning.*; import org.joone.io.*; import org.joone.net.*; /** * Sample class to demonstrate the use of the MemoryInputSynapse * @author Josï¿?Rodriguez */ public class XORMemory implements NeuralNetListener { private MemoryOutputSynapse memOut; //Variables defined for the entire class private FileOutputSynapse fileOut; /** * @param args the command line arguments */ public static void main(String args[]) { XORMemory xor = new XORMemory(); xor.Go(); } /** * Go() sets up the XOR network and trains it. */ public void Go() { SigmoidLayer input = new SigmoidLayer(); // First, creates the three Layers SigmoidLayer hidden = new SigmoidLayer(); SigmoidLayer output = new SigmoidLayer(); input.setLayerName("input"); hidden.setLayerName("hidden"); output.setLayerName("output"); input.setRows(2); // Set their dimensions hidden.setRows(3); output.setRows(1); // Now create the two Synapses FullSynapse synapse_IH = new FullSynapse(); /* input -> hidden conn. */ FullSynapse synapse_HO = new FullSynapse(); /* hidden -> output conn. */ synapse_IH.setName("IH"); synapse_HO.setName("HO"); input.addOutputSynapse(synapse_IH); // Connect the input layer whit the hidden layer hidden.addInputSynapse(synapse_IH); hidden.addOutputSynapse(synapse_HO);// Connect the hidden layer whit the output layer output.addInputSynapse(synapse_HO); FileInputSynapse inputStream = new FileInputSynapse(); //USE WITH FILE INPUT inputStream.setFirstCol(1); inputStream.setLastCol(2); inputStream.setFileName("xor.txt"); input.addInputSynapse(inputStream); TeachingSynapse trainer = new TeachingSynapse(); FileInputSynapse samples = new FileInputSynapse(); samples.setFileName("xor.txt"); trainer.setDesired(samples); samples.setFirstCol(3); samples.setLastCol(3); // Connects the Teacher to the last layer of the net output.addOutputSynapse(trainer); memOut = new MemoryOutputSynapse(); //OUTPUT TO ARRAY STUFF memOut.setBuffered(false); output.addOutputSynapse(memOut); NeuralNet nnet = new NeuralNet(); /*Use the NeuralNet object*/ nnet.addLayer(input, NeuralNet.INPUT_LAYER); nnet.addLayer(hidden, NeuralNet.HIDDEN_LAYER); nnet.addLayer(output, NeuralNet.OUTPUT_LAYER); nnet.setTeacher(trainer); Monitor monitor = nnet.getMonitor(); monitor.setLearningRate(0.8); monitor.setMomentum(0.3); monitor.setTrainingPatterns(4); monitor.setTotCicles(20000); monitor.setLearning(true); monitor.addNeuralNetListener(this); nnet.start(); nnet.getMonitor().Go(); } /** * Called after training. */ public void netStopped(NeuralNetEvent e) { System.out.println("Training finished"); netFinished(); //Go to the method that deals with all post-training stuff } /** * Called after each cycle. */ public void cicleTerminated(NeuralNetEvent e) { Monitor mon = (Monitor) e.getSource(); long c = mon.getCurrentCicle(); long cl = c / 1000; if ((cl * 1000) == c) { // Print the results every 1000 cycles System.out.println(c + " cycles remaining - Error = " + mon.getGlobalError()); } } /** * Called before training starts. */ public void netStarted(NeuralNetEvent e) {System.out.println("Training...");} public void errorChanged(NeuralNetEvent e) {} public void netStoppedError(NeuralNetEvent e,String error) { } public void netFinished(){ System.out.println("\nPrinting output results"); for(int i=0; i < 4; ++i){ double[] pattern = memOut.getNextPattern(); System.out.println("Output Pattern #" + (i+1) + " = " + pattern[0]); } System.exit(0); } }