⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 monitor.java

📁 一个纯java写的神经网络源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package org.joone.engine;import org.joone.net.NetCheck;import org.joone.net.NeuralNet;import java.io.*;import java.util.*;import org.joone.log.*;/** * The Monitor object is the controller of the behavior of the neural net. * It controls the start/stop actions and permits to set the parameters of the net * (learning rate, momentum, ecc.). * Each component of the neural net (Layers and Synapses) are connected to a monitor object * (the monitor can be different or the same for the all components). */public class Monitor implements Serializable {    private static final long serialVersionUID = 2909501813894146845L;        private int preLearning = 0;    private boolean learning = false;    private int currentCicle;    private int run = 0;    private int saveCurrentCicle;    private int saveRun;            // Starting parameters    private int patterns;  // Training patterns    private int validationPatterns; // Validation patterns    private int totCicles;    private double learningRate;    private double momentum;    private double globalError;    private int batchSize = 0;        /** Use RMSE (if true) for back propagation, MSE (if false) otherwise. */    private boolean useRMSE = true;        /** The learner factory. If set this factory provides synapses and layers     * with learners. */    private LearnerFactory theLearnerFactory = null;        /**     * @label parent     */    private Monitor parent;        /* No removable listeners. They cannot be removed on the removeAllListeners call.     * This is useful to avoid that permanent internal listeners are removed     * when the neural network is cloned.     */    private transient Vector internalListeners = new Vector();        private transient Vector netListeners = new Vector();    private transient boolean firstTime = true;    private transient boolean exporting = false;    private transient boolean validation = false;    private transient boolean running = false;        /** The next flag indicates if training data should be used for validation (true), or not (false).     * The default is false. */    private transient boolean trainingDataForValidation = false;        private static final ILogger log = LoggerFactory.getLogger( Monitor.class );        private boolean supervisioned = false;    private boolean singleThreadMode = true;        public int learningMode = 0;        private List learners; // Container of the available Learners    private Hashtable params;            /**     * This is the default Constructor.     */    public Monitor() {        firstTime = true;        netListeners = new Vector();        internalListeners = new Vector();        parent = null;    }        /**     * adds a neural net event listener the Monitor     * @param l NeuralNetListener     */    public void addNeuralNetListener(NeuralNetListener l) {        this.addNeuralNetListener(l, true);    }        /** adds a neural net event listener to the Monitor     * @param l the new NeuralNetListener     * @param removable true if the added listener can be removed by the removeAllListeners method call     */    public synchronized void addNeuralNetListener(NeuralNetListener l, boolean removable) {        if (parent != null)            parent.addNeuralNetListener(l, removable);        else {            if (!getListeners().contains(l)) {                netListeners.addElement(l);                if (!removable) {                    if (!getNoDetachListeners().contains(l))                        getNoDetachListeners().addElement(l);                }            }        }    }        private Vector getNoDetachListeners() {        if (internalListeners == null)            internalListeners = new Vector();        return internalListeners;    }        private Vector getListeners() {        if (netListeners == null)            netListeners = new Vector();        return netListeners;    }        /** Invoked when an epoch finishes     */    public void fireCicleTerminated() {        if (parent != null)            parent.fireCicleTerminated();        else {            int size = getListeners().size();            if (size == 0)                return;            Object[] list;            synchronized (this) {                list = getListeners().toArray();            }            NeuralNetEvent event = new NeuralNetEvent(this);            for (int i = 0; i < size; ++i) {                NeuralNetListener listener = (NeuralNetListener) list[i];                listener.cicleTerminated(event);            }        }    }        /** Invoked when the net starts     */    public void fireNetStarted() {        if (parent != null)            parent.fireNetStarted();        else {            int size = getListeners().size();                        if (size == 0)                return;                        Object[] list;            synchronized (this) {                list = getListeners().toArray();            }            NeuralNetEvent event = new NeuralNetEvent(this);            for (int i = 0; i < list.length; ++i) {                NeuralNetListener listener = (NeuralNetListener) list[i];                listener.netStarted(event);            }        }    }        /** Invoked when all the epochs finish     */    public void fireNetStopped() {        if (parent != null)            parent.fireNetStopped();        else {            int size = getListeners().size();            if (size == 0)                return;                        Object[] list;            synchronized (this) {                list = getListeners().toArray();            }            NeuralNetEvent event = new NeuralNetEvent(this);                        for (int i = 0; i < list.length; ++i) {                NeuralNetListener listener = (NeuralNetListener) list[i];                listener.netStopped(event);            }        }    }        /** Invoked when an error occurs     * @param errMsg the thrown error message     */    public void fireNetStoppedError(String errMsg) {        if (parent != null)            parent.fireNetStoppedError(errMsg);        else {            int size = getListeners().size();                        if (size == 0)                return;                        Object[] list;            synchronized (this) {                list = getListeners().toArray();            }            NeuralNetEvent event = new NeuralNetEvent(this);                        for (int i = 0; i < list.length; ++i) {                NeuralNetListener listener = (NeuralNetListener) list[i];                listener.netStoppedError(event,errMsg);            }            if (running) {                log.error("Neural net stopped due to the following error: "+errMsg);                log.debug("\tepoch:"+currentCicle);                log.debug("\tcycle:"+run);                log.debug("\tlearning:"+isLearning());                log.debug("\tvalidation:"+isValidation());                log.debug("\ttrainingPatterns:"+getTrainingPatterns());                log.debug("\tvalidationPatterns:"+getValidationPatterns());            }        }    }        /** Invoked when the GlobalError changes     */    public void fireErrorChanged() {        if (parent != null)            parent.fireErrorChanged();        else {            int size = getListeners().size();                        if (size == 0)                return;                        Object[] list;            synchronized (this) {                list = getListeners().toArray();            }            NeuralNetEvent event = new NeuralNetEvent(this);                        for (int i = 0; i < list.length; ++i) {                NeuralNetListener listener = (NeuralNetListener) list[i];                listener.errorChanged(event);            }        }    }        /** Returns the current epoch     * @return int     */    public synchronized int getCurrentCicle() {        if (parent != null)            return parent.getCurrentCicle();        else            return currentCicle;    }        /** Returns the actual (R)MSE of the NN     * @return double     */    public double getGlobalError() {        if (parent != null)            return parent.getGlobalError();        else            return globalError;    }        /** Returns the learning rate     * @return double     */    public synchronized double getLearningRate() {        if (parent != null)            return parent.getLearningRate();        else            return learningRate;    }        /** Returns the momentum     * @return double     */    public double getMomentum() {        if (parent != null)            return parent.getMomentum();        else            return momentum;    }        /** Returns the number of the input training patterns     * @return int     */    public int getTrainingPatterns() {        if (parent != null)            return parent.getTrainingPatterns();        else            return patterns;    }        /** Sets the number of the input training patterns     * @param newPatterns int     */    public void setTrainingPatterns(int newPatterns) {        if (parent != null)            parent.setTrainingPatterns(newPatterns);        else            patterns = newPatterns;    }        /** Returns the initial ignored input patterns (during the training phase)     * @return int     */    public int getPreLearning() {        if (parent != null)            return parent.getPreLearning();        else            return preLearning;    }        /** Returns the actual elaborated pattern     * @return int     */    public synchronized int getStep() {        if (parent != null)            return parent.getStep();        else            return run;    }        /** Returns the total number of epochs     * @return int     */    public int getTotCicles() {        if (parent != null)            return parent.getTotCicles();        else            return totCicles;    }            /**     * Runs the neural net in multi-thread mode.     * WARNING: AVOID to invoke this method. Use instead NeuralNet.go()     *     * @see org.joone.net.NeuralNet.go()     */    public synchronized void Go() {        if (parent != null)            parent.Go();        else {            setSingleThreadMode(false);            run = getNumOfPatterns();            currentCicle = totCicles;            firstTime = false;            running = true;            notifyAll();        }    }        /**     * Returns TRUE if the net is into a learning phase     * @return boolean     */    public boolean isLearning() {        return learning;    }        /** Returns the phase of the net (learning or not) for the current pattern     * @return boolean     * @param num the pattern requested     */    public boolean isLearningCicle(int num) {        if (parent != null) {            boolean learn = parent.isLearningCicle(num);            return (learn & isLearning());        } else            if (num > preLearning)                return isLearning();            else                return false;    }        public synchronized void resetCycle()  {        run = 0;    }            /** Returns if the next pattern must be elaborated     * @return boolean     */    public synchronized boolean nextStep() {        if (parent != null)            return parent.nextStep();        else {            while (run == 0) {                try {                    if (!firstTime) {                        if (currentCicle > 0) {                            fireCicleTerminated();                            --currentCicle;                            if(currentCicle < 0) {                                // currentCicle might be smaller than 0 here if someone                                // calls Monitor.Stop in a cicleTerminated() method (which                                // is called by the fireCicleTerminated() method)                                currentCicle = 0;                            }                            run = getNumOfPatterns();                        }                        if (currentCicle == 0) {                            running = false;                            if (!this.isSupervised() || (!this.isLearning() && !this.isValidation()))                                new NetStoppedEventNotifier(this).start();                            if (saveRun == 0) {                                saveRun = getNumOfPatterns();                                saveCurrentCicle = totCicles;                            }                            run = 0;                            firstTime = true;                            return false;                            //wait();                        }                    } else                    /* If it goes here, it means that this method                     * has been called first to call Go() or runAgain() */                        wait();                } catch (InterruptedException e) {                    //e.printStackTrace();                    run = 0;                    firstTime = true;                    return false;                }            }            if ((run == getNumOfPatterns()) && (currentCicle == totCicles))                fireNetStarted();            if (run > 0)                --run;            return true;        }    }        protected Object readResolve() {        firstTime = true;        return this;    }        /** Removes a listener     * @param l the listener to be removed     */    public synchronized void removeNeuralNetListener(NeuralNetListener l) {        if (parent != null)            parent.removeNeuralNetListener(l);        else {            getListeners().removeElement(l);            getNoDetachListeners().removeElement(l);        }    }        /**     * Let continue the net.     */    public synchronized void runAgain() {        if (parent != null)            parent.runAgain();        else {            run = getNumOfPatterns(); // old: run = saveRun;            currentCicle = saveCurrentCicle;            firstTime = false;            running = true;            notifyAll();        }    }        /** Not used     * @param newCurrentCicle int     */    public void setCurrentCicle(int newCurrentCicle) {        if (parent != null)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -