📄 neuralnet.java
字号:
macroPlugin.set("jNet", this); macroPlugin.set("jMon", getMonitor()); } } /** Gets a custom parameter from the neural net. * The user is free to use the custom parameters as s/he wants. * They are useful to transport a whatever value along with the net. * @param key The searched key * @return The value of the parameter if found, otherwise null */ public Object getParam(String key) { if (params == null) return null; return params.get(key); } /** Sets a custom parameter of the neural net. * The user is free to use the custom parameters as s/he wants. * They are useful to transport a whatever value along with the net. * @param key The key of the param * @param obj The value of the param */ public void setParam(String key, Object obj) { if (params == null) params = new Hashtable(); if (params.containsKey(key)) params.remove(key); params.put(key, obj); } /** Return all the keys of the parameters contained in the net. * @return An array of Strings containing all the keys if found, otherwise null */ public String[] getKeys() { if (params == null) return null; String[] keys = new String[params.keySet().size()]; Enumeration myEnum = params.keys(); for (int i=0; myEnum.hasMoreElements(); ++i) { keys[i] = (String)myEnum.nextElement(); } return keys; } /** * Compiles all layers' check messages. * * @see NeuralLayer * @return validation errors. */ public TreeSet check() { // Prepare an empty set for check messages; TreeSet checks = new TreeSet(); // Check for an empty neural network if ((layers == null) || layers.isEmpty()) { checks.add(new NetCheck(NetCheck.FATAL, "The Neural Network doesn't contain any layer", mon)); // If empty it makes no sense to continue the check return checks; } else // Check for the presence of more than one InputSynpase having stepCounter set to true if (getNumOfstepCounters() > 1) checks.add(new NetCheck(NetCheck.FATAL, "More than one InputSynapse having stepCounter set to true is present", mon)); // Check all layers. for (int i = 0; i < layers.size(); i++) { Layer layer = (Layer) layers.elementAt(i); checks.addAll(layer.check()); } // Check the teacher (only if it exists and this is not a nested neural network) if (mon.getParent() == null) { if (teacher != null) { checks.addAll(teacher.check()); if (mon != null && mon.isLearning() && !mon.isSupervised()) checks.add(new NetCheck(NetCheck.WARNING, "Teacher is present: the supervised property should be set to true", mon)); } else { if (mon != null && mon.isLearning() && mon.isSupervised()) checks.add(new NetCheck(NetCheck.FATAL, "Teacher not present: set to false the supervised property", mon)); } } // Check the Monitor. if (mon != null) { checks.addAll(mon.check()); } // Return check messages. return checks; } // NET LISTENER METHODS /** * Not implemented. * @param e */ public void netStarted(NeuralNetEvent e) { } /** * Not implemented. * @param e */ public void cicleTerminated(NeuralNetEvent e) { } /** * Not implemented. * @param e */ public void netStopped(NeuralNetEvent e) { } /** * Not implemented. * @param e */ public void errorChanged(NeuralNetEvent e) { } /** * Stops the execution threads and resets all the layers * in the event of an crtitical network error. * @param error The error message. * @param e The event source of this event. */ public void netStoppedError(NeuralNetEvent e, String error) { // Stop and reset all the Layers. this.terminate(false); } /** * This method permits to set externally a particular order to * traverse the Layers. If not used, the order will be calculated * automatically. Use this method in cases where the automatic * ordering doesn't work (e.g. in case of complex recurrent connections) * NOTE: if you set this property, you're responsible to update the array * whenever a layer is added/removed. * @param orderedLayers an array containing the ordered layers */ public void setOrderedLayers(Layer[] orderedLayers) { this.orderedLayers = orderedLayers; } public Layer[] getOrderedLayers() { return orderedLayers; } /** * This method calculates the order of the layers of the network, from the input to the output. * If the setOrderedLayers method has been invoked before, that array will be returned, otherwise * the order will be calculated automatically. * @return An array containing the ordered Layers, from the input to the output (i.e. layers[0]=input layer, layers[n-1]=output layer. */ public Layer[] calculateOrderedLayers() { if (getOrderedLayers() == null) { if (intOrderedLayers == null) { NeuralNetMatrix matrix = new NeuralNetMatrix(this); intOrderedLayers = matrix.getOrderedLayers(); } } else { intOrderedLayers = getOrderedLayers(); } return intOrderedLayers; } /** Runs the network. * @param singleThreadMode If true, runs the network in single thread mode * @param sync If true, runs the network in a separated thread and returns immediately. */ public void go(boolean singleThreadMode, boolean sync) { getMonitor().setSingleThreadMode(singleThreadMode); this.go(sync); } private transient Thread singleThread = null; /** Runs the network. The running mode is determined by * the value of the singleThreadMode property. * @param sync If true, runs the network in a separated thread and returns immediately. */ public void go(boolean sync) { if (getMonitor().isSingleThreadMode()) { Runnable runner = new Runnable() { public void run() { fastRun(); } }; setSingleThread(new Thread(runner)); getSingleThread().start(); } else { this.start(); getMonitor().Go(); } // If launched in synch mode, waits for the thread completition if (sync) { this.join(); } } /** Runs the network in async mode (i.e. equivalent to go(false) ). * The running mode is determined by the value of the singleThreadMode property. */ public void go() { this.go(false); } /** Continue the execution of the network after the stop() method is called. */ public void restore() { if (getMonitor().isSingleThreadMode()) { Runnable runner = new Runnable() { public void run() { fastContinue(); } }; setSingleThread(new Thread(runner)); getSingleThread().start(); } else { this.start(); getMonitor().runAgain(); } } /********************************************************* * Implementation code for the single-thread version of Joone *********************************************************/ private boolean stopFastRun; /* This method runs the neural network in single-thread mode. */ protected void fastRun() { this.fastRun(getMonitor().getTotCicles()); } /* This method restore the running of the neural network * in single-thread mode, starting from the epoch at which * it had been previously stopped. */ protected void fastContinue() { this.fastRun(getMonitor().getCurrentCicle()); } /* This method runs the neural network in single-thread mode * starting from the epoch passed as parameter. * NOTE: the network will count-down from firstEpoch to 0. * @param firstEpoch the epoch from which the network will start. */ protected void fastRun(int firstEpoch) { Monitor mon = getMonitor(); mon.setSingleThreadMode(true); int epochs = firstEpoch; int patterns = mon.getNumOfPatterns(); Layer[] ordLayers = calculateOrderedLayers(); int layers = ordLayers.length; // Calls the init method for all the Layers for (int ly=0; ly < layers; ++ly) { ordLayers[ly].init(); } stopFastRun = false; mon.fireNetStarted(); for (int epoch=epochs; epoch > 0 ; --epoch) { mon.setCurrentCicle(epoch); for (int p=0; p < patterns; ++p) { // Forward stepForward(null); if (getMonitor().isLearningCicle(p+1)) { // Backward stepBackward(null); } } mon.fireCicleTerminated(); if (stopFastRun) { break; } } Pattern stop = new Pattern(new double[ordLayers[0].getRows()]); stop.setCount(-1); stepForward(stop); mon.fireNetStopped(); } /* Use this method to perform a single step forward. * The network is interrogated using the next available * input pattern (only one). * @param pattern The input pattern to use. If null, the input pattern is read from the input synapse connected to the input layer. */ protected void singleStepForward(Pattern pattern) { getMonitor().setSingleThreadMode(true); Layer[] ordLayers = calculateOrderedLayers(); int layers = ordLayers.length; // Calls the init method for all the layers for (int ly=0; ly < layers; ++ly) { ordLayers[ly].init(); } this.stepForward(pattern); } /* Use this method to perform a single step backward. * The pattern passed as parameter, that is backpropagated, must contain * the error in terms of differences from the desired pattern. * @param pattern The error pattern to backpropagate. If null, the pattern is read from the teacher connected to the output layer. */ protected void singleStepBackward(Pattern error) { getMonitor().setSingleThreadMode(true); this.stepBackward(error); } protected void stepForward(Pattern pattern) { Layer[] ordLayers = calculateOrderedLayers(); int layers = ordLayers.length; ordLayers[0].fwdRun(pattern); for (int ly=1; ly < layers; ++ly) { ordLayers[ly].fwdRun(null); } } protected void stepBackward(Pattern error) { Layer[] ordLayers = calculateOrderedLayers(); int layers = ordLayers.length; for (int ly=layers; ly > 0; --ly) { ordLayers[ly-1].revRun(error); } } /* This method serves to stop the network * when running in single-thread mode. * It DOES NOT affect the multi-thread running * (i.e. a network launched with Monitor.Go() method. */ protected void stopFastRun() { stopFastRun = true; } protected Thread getSingleThread() { return singleThread; } protected void setSingleThread(Thread singleThread) { this.singleThread = singleThread; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -