📄 learningthread.java
字号:
//////////////////////////////////////////////////////////////////////////////////// // // Copyright (C) 1996 L. Patocchi & W.Gander//// This program is free software; you can redistribute it and/or modify it// under the terms of the GNU General Public License as published by the Free// Software Foundation; either version 2 of the License, or (at your option)// any later version.//// This program is distributed in the hope that it will be useful, but WITHOUT// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for// more details.//// You should have received a copy of the GNU General Public License along with// this program; if not, write to the Free Software Foundation, Inc., 675 Mass// Ave, Cambridge, MA 02139, USA.//// Contacts:// // Project Supervisor// W.Hett hew@info.isbiel.ch// // Authors// W.Gander gandw@info.isbiel.ch// L.Patocchi patol@info.isbiel.ch//// Documentation can be found at://// http://www.isbiel.ch/Projects/janet/index.html////////////////////////////////////////////////////////////////////////////////////////// File : learningThread.java//package jaNet.backprop.gui;import jaNet.backprop.*;public class learningThread extends Thread{ public static final int MAXS = BPNgui.MAXS; public static final int MINE = BPNgui.MINE; public static final int BOTH = BPNgui.BOTH; public static final int SAVE_LAST = BPNgui.SAVE_LAST; public static final int SAVE_BEST = BPNgui.SAVE_BEST; public static final int CHOSE_RAND = BPNgui.CHOSE_RAND; public static final int CHOSE_SEQU = BPNgui.CHOSE_SEQU; public static final double BIG = 1000000; // swap 'shakenes' * '# of patterns' times when in CHOSE_RAND; public double shakenes = 1.0; private BPN bpn; private learningSet lSet; private int stopMode; private int stopMaxS; private double stopMinE; private int saveMode; private int choseMode; private boolean exitNow; private int showDataFrequence; private int showGraphFrequence; private int sleepFrequence = 2000; private BPNgui dearParent; public learningThread(BPNgui parent, BPN bpn, learningSet lSet, int stopMode, int stopMaxS, double stopMinE, int saveMode, int patternChoseMode, int showDataFrequence, int showGraphFrequence) throws BPNException{ super("BPNgui teacher"); if(parent == null) throw new BPNException("learningThread: parent is null."); if(bpn == null) throw new BPNException("no BPN NN to train."); if(lSet == null) throw new BPNException("no learning sets."); dearParent = parent; this.bpn = bpn; this.lSet = lSet; this.stopMode = stopMode; this.stopMaxS = stopMaxS; this.stopMinE = stopMinE; this.saveMode = saveMode; this.choseMode = patternChoseMode; this.showDataFrequence = showDataFrequence; this.showGraphFrequence = showGraphFrequence; exitNow = false; } private synchronized boolean canExit(int count, double error){ return !(exitNow || ((stopMode==MAXS && count>stopMaxS) || (stopMode==MINE && error<stopMinE) || (stopMode==BOTH && (count>stopMaxS || error<stopMinE)))); } public synchronized void stopNow(){ exitNow = true; } public void run(){ int nPatterns = lSet.getSize(); double globalError = BIG; double lowerError = BIG; double thisError; int count = 0; int patternRef; int pattern; // actually set the choosing pattern sequence to CHOSE_SEQU int patternSequence[] = new int[nPatterns]; for(int i=0; i<nPatterns; i++) patternSequence[i] = i; try{ // begin learning session while(canExit(count, globalError)){ patternRef = count%nPatterns; // if CHOSE_RAND then shake up a bit pattern sequence if(patternRef==0 && choseMode==CHOSE_RAND && nPatterns>1){ for(int j=0; j<((int)((double)nPatterns*shakenes)); j++){ // swap two elements at random int p1 = (int)(Math.random()*(double)nPatterns); int p2 = (int)(Math.random()*(double)nPatterns); int tmp = patternSequence[p1]; patternSequence[p1] = patternSequence[p2]; patternSequence[p2] = tmp; } } // giva hopefully an occasion to garbage collect a bit if(count%sleepFrequence == 0){ try {Thread.sleep(500);} catch (InterruptedException e){}; } // gets real pattern number pattern = patternSequence[patternRef]; // learn this pattern bpn.learn(lSet.inputs[pattern].vector, lSet.targets[pattern].vector); // calculate global error thisError = bpn.getError(); globalError = 0.0; for(int i=0; i<nPatterns; i++) globalError += bpn.getError(lSet.inputs[i].vector,lSet.targets[i].vector); if(globalError < lowerError){ lowerError = globalError; dearParent.setLowerGlobalError(lowerError); // if SAVE_BEST mode make a copy of weights in a temp buffer if(saveMode == SAVE_BEST){ bpn.setWeightCopy(); } } // displays data every showDataFrequence iterations; if(showDataFrequence != 0) if(count%showDataFrequence == 0){ dearParent.setCurrentSession(count); dearParent.setCurrentError(globalError); } if(showGraphFrequence != 0) if(count%showGraphFrequence == 0){ dearParent.showPattern(); } count ++; // yield yield(); } }catch(BPNException bpne){ dearParent.todo = ""+bpne; } dearParent.setCurrentSession(count-1); dearParent.setCurrentError(globalError); dearParent.selectedStopLearning(); if(saveMode == SAVE_BEST) bpn.setWeightCopy(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -