📄 multiclasslearner.java
字号:
/*
* YALE - Yet Another Learning Environment
* Copyright (C) 2001-2004
* Simon Fischer, Ralf Klinkenberg, Ingo Mierswa,
* Katharina Morik, Oliver Ritthoff
* Artificial Intelligence Unit
* Computer Science Department
* University of Dortmund
* 44221 Dortmund, Germany
* email: yale-team@lists.sourceforge.net
* 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.meta;
import edu.udo.cs.yale.operator.parameter.*;
import edu.udo.cs.yale.operator.learner.Model;
import edu.udo.cs.yale.operator.learner.Learner;
import edu.udo.cs.yale.operator.learner.kernel.SVMLightLearner;
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.tools.math.RunVector; // RK/2003/06/06: error estimation added
import edu.udo.cs.yale.operator.performance.PerformanceVector; // RK/2003/06/06: error estimation added
import edu.udo.cs.yale.operator.learner.Learner; // RK/2003/06/06: error estimation added
import edu.udo.cs.yale.example.ExampleSet;
import edu.udo.cs.yale.example.Attribute;
import edu.udo.cs.yale.example.AttributeWeights;
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.
* <br/>
* The performance vector returned by this operator is the average of the estimated performances returned
* by the inner {@link SVMLightLearner}. Hence, this estimation only is a good estimation for the models
* of the inner binary learner, but not for the complete multi class model.
*
* @yale.xmlclass MultiClassLearner
* @see edu.udo.cs.yale.operator.learner.meta.MultiModel
* @see edu.udo.cs.yale.operator.learner.kernel.SVMLightLearner
* @author Simon Fischer, Ingo Mierswa, Ralf Klinkenberg
* @version $Id: MultiClassLearner.java,v 1.3 2004/08/27 11:57:41 ingomierswa Exp $
*/
public class MultiClassLearner extends OperatorChain implements Learner {
// 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, PerformanceVector.class };
private int numberOfClasses = 2;
private RunVector innerPerformanceEstimations = new RunVector();
/** 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 exampleSet = (ExampleSet)getInput(ExampleSet.class);
numberOfClasses = exampleSet.getLabel().getValues().size();
Model multiModel = learn(exampleSet);
String filename = getParameterAsString("model_file");
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 Model learn(ExampleSet exampleSet) throws OperatorException {
Model[] models = new Model[numberOfClasses];
SVMLightLearner learner = (SVMLightLearner)getOperator(0);
for (int i = 0; i < numberOfClasses; i++) {
learner.setPositiveLabelIndex(i + Attribute.FIRST_CLASS_INDEX);
IOContainer learnResult = learner.apply(getInput().append(new IOObject[] { exampleSet }));
models[i] = (Model)learnResult.getInput(Model.class);
innerPerformanceEstimations.addVector(learner.getEstimatedPerformance());
}
MultiModel multiModel = new MultiModel(exampleSet.getLabel(), models);
return multiModel;
}
/** Returns true iff the learner can generate a performance vector during training. */
public boolean canEstimatePerformance() { return true; }
public PerformanceVector getEstimatedPerformance() { return (PerformanceVector)innerPerformanceEstimations.average(); }
/** Returns true iff the learner can generate a attribute weight vector. */
public boolean canCalculateWeights() { return false; }
/** Returns the calculated weight vectors. */
public AttributeWeights getWeights(ExampleSet exampleSet) { return null; }
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(this,
"MultiClassLearner '" + getName() + "': " +
"Inner operator is not a SVMLightLearner.");
}
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();
ParameterType type = new ParameterTypeFile("model_file", "If this parameter is set, the model is written to a file.", true);
type.setExpert(false);
types.add(type);
return types;
}
public int getNumberOfSteps() {
return getNumberOfChildrensSteps() * numberOfClasses + 1;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -