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

📄 bpngui.java

📁 JaNet: Java Neural Network Toolkit resume: A well documented toolkit for designing and training, a
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
////////////////////////////////////////////////////////////////////////////////////  //  //  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 : BPNgui.java////////////////////////////////////////////////////////////////////
//
// 		BPNgui base class
//
//////////////////////////////////////////////////////////////////
//
//      Author:  Patocchi L.
//      Date:    10.09.1996
//      Project: jaNet
//
//		BPN graphical user interface
//       
//////////////////////////////////////////////////////////////////
//
//      date            who                                     what
//  10.09.1996  Patocchi L.                     creation
package jaNet.backprop.gui;

import java.awt.*;
import jaNet.jaNet;
import jaNet.NNeMo;
import jaNet.ModuleEntry;
import jaNet.backprop.*;

public class BPNgui extends Frame implements NNeMo,Runnable{

    Menu menuFile;
    Menu menuNeural;
    Menu menuDisplay;

	public static final String	NONE = "NONE";

	public static final String	entryPatternRefHelp = "Chose the pattern number. Shortcuts: [UP] previous pattern, [DOWN] next pattern";
	public static final String	textMonitorHelp = "Shows most important events.";
	public static final String	buttonInitNNHelp = "Sets wieghts to random values.";
	public static final String	buttonStartStopHelp = "Starts and stops the learning.";
	public static final String	buttonStepByHelp = "Learn network for more sessions.";
	public static final String	valuesScaleHelp = "Color scale reference.";
	
	// stop learning modes
	public static final int		MAXS = 0;
	public static final int		MINE = 1;
	public static final int		BOTH = 2;
	// saving BPN way when learning mode
	public static final int		SAVE_LAST = 0;
	public static final int		SAVE_BEST = 1;
	// chosing patterns way when learning mode
	public static final int		CHOSE_RAND = 0;
	public static final int		CHOSE_SEQU = 1;

    private stopRules           BPNstopRules;
    private networkStructure    BPNnetworkStructure;
    private errorGraph          BPNerrorGraph;
    private networkParameters	BPNnetworkParameters;
    private errorDialog 		BPNerrorDialog;
	private layerSettings 		BPNlayerSettings;
	private displayRules 		BPNdisplayRules;

    public BPNdraw 				canvasNeuralNet;
    public TextArea 			textMonitor;
    public Button 				buttonInitNN;
    public Button 				buttonStartStop;
    public Button 				buttonStepBy;
    public TextField 			entryStepBy;
    public TextField 			entryPatternRef;
    public Checkbox 			rbLowerError;
    public Checkbox 			rbLastCalculated;
    public Label 				labelCurrentGlobalError;
    public Label 				labelLowerGlobalError;
    public Label				labelLearningSession;
    public Label 				labelOnFoot;
    public FileDialog			saveBPN;
    public FileDialog			loadPatterns;
	public Scale 				valuesScale;

	private Color				minColor					= new Color(255,   0,   0);
	private Color				maxColor					= new Color(  0, 255, 255);

    private BPN					mainBPN;
    private BPN					undoBPN;
    
    private learningThread		currentLearningThread		= null;
    private boolean				isLearning					= false;

	// thread guard
	private Thread 				threadGuard 				= null;
	public  String				todo 						= NONE;
	
	// training parameters
	private int					stepBy						= 100;
	public int					curLearnStopMode			= BOTH;
	public int					curSaveMode					= SAVE_BEST;
	public int					maxSesStop					= 10000;
	public double				minErrStop					= 0.09;
	private learningSet			currentLearningSet			= null;
	private int					currentPattern				= 0;
	private double				minDomain					= -1.0;
	private double				maxDomain					=  1.0;
	private int					showDataFrequence			= 50;
	private int					showGraphFrequence			= 100;

	private ModuleEntry			currentMe;
	
	// behaviour
	boolean						needSave					= true;
	
//////////////////////////////////////////////////////////////////
//
//	constructor
//
//////////////////////////////////////////////////////////////////
    public BPNgui() {
		super("BPN");
		// start thread guard
		start();
	}
			
//////////////////////////////////////////////////////////////////
//
//	NNeMo interface implementors
//
//////////////////////////////////////////////////////////////////
	public void NNeMoInit(ModuleEntry me){
		currentMe = me;
		try{
			if(me.fileName != null){
				// create our main BPN reading from file
				mainBPN = new BPN(me.pathName, me.fileName);
				//System.out.println("load"+me.pathName+" "+ me.fileName);
				// create components in the Frame
				createComponents();

				setTitle(me.pathName, me.fileName);
				show();	
				needSave = false;
				canvasNeuralNet.setBPN(mainBPN);
				// set old size if exist one
				move(me.x, me.y);
				resize(me.width, me.height);
			}
		}catch(BPNException bpne){
			todo = ""+bpne;
			me.fileName = null;
		}
		if(me.fileName == null){
			this.setTitle("BPN - New");		
		
			// create buttons fields ....
			createComponents();

			// setup the minimal possible network
			int layers[] = {1,1};
			String activations[] = {"jaNet.backprop.Sigmoid"};
			try{
				mainBPN = new BPN(layers, activations);
			}catch(BPNException bpne){
				todo = ""+bpne;
			}
			
			undoBPN = null;

			mainBPN.setLearningRate(0.5);
			mainBPN.setMomentum(0.77);
			
			needSave = true;
			canvasNeuralNet.setBPN(mainBPN);
			move(50,50);
			
			me.minDomain = minDomain;
			me.maxDomain = maxDomain;
		}
		// if there was patters load them
		if(me.patternFile.compareTo("")!=0 && me.patternPath.compareTo("")!=0)
			loadPatternFile(me.patternPath, me.patternFile);
		
		show();	
	}
	public void NNeMoClose(){
		exitModule();
	}
	public void NNeMoSave(){
		selectedSave();
	}
	public boolean NNeMoNeedSave(){
		return needSave;
	}

//////////////////////////////////////////////////////////////////
//
//	gui functionality methods
//
//////////////////////////////////////////////////////////////////	
	public void refreshBPNgui(){
	
		canvasNeuralNet.setBPN(mainBPN);
		canvasNeuralNet.getGraphics().clearRect(0,0,canvasNeuralNet.size().width,canvasNeuralNet.size().height);
		canvasNeuralNet.paint(canvasNeuralNet.getGraphics());
		
		if(BPNstopRules != null) 			BPNstopRules.hide();
		if(BPNnetworkStructure != null) 	BPNnetworkStructure.hide();
		if(BPNnetworkParameters != null) 	BPNnetworkParameters.hide();
		if(BPNerrorDialog != null) 			BPNerrorDialog.hide();
		BPNerrorGraph.showElements();
		showPattern();
	}
	
    public void showPattern(int pattern){
		if(mainBPN != null && currentLearningSet != null){
			if(pattern <0 || pattern > currentLearningSet.getSize()) return;
			try{
				mainBPN.propagate(currentLearningSet.inputs[pattern].vector);
			}catch(BPNException bpne){
				todo = ""+bpne;
				return;
			}
			canvasNeuralNet.updateValues(mainBPN.getBPNdescriptor());
		}
    }
    
    public void showPattern(){
		showPattern(currentPattern);
		BPNerrorGraph.showElements();
    }
	public void paint(Graphics g){
		currentMe.width = size().width;
		currentMe.height = size().height;
		super.paint(g);
	}
 
	public void exitModule(){
		// hide all sub-windows
		if(BPNstopRules != null) 			BPNstopRules.hide();
		if(BPNnetworkStructure != null) 	BPNnetworkStructure.hide();
		if(BPNnetworkParameters != null) 	BPNnetworkParameters.hide();
		if(BPNerrorDialog != null) 			BPNerrorDialog.hide();
		if(BPNerrorGraph != null) 			BPNerrorGraph.hide();

		hide();         // hide the Frame
	    //dispose();    // tell windowing system to free resources
	    if(currentLearningThread != null) currentLearningThread.stop();
	    if(threadGuard != null) threadGuard.stop();
	    needSave = false;
	    //System.exit(0); // exit
	}

    public boolean handleEvent(Event event) {
		//System.out.println(""+event.id+" "+event.target);
		if(event.target == entryPatternRef){
			putOnFooter(entryPatternRefHelp);
		}else
		if(event.target == textMonitor){
			putOnFooter(textMonitorHelp);
		}else
		if(event.target == valuesScale){
			putOnFooter(valuesScaleHelp);
		}

     	if (event.id == Event.ACTION_EVENT && event.target == buttonInitNN) {
				// initialise NN
				try{
					mainBPN.randomize();
					putOnMonitor("Weights has been randomized ( min = "+mainBPN.getInitMin()+" , max = "+mainBPN.getInitMax()+" )\n");
					refreshBPNgui();
    	    	}catch(BPNException bpne){
					todo = ""+bpne;
    	    	}
    	    	return true;
    	}
    	else
     	if (event.id == Event.ACTION_EVENT && event.target == buttonStartStop) {
				if(buttonStartStop.getLabel().compareTo("Start")==0){
					selectedStartLearning();
				}else{
					selectedStopLearning();
				}
    	    	return true;
    	}
    	else
     	if (event.id == Event.ACTION_EVENT && event.target == buttonStepBy) {
				selectedStepBy();
    	    	return true;
    	}else
    	if (event.id == Event.KEY_RELEASE && event.target == entryStepBy) {
				String entry = entryStepBy.getText();
				try{
					stepBy = Integer.parseInt(entry);
					//putOnMonitor("step by = "+stepBy+"\n");
				}catch(NumberFormatException nfe){
					if(entry.compareTo("") != 0){
						todo = ""+nfe;
					}else{
						stepBy = 0;
						//putOnMonitor("step by = "+stepBy+"\n");
					}
				}
    	    	return true;
    	}else
    	if (event.id == Event.KEY_RELEASE && event.target == entryPatternRef) {
			String entry = entryPatternRef.getText();
			int value;
			try{
				value = Integer.parseInt(entry);
				//putOnMonitor("step by = "+stepBy+"\n");
			}catch(NumberFormatException nfe){
				if(entry.compareTo("") != 0){
					todo = ""+nfe;
				}
				return true;
			}
			if(value < 0 || value > currentLearningSet.getSize()-1) return true;
			currentPattern = value;
			showPattern();
			return true;
    	}else
    	if (event.id == Event.KEY_ACTION_RELEASE && event.target == entryPatternRef){
			String entry = entryPatternRef.getText();
			int value;
			try{
				value = Integer.parseInt(entry);
				//putOnMonitor("step by = "+stepBy+"\n");
			}catch(NumberFormatException nfe){
				if(entry.compareTo("") != 0){
					todo = ""+nfe;
				}
				return true;
			}
			if(event.key == Event.UP){
				if(value+1 > currentLearningSet.getSize()-1) return true;
				currentPattern = value+1;
			}else
			if(event.key == Event.DOWN){
				if(value-1 < 0) return true;
				currentPattern = value-1;
			}
			entryPatternRef.setText(""+currentPattern);
			showPattern();
			return true;
    	}else
	   	if (event.id == Event.WINDOW_MOVED) {
			currentMe.x = location().x;
			currentMe.y = location().y;
            return true;
    	}else
	   	if (event.id == Event.WINDOW_DESTROY) {
			exitModule();
            return true;
    	}
    	return super.handleEvent(event);
    }

    public boolean action(Event event, Object arg) {
		if (event.target instanceof MenuItem) {
			String label = (String) arg;
			if (label.equalsIgnoreCase("Error Graph...")) {
				selectedErrorGraph();
				return true;
			} else if (label.equalsIgnoreCase("Parameters ...")) {
				selectedNetworkParameters();
				return true;
			} else if (label.equalsIgnoreCase("Stop Rules...")) {
				selectedStopRules();
				return true;
			} else if (label.equalsIgnoreCase("Load Patterns ...")) {
				selectedLoadPatterns();
				return true;
			} else if (label.equalsIgnoreCase("Close Module")) {
				selectedCloseModule();
				return true;
			} else if (label.equalsIgnoreCase("Save As...")) {
				selectedSaveAs();
				return true;
			} else if (label.equalsIgnoreCase("Save")) {
				selectedSave();
				return true;
			} else if (label.equalsIgnoreCase("Display frequency...")) {
				selectedDisplayFrequences();
				return true;
			}
		}
		return super.action(event, arg);
    }
    public void selectedSave() {
		if(isLearning) return;
		if(currentMe.pathName==null || currentMe.fileName==null){
			selectedSaveAs();
			return;
		}
		// saves current state !
		if(saveModule(currentMe.pathName, currentMe.fileName))/* it's OK */;
    }
    public void selectedSaveAs() {
		if(isLearning) return;
		if(currentMe.pathName!=null)
			saveBPN.setDirectory(currentMe.pathName);
		if(currentMe.fileName!=null)
			saveBPN.setFile(currentMe.fileName);
		
		saveBPN.show();
		String file = saveBPN.getFile();
		if(file==null) return;
		if(file.endsWith("*.*")) file = file.substring(file.length()-3,file.length());
		// do it remains something ?
		if(file.length() == 0) return;
		
		if(saveModule(saveBPN.getDirectory(), file)){
			currentMe.pathName = "."; //saveBPN.getDirectory();
			currentMe.fileName = file;
			setTitle(currentMe.pathName,currentMe.fileName);
		}
    }
    public void selectedCloseModule() {
		if(isLearning) return;
		exitModule();
    }
    public void selectedLoadPatterns() {
		if(isLearning) return;
		String				patternfile;
		String				patternpath;

⌨️ 快捷键说明

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