📄 validationchain.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.validation;
import edu.udo.cs.yale.tools.math.AverageVector;
import edu.udo.cs.yale.example.ExampleSet;
import edu.udo.cs.yale.example.Attribute;
import edu.udo.cs.yale.operator.Operator;
import edu.udo.cs.yale.operator.OperatorChain;
import edu.udo.cs.yale.operator.IOContainer;
import edu.udo.cs.yale.operator.OperatorException;
import edu.udo.cs.yale.operator.IllegalInputException;
import edu.udo.cs.yale.operator.IODescription;
import edu.udo.cs.yale.operator.IOObject;
import edu.udo.cs.yale.operator.MissingIOObjectException;
import edu.udo.cs.yale.operator.Value;
import edu.udo.cs.yale.operator.learner.Model;
import edu.udo.cs.yale.operator.performance.PerformanceVector;
import edu.udo.cs.yale.operator.performance.PerformanceCriterion;
import java.util.List;
/** Abstract superclass of operator chains that split an {@link ExampleSet} into a training
* and test set and return a performance vector. The two inner operators must be a
* learner returning a {@link Model} and an operator or operator chain that can
* apply this model and returns a {@link PerformanceVector}. Hence the second inner operator
* usually is an operator chain containing a model applier and a performance evaluator.
*
* @author ingo, simon
* @version $Id: ValidationChain.java,v 1.5 2004/10/07 20:31:00 ingomierswa Exp $
*/
public abstract class ValidationChain extends OperatorChain {
private static final Class[] OUTPUT_CLASSES = { AverageVector.class };
private static final Class[] INPUT_CLASSES = { ExampleSet.class };
private PerformanceCriterion lastPerformance;
private IOContainer learnResult;
private boolean methodEvaluation;
public ValidationChain() {
addValue(new Value("performance", "The last performance average (main criterion).") {
public double getValue() {
if (lastPerformance != null)
return lastPerformance.getValue();
else
return Double.NaN;
}
});
addValue(new Value("variance", "The variance of the last performance (main criterion).") {
public double getValue() {
if (lastPerformance != null)
return lastPerformance.getVariance();
else
return Double.NaN;
}
});
}
/** Returns the maximum number of innner operators. */
public int getMaxNumberOfInnerOperators() { return 2; }
/** Returns the minimum number of innner operators. */
public int getMinNumberOfInnerOperators() { return 2; }
/** returns the the classes this operator expects as input. */
public Class[] getOutputClasses() { return OUTPUT_CLASSES; }
/** returns the the classes this operator provides as output. */
public Class[] getInputClasses() { return INPUT_CLASSES; }
/** Checks the correctness of the input and output classes requested and provided,
* respectively, by the encapsulated inner operators of the <code>ValidationChain</code>.
* These input and output classes are OK, if the first inner operator returns a model and
* the second returns a performance vector.
* The method returns the output classes of the second encapsulated inner operator.
*/
public Class[] checkIO(Class[] input) throws IllegalInputException {
Operator learner = getLearner();
Operator evaluator = getEvaluator();
input = learner.checkIO(input);
if (!IODescription.containsClass(Model.class, input))
throw new IllegalInputException(this, learner, Model.class);
Class[] newInput = new Class[input.length+1];
for (int i = 0; i < input.length; i++) {
newInput[i] = input[i];
}
newInput[newInput.length-1] = ExampleSet.class;
input = evaluator.checkIO(newInput);
if (!IODescription.containsClass(AverageVector.class, input))
throw new IllegalInputException(this, evaluator, AverageVector.class);
return input;
}
/** Returns the first encapsulated inner operator (or operator chain),
* i.e. the learning operator (chain). */
private Operator getLearner() { return getOperator(0); }
/** Returns the second encapsulated inner operator (or operator chain),
* i.e. the application and evaluation operator (chain) */
private Operator getEvaluator() { return getOperator(1); }
/** Can be used by subclasses to set the performance of the example set. */
protected void setResult(PerformanceCriterion pc) { lastPerformance = pc; }
/** Applies the learner (= first encapsulated inner operator).
* This method recalculates the attribute statistics on the training set. */
protected IOContainer learn(ExampleSet trainingSet) throws OperatorException {
trainingSet.recalculateAllAttributeStatistics();
return learnResult = getLearner().apply(getInput().prepend(new IOObject[] { trainingSet }));
}
/** Applies the applier and evaluator (= second encapsulated inner operator).
* In order to reuse possibly created predicted label attributes, we do the following:
* We compare the predicted label of <code>testSet</code> before and
* after applying the inner operator. If it changed, the predicted label is removed again.
* No outer operator could ever see it.
* This method recalculates the attribute statistics on the test set. */
protected IOContainer evaluate(ExampleSet testSet) throws OperatorException {
if (learnResult == null) {
throw new RuntimeException("Wrong use of ValidationChain.evaluate(ExampleSet): " +
"No preceding invocation of learn(ExampleSet)!");
}
testSet.recalculateAllAttributeStatistics();
Attribute predictedBefore = testSet.getPredictedLabel();
IOContainer evalInput = learnResult.append(new IOObject[] { testSet });
IOContainer result = getEvaluator().apply(evalInput);
Attribute predictedAfter = testSet.getPredictedLabel();
if ((predictedAfter != null) &&
((predictedBefore == null) ||
(predictedBefore.getIndex() != predictedAfter.getIndex()))) {
testSet.clearPredictedLabel();
testSet.getExampleTable().removeAttribute(predictedAfter);
}
learnResult = null;
return result;
}
/** Searches for the average vectors in the given IOContainer and fills the list if it is empty
* or build the averages.*/
protected void handleAverages(IOContainer evalOutput, List averageVectors) {
PerformanceVector performanceVector = null;
int n = 0;
boolean inputOk = true;
while (inputOk) {
try {
AverageVector currentAverage = (AverageVector)evalOutput.getInput(AverageVector.class);
if (averageVectors.size() == 0) {
// first run --> do not calculate average values but fill the vector list
averageVectors.add(currentAverage);
} else {
// later runs --> build the average with corresponding average vectors
AverageVector oldVector = (AverageVector)averageVectors.get(n++);
for (int i = 0; i < oldVector.size(); i++) {
oldVector.getAveragable(i).buildAverage(currentAverage.getAveragable(i));
}
}
} catch (MissingIOObjectException e) {
inputOk = false;
}
}
}
/** Returns the first performance vector in the given list or null if no performance vectors exist. */
protected PerformanceVector getPerformanceVector(List averageVectors) {
java.util.Iterator i = averageVectors.iterator();
while (i.hasNext()) {
AverageVector currentAverage = (AverageVector)i.next();
if (currentAverage instanceof PerformanceVector)
return (PerformanceVector)currentAverage;
}
return null;
}
public abstract int getNumberOfValidationSteps();
public int getNumberOfSteps() {
return getNumberOfValidationSteps() * super.getNumberOfChildrensSteps() + 1;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -