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

📄 multiclasslearner.java

📁 著名的开源仿真软件yale
💻 JAVA
字号:
/* *  YALE - Yet Another Learning Environment *  Copyright (C) 2002, 2003 *      Simon Fischer, Ralf Klinkenberg, Ingo Mierswa,  *          Katharina Morik, Oliver Ritthoff *      Artificial Intelligence Unit *      Computer Science Department *      University of Dortmund *      44221 Dortmund,  Germany *  email: yale@ls8.cs.uni-dortmund.de *  web:   http://yale.cs.uni-dortmund.de/ * *  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *  USA. */package edu.udo.cs.yale.operator.learner;import edu.udo.cs.yale.operator.parameter.*;import edu.udo.cs.yale.operator.OperatorChain;import edu.udo.cs.yale.operator.OperatorException;import edu.udo.cs.yale.operator.IOObject;import edu.udo.cs.yale.operator.IOContainer;import edu.udo.cs.yale.operator.IllegalInputException;import edu.udo.cs.yale.operator.performance.RunVector;          // RK/2003/06/06: error estimation addedimport edu.udo.cs.yale.operator.performance.PerformanceVector;  // RK/2003/06/06: error estimation addedimport edu.udo.cs.yale.operator.learner.Learner;                // RK/2003/06/06: error estimation addedimport edu.udo.cs.yale.example.ExampleSet;import edu.udo.cs.yale.example.Attribute;import edu.udo.cs.yale.tools.TempFileService;import edu.udo.cs.yale.tools.LogService;import java.io.File;import java.io.IOException;import java.util.List;/** This operator chain must contain exactly one inner operator which must (currently) be a SVMLightLearner *  for classification. In order to classify example sets with more than two classifications, the inner *  learner is applied once for each class. Thus, it generates a MultiModel consisting of numberOfClasses  *  submodels. When the MultiModel is applied, all submodels are applied and the class belonging to the *  model with the highest confidence for the "positive" class is chosen. * *  @yale.xmlclass MultiClassLearner *  @see edu.udo.cs.yale.operator.learner.MultiModel *  @see edu.udo.cs.yale.operator.learner.SVMLightLearner *  @author Simon Fischer, Ingo Mierswa, Ralf Klinkenberg *  @version $Id: MultiClassLearner.java,v 2.6 2003/09/03 09:57:02 fischer Exp $ */public class MultiClassLearner extends OperatorChain {    // History:    // - RK/2003/06/06: error estimation added;    //    // To do:    // - generalize class to allow other learners than just SVMLightLearner as inner learner;    private static final Class[]  INPUT_CLASSES  = { ExampleSet.class };        private static final Class[]  OUTPUT_CLASSES = { MultiModel.class };    private int     numberOfClasses;    /** Iterates over all classes <i>i</i>. The     *  inner Learner is notified about the current class by setPositiveLabelIndex(i). Then     *  the learning algorithm is applied. A MultiModel is created from the generated models.     */    public IOObject[] apply() throws OperatorException {	ExampleSet eSet = (ExampleSet)getInput(ExampleSet.class);	numberOfClasses = eSet.getLabel().getNumberOfClasses();	String filename = getParameterAsString("model_file");	Model[] models = new Model[numberOfClasses];	SVMLightLearner learner = (SVMLightLearner)getOperator(0);	// collect performance estimations of the enclosed learners for averaging  // RK/2003/06/06: performance estimation added	RunVector  innerPerformanceEstimations  = new RunVector();                 // RK/2003/06/06: performance estimation added	for (int i = 0; i < numberOfClasses; i++) {	    learner.setPositiveLabelIndex(i + Attribute.FIRST_CLASS_INDEX);	    IOContainer learnResult = learner.apply(getInput().append(new IOObject[] { eSet }));	    models[i] = (Model)learnResult.getInput(Model.class);	    innerPerformanceEstimations.add(learner.getEstimatedPerformance());  // RK/2003/06/06: performance estimation added	}	MultiModel multiModel = new MultiModel(models);	try {	    if (filename != null) multiModel.writeModel(getExperiment().resolveFileName(filename));	} catch (IOException e) {	    LogService.logMessage("MultiClassLearner'" + getName() + "': " +				  "Cannot write MultiModel to disc!", LogService.ERROR);	}	return new IOObject[] { multiModel, innerPerformanceEstimations.average() };    }    public Class[] getInputClasses() { return INPUT_CLASSES; }    public Class[] getOutputClasses() { return OUTPUT_CLASSES; }    /** Ok if the only inner operator returns a learner.     */    public Class[] checkIO(Class[] input) throws IllegalInputException {	SVMLightLearner learner;	try {	    learner = (SVMLightLearner)getOperator(0);	} catch (ClassCastException e) {	    throw new IllegalInputException("MultiClassLearner '" + getName() + "': " + 					    "Inner operator is not a SVMLightLearner.", this);	}	learner.checkIO(input);	return new Class[] { MultiModel.class };    }    /** Returns the maximum number of innner operators. */    public int getMaxNumberOfInnerOperators() { return 1; }    /** Returns the minimum number of innner operators. */    public int getMinNumberOfInnerOperators() { return 1; }    public List getParameterTypes() {	List types = super.getParameterTypes();	types.add(new ParameterTypeFile("model_file", "If this parameter is set, the model is written to a file.", true));	return types;    }    public int getNumberOfSteps() {	return getNumberOfChildrensSteps() * numberOfClasses + 1;    }}

⌨️ 快捷键说明

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