📄 immediateembeddedxor.java
字号:
/* * EmbeddedXOR.java * * Created on 7 maggio 2002, 19.27 */package org.joone.samples.engine.xor;import org.joone.log.*;import org.joone.engine.*;import org.joone.net.NeuralNet;import java.io.*;/** * This example shows the use of a neural network embedded in another * application that gets the output from the DirectSynapse object * querying the neural network with one input pattern at time * * @author pmarrone */public class ImmediateEmbeddedXOR { /** * Logger * */ private static final ILogger log = LoggerFactory.getLogger(ImmediateEmbeddedXOR.class); private double[][] inputArray = { {0, 0}, {0, 1}, {1, 0}, {1, 1} }; /** Creates a new instance of EmbeddedXOR */ public ImmediateEmbeddedXOR() { } /** * @param args the command line arguments */ public static void main(String[] args) { if (args.length < 1) { System.out.println("Usage: java org.joone.samples.xor.EmbeddedXOR <neuralNet>"); System.out.println("where <neuralNet> is the file name (with its complete path) that contains the serialized XOR.snet"); } else { ImmediateEmbeddedXOR xor = new ImmediateEmbeddedXOR(); xor.Go(args[0]); } } private void Go(String fileName) { // We load the serialized XOR neural net NeuralNet xor = restoreNeuralNet(fileName); if (xor != null) { /* We get the first layer of the net (the input layer), then remove all the input synapses attached to it and attach a DirectSynapse */ Layer input = xor.getInputLayer(); input.removeAllInputs(); DirectSynapse memInp = new DirectSynapse(); input.addInputSynapse(memInp); /* We get the last layer of the net (the output layer), then remove all the output synapses attached to it and attach a DirectSynapse */ Layer output = xor.getOutputLayer(); output.removeAllOutputs(); DirectSynapse memOut = new DirectSynapse(); output.addOutputSynapse(memOut); // Now we interrogate the net xor.getMonitor().setLearning(false); xor.start(); log.debug("Threads running: "+xor.isRunning()); /* Question: where is the following code? * xor.getMonitor().setTotCicles(1); * xor.getMonitor().setPatterns(4); * xor.getMonitor().Go(); * -------------------------------------- * As the external application controls each run of the NN, * we don't need to invoke the above methods! */ for (int n=0; n < 100; ++n) { log.debug("Cycle #"+n); for (int i=0; i < 4; ++i) { // Prepare the next input pattern Pattern iPattern = new Pattern(inputArray[i]); iPattern.setCount(i+1); // Interrogate the net memInp.fwdPut(iPattern); // Read the output pattern and print out it //double[] pattern = memOut.getNextPattern(); Pattern pattern = memOut.fwdGet(); log.debug("Output Pattern #"+(i+1)+" = "+pattern.getArray()[0]); } } Pattern stop = new Pattern(new double[2]); stop.setCount(-1); memInp.fwdPut(stop); memOut.fwdGet(); log.debug("Threads running: "+xor.isRunning()); log.debug("Finished"); } } // // TO DO // add some comments private NeuralNet restoreNeuralNet(String fileName) { NeuralNet nnet = null; try { FileInputStream stream = new FileInputStream(fileName); ObjectInput input = new ObjectInputStream(stream); nnet = (NeuralNet)input.readObject(); } catch (Exception e) { log.warn( "Exception thrown while restoring the Neural Net. Message is : " + e.getMessage(), e ); } return nnet; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -