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

📄 controller.java

📁 Boosting算法软件包
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package jboost.controller;import java.io.BufferedWriter;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.FileWriter;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.io.PrintWriter;import java.util.Iterator;import java.util.Vector;import jboost.CandidateSplit;import jboost.NotSupportedException;import jboost.Predictor;import jboost.WritablePredictor;import jboost.atree.AlternatingTree;import jboost.atree.InstrumentException;import jboost.atree.InstrumentedAlternatingTree;import jboost.booster.AbstractBooster;import jboost.booster.BrownBoost;import jboost.booster.Booster;import jboost.booster.Prediction;import jboost.examples.BadLabelException;import jboost.examples.Example;import jboost.examples.ExampleDescription;import jboost.examples.ExampleSet;import jboost.examples.Instance;import jboost.examples.Label;import jboost.examples.LabelDescription;import jboost.examples.WordTable;import jboost.learner.IncompAttException;import jboost.learner.SplitterBuilder;import jboost.learner.SplitterBuilderFamily;import jboost.monitor.Monitor;import jboost.tokenizer.DataStream;import jboost.tokenizer.ExampleStream;import jboost.tokenizer.ParseException;import jboost.tokenizer.jboost_DataStream;import jboost.util.ExecutorSinglet;import EDU.oswego.cs.dl.util.concurrent.CountDown;import EDU.oswego.cs.dl.util.concurrent.Executor;import EDU.oswego.cs.dl.util.concurrent.LinkedQueue;import EDU.oswego.cs.dl.util.concurrent.PooledExecutor;/** * The Controller class is the primary source of execution for jboost code. * This class is designed to be used in an executable jar file. The main() * method will use configuration data to build a learner and read in the train * and test examples. *   */public class Controller {    // main objects    private Booster m_booster;    private InstrumentedAlternatingTree m_learningTree;    private Vector m_splitterBuilderVector;    // configuration object    private ControllerConfiguration m_config;    // m_monitor    private Monitor m_monitor;    private ExampleStream m_trainStream;    private ExampleStream m_testStream;    private ExampleDescription m_exampleDescription;    private int[] m_trainSetIndices;    private AlternatingTree m_serializedTree;      private static final String DEFAULT_MANPAGE = "manpage";    private static final int DEFAULT_NUMROUNDS = 150;      public static void main(String[] argv) {	ControllerConfiguration configuration= null;	Controller controller= null;    	try {	    // read the command line	    configuration= new ControllerConfiguration(DEFAULT_MANPAGE, argv);	    Monitor.init_log(configuration);	    configuration.checkCommandValues();	    controller = new Controller(configuration);	    	    	    // the rest of the code can be called from an external main	    controller.startLearning();	    controller.outputLearningResults();	    shutdown();	} catch (BadCommandException e) {	    configuration.printUsage();	    System.err.println("JBoost Exception: " + e.getMessage());	} catch (Exception e) {	    System.err.println(e.getMessage());	    configuration.printUsage();	    e.printStackTrace();	    e.getMessage();	} finally {	    Monitor.closeLog();	}    }      /**     * Build a default controller     */    public Controller(ControllerConfiguration configuration) throws Exception {	m_config= configuration;	init();    }      /**     * Initialize the controller      * Read in data from the configuration and perform operations that might generate     * exceptions     */    public void init() throws Exception {	try {	    startTokenizer();	} catch (Exception e) {	    // TODO Auto-generated catch block	    e.printStackTrace();	}    	// initialize thread pool, if required	String nthreads_str=m_config.getNThreads();	if(nthreads_str!=null) { // something was specified	    int nthreads=Integer.parseInt(nthreads_str);	    if(nthreads>0) {		if (Monitor.logLevel > 1) {		    Monitor.log("Initializing thread pool of size "+nthreads);		}		PooledExecutor pe=new PooledExecutor(new LinkedQueue(),nthreads);		pe.setMinimumPoolSize(nthreads);		pe.waitWhenBlocked();		ExecutorSinglet.setExecutor(pe);	    }	}        	m_exampleDescription= m_trainStream.getExampleDescription();	// initialize the m_booster	LabelDescription labelDescription= m_exampleDescription.getLabelDescription();	int noOfLabels= labelDescription.getNoOfValues();	boolean multiLabel= labelDescription.isMultiLabel();	m_booster= AbstractBooster.getInstance(m_config, noOfLabels, multiLabel);	if (m_config.getPrintPotential()) {	    BrownBoost b = (BrownBoost) m_booster;	    System.out.println("\tPotential loss of m_booster: " + b.getInitialPotential());	    System.exit(0);	}   	Monitor.log("The m_booster is: " + m_booster);	if (Monitor.logLevel > 1) {	    Monitor.log("The m_booster is: " + m_booster);	}	// build the splitterBuilder array	buildSplitterBuilderArray();    	// load serialized tree, if necessary	String serializedFile= m_config.getString(ControllerConfiguration.SERIALIZED_INPUT, null);	if (serializedFile != null) {	    m_serializedTree= loadTree(serializedFile);	}    	try {	    // read data	    readTrainData();	    System.out.println("Monitor log level: " + Monitor.logLevel);	    if (Monitor.logLevel > 0) {		// XXX: why is this dependent on the logLevel?		System.out.println("Reading test data");		readTestData();	    }	} catch (Exception e) {	    // XXX: what to do?	    throw new InstantiationException(e.getMessage());	}    	// initialize m_monitor	m_monitor= new Monitor(m_booster, m_config);    }      /**     * Necessary shutdown procedures     */    public static void shutdown() {	// shutdown pooled executor	Executor e=ExecutorSinglet.getExecutor();	if(e instanceof PooledExecutor) {	    ((PooledExecutor)e).shutdownNow();	}    }      /**     * Create the InstrumentedAlternatingTree or load it from a file.     * Then run the learn method     * @throws Exception     */    public void startLearning() throws Exception {	long start, stop;	// build Alternating Tree	if (m_serializedTree != null) {	    m_learningTree= new InstrumentedAlternatingTree(m_serializedTree, 							    m_splitterBuilderVector,							    m_booster, m_trainSetIndices,							    m_config);	} else {	    // initialize alternating tree	    m_learningTree= new InstrumentedAlternatingTree(m_splitterBuilderVector, m_booster, 							    m_trainSetIndices,m_config);	    System.out.println("Finished creating root (iteration -1)");	}    	// main loop	start= System.currentTimeMillis();	learn(m_config.getInt("numRounds", DEFAULT_NUMROUNDS));	stop= System.currentTimeMillis();    	if (Monitor.logLevel > 1) {	    Monitor.log("It took " + (stop - start) / 1000.0 + " seconds to learn.");	}    }        /**     * generate output files      * @throws Exception     */    public void outputLearningResults() throws Exception {	// output final result	reportResults();    	WritablePredictor res= m_learningTree.getCombinedPredictor();    	if (Monitor.logLevel > 5) {	    Monitor.log(WordTable.globalTable);	}	//  output a serialization of the alternating tree	//String serializedFile= m_config.getSerializationOutputFileName();	String serializedFile= m_config.getString("serialTreeOutput", null);	if (serializedFile != null) {	    saveTree(res, serializedFile);	}    	// output C code	String cFile= m_config.getCoutputFileName();	if (cFile != null) {	    generateCode(res, "C", cFile,m_config.getString("cOutputProc", "predict"));	}    	// output Matlab code	String matlab= m_config.getMatlabOutputFileName();	if (matlab != null) {	    generateCode(res,"MatLab", matlab, m_config.getString("matlabOutputFunction", "predict"));	}    	// output java code	String java= m_config.getJavaOutputFileName();	if (java != null) {	    generateCode(res, "java", java, m_config.getString("javaOutputClass", "predict"));	}    	if (Monitor.logLevel > 1) {	    test(res);	}    	m_monitor.close();    }        /**     * Serialize the tree and the associated WordTable.      * No information about the booster is stored. This tree can be reloaded     * and used with a completely different booster.     *      * @param atree     * @param serializedTree     * @throws Exception     */    public void saveTree(WritablePredictor atree, String serializedFile) throws Exception{	if (serializedFile != null) {	    ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream(serializedFile));	    oos.writeObject(atree);	    oos.writeObject(WordTable.globalTable);       	    oos.close();	    System.out.println("Wrote tree to " + serializedFile);	} else {	    throw new ConfigurationException("Missing output name for writing Alternting Tree.");	}    }      /**     * Load an existing tree from a file     * Instrument the tree using the examples and booster specified in the configuration     * for this Controller.     * @param serializedFile     * @throws Exception     */    public AlternatingTree loadTree(String serializedFile) throws Exception {	// output a serialization of the alternating tree	m_exampleDescription= m_trainStream.getExampleDescription();	AlternatingTree atree= null;    	ObjectInputStream input= new ObjectInputStream(new FileInputStream(serializedFile));	atree= (AlternatingTree) input.readObject();	WordTable.globalTable= (WordTable) input.readObject();	System.out.println("Loaded tree from file " + serializedFile);    	return atree;    }        public WritablePredictor getPredictor() {	return m_learningTree.getCombinedPredictor();    }        public ExampleStream getTrainStream() {	return m_trainStream;    }      public ExampleStream getTestStream() {	return m_testStream;    }      public Configuration getConfiguration() {	return m_config;    }      public Booster getBooster() {	return m_booster;    }      public InstrumentedAlternatingTree getTree() {	return m_learningTree;    }      /**     * The basic learner.      * @param iterNo number of rounds to boost the learner     * @throws NotSupportedException     * @throws InstrumentException     */    private void learn(int iterNo)	throws NotSupportedException, InstrumentException {	// The stopping criterion should also be more general.

⌨️ 快捷键说明

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