📄 validationchain.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;import edu.udo.cs.yale.tools.LogService;import edu.udo.cs.yale.example.ExampleSet;import edu.udo.cs.yale.example.Attribute;import edu.udo.cs.yale.operator.learner.Model;import edu.udo.cs.yale.operator.performance.PerformanceVector;import edu.udo.cs.yale.operator.performance.PerformanceCriterion;/** 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 2.7 2003/05/14 13:33:20 fischer Exp $ */public abstract class ValidationChain extends OperatorChain { private static final Class[] OUTPUT_CLASSES = { PerformanceVector.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 (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(learner.getName() + " doesn't provide model", this); 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(PerformanceVector.class, input)) throw new IllegalInputException(evaluator.getName() + " does not provide performance vector", this); return new Class[] { PerformanceVector.class }; } /** 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). */ protected IOContainer learn(ExampleSet trainingSet) throws OperatorException { 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. */ 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)!"); } Attribute predictedBefore = testSet.getPredictedLabel(); IOContainer result = getEvaluator().apply(learnResult.append(new IOObject[] { testSet })); Attribute predictedAfter = testSet.getPredictedLabel(); if ((predictedAfter != null) && ((predictedBefore == null) || (predictedBefore.getIndex() != predictedAfter.getIndex()))) { testSet.clearPredictedLabel(); testSet.getExampleTable().removeAttribute(predictedAfter); } learnResult = null; return result; } protected void setLastPerformance(PerformanceCriterion pc) { lastPerformance = pc; } public abstract int getNumberOfValidationSteps(); public int getNumberOfSteps() { return getNumberOfValidationSteps() * super.getNumberOfChildrensSteps() + 1; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -