📄 batchwindowlearner.java
字号:
"the training set. The enclosed learner " + ((Learner) getLearner()).getName() + " is not able to do this."); } } /** applies the inner learner (= first encapsulated inner operator). */ // protected IOContainer learn(ExampleSet trainingSet) throws OperatorException { // return learnResult = getLearner().apply(getInput().append(new IOObject[] { trainingSet })); // } /** applies the inner applier and evaluator (= second encapsulated inner operator). */ // protected IOContainer evaluate (Model learnResult, ExampleSet testSet) throws OperatorException { // if (learnResult == null) { // throw new FatalException("Wrong use of BatchWindowLearner.evaluate(Model,ExampleSet): " + // "learned model is null."); // } // IOContainer input = new IOContainer(new IOObject[] { learnResult, testSet }); // IOContainer result = getModelApplierAndPerformanceEvaluator().apply(input); // return result; // } /** set weights of all training examples (= all examples in past batches) */ /* private void setExponentialTrainingExampleWeights (BatchedExampleSet exampleSet, double lambda) throws OperatorException { ExampleReader exampleIterator = exampleSet.getExampleReader(); Example currentExample = null; Attribute batchIndexAttribute = exampleSet.getBatchIndexAttribute(); double currentBatch = 0.0; double weight = 0.0; // --- global weighting loop --- while (exampleIterator.hasNext()) { currentExample = exampleIterator.next(); currentBatch = currentExample.getValue (batchIndexAttribute); if (currentBatch < ((double)exampleSet.getLastBatch())) { // only change weight of examples in past batches weight = computeGlobalExponentialWeight (lambda, (exampleSet.getLastBatch() - currentBatch)); // RK/2002/05/25: new if (weight < minimalWeight) { weight = 0.0; } // do not consider weights below the treshold try { currentExample.setWeight (weight); } catch (MethodNotSupportedException e) { throw new FatalException("BatchWindowLearner '" + getName() + "': The example set passed to " + "this operator must contain examples with weights.", e); } } } } */ /** This method learns a classification model and returns it (along with a performance estimation * (xi-alpha-estimation of the classification error)). */ public Model learn (ExampleSet inputSet) throws OperatorException { //// abstract method in super classes Learner and BatchLearner //// RK/2003/07/25: prevent class cast exception: LogService.logMessage ("BatchWindowLearner '" + getName() + "', method 'learn(ExampleSet inputSet)': " + "Check classes of internal learner and " + "example set...", LogService.MINIMUM); if (!(inputSet instanceof BatchedExampleSet)) throw new OperatorException("BatchWindowLearner '" + getName() + "', method 'learn(ExampleSet inputSet)': " + "the input example set must be an instance of" + "BatchedExampleSet, but it is not"); if (!(getLearner() instanceof Learner)) // or MultiClassLearner throw new OperatorException("BatchWindowLearner '" + getName() + "', method 'learn(ExampleSet inputSet)': " + "the first encapsulated operator must be"+ "an instance of Learner, but it is not"); LogService.logMessage ("BatchWindowLearner '" + getName() + "', method 'learn(ExampleSet inputSet)': " + "Classes of internal learner and example set " + "successfully checked.", LogService.MINIMUM); //// BatchedExampleSet exampleSet = (BatchedExampleSet) inputSet; // type cast of input example set to BatchedExampleSet BatchedExampleSet trainingExampleSet = null; // examples in time window used for training Learner learner = (Learner)(getLearner()); // RK/2003/07/25: ??? //Operator learner = (Learner)(getLearner()); // RK/2003/07/25: ??? PerformanceVector currentPerformance = null; double bestWindowSize = 1; double bestError = 1.0; double currentError = 1.0; Model bestModel = null; Model currentModel = null; // LogService.logMessage ("BatchWindowLearner '" + getName() + // RK/2003/05/05: already done below // "': Start learning...", LogService.TASK); // RK/2003/05/05: already done below // // LogService.logMessage (" Transduction scheme: " + // // TRANSDUCTION_SCHEME_TYPE_NAME[transductionScheme] + "\n", // // LogService.TASK); performanceEstimation = null; int lastBatch = exampleSet.getLastBatch(); // last batch available for the training set int firstBatch = exampleSet.getFirstBatch(); // first batch available for the training set if (firstBatch < lastBatch - maximumWindowSize) // consider maximum time window size (default: unlimited) firstBatch = lastBatch - maximumWindowSize + 1; if (minimumWindowSize > 1) // consider minimum time window size (default: 1) firstBatch += minimumWindowSize - 1; for (int batch = firstBatch; batch <= lastBatch; batch++) { LogService.logMessage("BatchWindowLearner '" + getName() + "': Start learning for window size = " + (lastBatch - batch + 1) + " ...", LogService.TASK); ResultService.logResult("BatchWindowLearner '" + getName() + // RK/2002/07/05: TMP "': Start learning for window size = " + // RK/2002/07/05: TMP (lastBatch - batch + 1) + " ..."); // RK/2002/07/05: TMP trainingExampleSet = new BatchedExampleSet (exampleSet, exampleSet.getBatchIndexAttribute(), batch, lastBatch); LogService.logMessage ("BatchWindowLearner '" + getName() + "': ... training example set created.", LogService.MINIMUM); // RK/2003/05/05: TMP TEMP //// learn model and estimate its performance on the training set: //currentModel = learner.learn ((ExampleSet) trainingExampleSet); // TMP: Simon/2003/15/05 IOContainer learnResult = learner.apply(new IOContainer(new IOObject[] {trainingExampleSet})); currentModel = (Model)learnResult.getInput(Model.class); LogService.logMessage ("BatchWindowLearner '" + getName() + "': ... model learned.", LogService.MINIMUM); // RK/2003/05/05: TMP TEMP currentPerformance = (PerformanceVector)learnResult.getInput(PerformanceVector.class); //currentPerformance = learner.getEstimatedPerformance(); LogService.logMessage ("BatchWindowLearner '" + getName() + "': ... performance evaluated.", LogService.MINIMUM); // RK/2003/05/05: TMP TEMP // currentError = (currentPerformance.get(0)).getValue(); // RK/2003/04/30: old version currentError = (currentPerformance.getMainCriterion()).getValue(); // RK/2003/04/30: new version LogService.logMessage ("BatchWindowLearner '" + getName() + "': ... current error retrieved.", LogService.MINIMUM); // RK/2003/05/05: TMP TEMP //// if current performance is better than best performance found so far, store it if ((currentError < bestError) || (bestModel == null)) { bestWindowSize = lastBatch - batch + 1; bestError = currentError; bestModel = currentModel; performanceEstimation = currentPerformance; } } LogService.logMessage ("BatchWindowLearner '" + this.getName() + "': selected window size = " + bestWindowSize + " for current batch = " + exampleSet.getLastBatch() + " (estimated classification error (or alpha sum): " + bestError + ").", LogService.TASK); ResultService.logResult ("BatchWindowLearner '" + this.getName() + "': selected window size = " + // RK/2002/07/05: TMP bestWindowSize + " for current batch = " + exampleSet.getLastBatch() + " (estimated classification error (or alpha sum): " + bestError + ")."); return bestModel; } /** returns <code>true</code> */ public boolean canEstimatePerformance() { return true; } /** returns an object of the class <tt>EstimatedPerformance</tt> containing the (xi-alpha-)performance * estimates of the model learned by the enclosed learner (e.g. a <code>SVMLightLearner</code> or a * <code>MySVMLearner</code>), if the method <code>learn(ExampleSet inputSet)</code> has been called * before. */ public PerformanceVector getEstimatedPerformance() { return performanceEstimation; } /** learns a model from a given example set (i.e. given in the input of the operator (<code>IOContainer</code>)). * The model is stored under the name <tt>model_file</tt> (if specified). */ public IOObject[] apply() throws OperatorException { // from super class Learner ExampleSet exampleSet = (BatchedExampleSet)getInput(ExampleSet.class); if (exampleSet==null) { throw new FatalException("BatchWindowLearner '"+getName()+"': No input example set!"); } if (exampleSet.getNumberOfAttributes()==0) { throw new FatalException("BatchWindowLearner '"+getName()+"': Input example set has no attributes!"); } // LogService.logMessage("BatchWindowLearner '" + getName() + "': Start learning...", LogService.TASK); // RK/2003/05/05: already done in 'learn(.)' method Model model = learn(exampleSet); try { if (modelFile != null) model.writeModel(getExperiment().resolveFileName(modelFile)); } catch (IOException e) { LogService.logMessage("BatchWindowLearner '" + getName() + "' Cannot write model file: " + modelFile, LogService.ERROR); } PerformanceVector perfVector = getEstimatedPerformance(); if (perfVector == null) { return new IOObject[] { model }; } else { return new IOObject[] { model, perfVector }; } } /** returns the input types of this operator as an array of the classes * of which objects need to be provided to this operator in the input array. */ public Class[] getInputClasses() { return INPUT_CLASSES; } /** returns the output types of this operator as an array of the * classes of the objects that this operator will deliver as output array. */ public Class[] getOutputClasses() { // from super class Learner return canEstimatePerformance() ? new Class[] { Model.class, PerformanceVector.class } : new Class[] { Model.class }; } /** returns the minimum of two integer variables. */ protected int minimumInt (int v1, int v2) { if (v1 < v2) return v1; return v2; } /** returns the maximum of two integer variables. */ protected int maximumInt (int v1, int v2) { if (v1 > v2) return v1; return v2; } /* As long as no JavaDoc comment is specified here, JavaDoc copies the comment * from the corresponding method of the super class. */ public List getParameterTypes() { List types = super.getParameterTypes(); types.add(new ParameterTypeString("model_file", "If set, the model is saved to the given file.", null)); types.add(new ParameterTypeInt("maximum_window_size", "Maximal size of the time window on the training data (default: unlimited size)", 1, Integer.MAX_VALUE, Integer.MAX_VALUE)); // value range = 1..MaxInteger, default = MaxInteger types.add(new ParameterTypeInt("minimum_window_size", "Minimal size of the time window on the training data (default value: 1 batch)", 1, Integer.MAX_VALUE, 1)); // value range = 1..MaxInteger, default = 1 return types; } /** Returns the minimum number of innner operators, which is equal to 1. */ public int getMinNumberOfInnerOperators() { return 1; } /** Returns the maximum number of innner operators, which is equal to the maximum <tt>Integer</tt> value of Java. */ public int getMaxNumberOfInnerOperators() { return Integer.MAX_VALUE; } /* As long as no JavaDoc comment is specified here, JavaDoc copies the comment * from the corresponding method of the super class. */ public int getNumberOfSteps() { // TO DO: return (sum number of steps of all inner operators) * (number of runs) ? return 1; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -