📄 neuralnet.java
字号:
/* * NeuralNet.java * * Created on 17 april 2001, 12.08 */package org.joone.net;import java.util.*;import java.io.*;import org.joone.helpers.structure.ConnectionHelper;import org.joone.helpers.structure.NeuralNetMatrix;import org.joone.log.*;import org.joone.engine.*;import org.joone.engine.learning.*;import org.joone.io.*;import org.joone.script.MacroInterface;import org.joone.exception.JooneRuntimeException;/** This object represents a container of a neural network, * giving to the developer the possibility to manage a * neural network as a whole. * Thanks to it, a neural network can be saved and restored * using an unique writeObject and readObject command, without * be worried about its internal composition. * Not only this, because using a NeuralNet object, we can * also easily transport a neural network on remote machines * and runnit there, writing only few and generalized java code. * */public class NeuralNet implements NeuralLayer, NeuralNetListener, Serializable { private static final int MAJOR_RELEASE = 2; private static final int MINOR_RELEASE = 0; private static final int BUILD = 0; private static final String SUFFIX = "RC1"; private static final ILogger log = LoggerFactory.getLogger( NeuralNet.class ); private Vector layers; private String NetName; private Monitor mon; private Layer inputLayer; private Layer outputLayer; private ComparingElement teacher; private static final long serialVersionUID = 8351124226081783962L; public static final int INPUT_LAYER = 0; public static final int HIDDEN_LAYER = 1; public static final int OUTPUT_LAYER = 2; protected Vector listeners; private MacroInterface macroPlugin; private boolean scriptingEnabled = false; private NeuralNetAttributes descriptor = null; private Hashtable params; private Layer[] orderedLayers = null; private transient Layer[] intOrderedLayers = null; /** Creates new NeuralNet */ public NeuralNet() { layers = new Vector(); // Creates a monitor with a back-reference to this neural-net mon = new Monitor(); } /** * Starts all the Layers' threads, in order * to prepare the launch of the neural network * in multi-thread mode. * DO NOT use for single-thread mode. */ public void start() { this.terminate(false); if (readyToStart()) { getMonitor().addNeuralNetListener(this, false); Layer ly = null; int i; try { for (i=0; i < layers.size(); ++i) { ly = (Layer)layers.elementAt(i); ly.start(); } } catch (RuntimeException rte) { // If some layer can't start, resets the state of the NN this.stop(); String msg; log.error(msg = "RuntimeException was thrown while starting the neural network. Message is:" + rte.getMessage(), rte); throw new JooneRuntimeException(msg, rte); } } else { String msg; log.warn(msg = "NeuralNet: The neural net is already running"); throw new JooneRuntimeException(msg); } } /** * Check if the neural net is ready to be started again * @return true only if there isn't any running layer */ private boolean readyToStart() { for (int i=0; i < 100; ++i) { if (!this.isRunning()) return true; else try { // waits for 10 millisecs Thread.sleep(10); } catch (InterruptedException e) { return false; } } return false; } /** Waits for all the termination of all running Threads * @see Thread.join() */ public void join() { if (getMonitor().isSingleThreadMode()) { if (getSingleThread() != null) { try { getSingleThread().join(); } catch (InterruptedException doNothing) { } } } else { for (int i=0; i < layers.size(); ++i) { Layer ly = (Layer)layers.elementAt(i); ly.join(); } if (teacher != null) teacher.getTheLinearLayer().join(); } } /** Terminates the execution of this NeuralNet * independently from the threading mode activated. */ public void stop() { if (getMonitor().isSingleThreadMode()) { this.stopFastRun(); } else { getMonitor().Stop(); } } /** * Terminates the execution of all the threads * of the neural network. * Used to force a neural network independently * from its internal state. * Use ONLY in multi-thread mode, and ONLY when * the call to the stop() method doesn't give * the expected results. * @param notify if true, the netStopped event is raised */ public void terminate(boolean notify) { if (isRunning()) { Layer ly = null; int i; for (i=0; i < layers.size(); ++i) { ly = (Layer)layers.elementAt(i); ly.stop(); } if (teacher != null) { teacher.getTheLinearLayer().stop(); if (teacher instanceof AbstractTeacherSynapse) { ((AbstractTeacherSynapse)teacher).netStoppedError(); } } if ((getMonitor() != null) && (notify)) new NetStoppedEventNotifier(getMonitor()).start(); } } /** * Terminates the execution of all the threads * of the neural network. * @see this.terminate(boolean notify) */ public void terminate() { this.terminate(true); } protected int getNumOfstepCounters() { int count = 0; for (int i=0; i < layers.size(); ++i) { Layer ly = (Layer)layers.elementAt(i); if (ly.hasStepCounter()) ++count; } if (teacher != null) { if ((teacher.getDesired() != null) && (teacher.getDesired().isStepCounter())) ++count; } return count; } /** Returns the input layer of the network. * If the method setInputLayer has been never invoked, the * input layer is found, set and returned. */ public Layer getInputLayer() { if (inputLayer != null) return inputLayer; setInputLayer(findInputLayer()); return inputLayer; } /** Returns the input layer, by searching for it following * the rules written in Layer.isInputLayer. Ignores any * previous call made to setInputLayer. */ public Layer findInputLayer() { Layer input = null; if (layers == null) return null; for (int i=0; i < layers.size(); ++i) { Layer ly = (Layer)layers.elementAt(i); if (ly.isInputLayer()) { input = ly; break; } } return input; } /** Returns the output layer of the network. * If the method setOutputLayer has been never invoked, the * output layer is found, set and returned. */ public Layer getOutputLayer() { if (outputLayer != null) return outputLayer; setOutputLayer(findOutputLayer()); return outputLayer; } /** Returns the output layer by searching for it following * the rules written in Layer.isOutputLayer. Ignores any * previous call made to setOutputLayer. */ public Layer findOutputLayer() { Layer output = null; if (layers == null) return null; for (int i=0; i < layers.size(); ++i) { Layer ly = (Layer)layers.elementAt(i); if (ly.isOutputLayer()) { output = ly; break; } } return output; } public int getRows() { Layer ly = this.getInputLayer(); if (ly != null) return ly.getRows(); else return 0; } public void setRows(int p1) { Layer ly = this.getInputLayer(); if (ly != null) ly.setRows(p1); } public void addNoise(double p1) { Layer ly; int i; for (i=0; i < layers.size(); ++i) { ly = (Layer)layers.elementAt(i); ly.addNoise(p1); } } public void randomize(double amplitude) { Layer ly; int i; for (i=0; i < layers.size(); ++i) { ly = (Layer)layers.elementAt(i); ly.randomize(amplitude); } } public Matrix getBias() { Layer ly = this.getInputLayer(); if (ly != null) return ly.getBias(); else return null; } public Vector getAllOutputs() { Layer ly = this.getOutputLayer(); if (ly != null) return ly.getAllOutputs(); else return null; } public String getLayerName() { return NetName; } public void removeOutputSynapse(OutputPatternListener p1) { Layer ly = this.getOutputLayer(); if (ly != null) ly.removeOutputSynapse(p1); } public void setAllInputs(Vector p1) { Layer ly = this.getInputLayer(); if (ly != null) ly.setAllInputs(p1); } public void removeAllOutputs() { Layer ly = this.getOutputLayer(); if (ly != null) ly.removeAllOutputs(); setTeacher(null); } public Vector getAllInputs() { Layer ly = this.getInputLayer(); if (ly != null) return ly.getAllInputs(); else return null; } public boolean addOutputSynapse(OutputPatternListener p1) { Layer ly = this.getOutputLayer(); if (ly != null) return ly.addOutputSynapse(p1); else return false; } public void setBias(Matrix p1) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -