From 8b83ecc5e43ee36d0ee52b20dd8e317f5a379a17 Mon Sep 17 00:00:00 2001 From: Anton Lee Date: Thu, 21 Sep 2023 15:19:12 +1200 Subject: [PATCH] Close evaluator after every task --- .../java/moa/tasks/EvaluateConceptDrift.java | 8 +- .../moa/tasks/EvaluateModelMultiLabel.java | 5 + .../moa/tasks/EvaluateModelMultiTarget.java | 5 + .../moa/tasks/EvaluateModelRegression.java | 5 + .../tasks/EvaluatePrequentialMultiLabel.java | 5 + .../tasks/EvaluatePrequentialMultiTarget.java | 5 + ...aluatePrequentialMultiTargetSemiSuper.java | 6 +- .../tasks/EvaluatePrequentialRegression.java | 5 + .../meta/ALPrequentialEvaluationTask.java | 322 +++++++++--------- 9 files changed, 203 insertions(+), 163 deletions(-) diff --git a/moa/src/main/java/moa/tasks/EvaluateConceptDrift.java b/moa/src/main/java/moa/tasks/EvaluateConceptDrift.java index 322eb0b24..1086367cd 100644 --- a/moa/src/main/java/moa/tasks/EvaluateConceptDrift.java +++ b/moa/src/main/java/moa/tasks/EvaluateConceptDrift.java @@ -231,9 +231,11 @@ protected Object doMainTask(TaskMonitor monitor, ObjectRepository repository) { if (immediateResultStream != null) { immediateResultStream.close(); } - /* if (outputPredictionResultStream != null) { - outputPredictionResultStream.close(); - }*/ + try { + evaluator.close(); + } catch (Exception ex) { + throw new RuntimeException("Exception closing evaluator", ex); + } return learningCurve; } } \ No newline at end of file diff --git a/moa/src/main/java/moa/tasks/EvaluateModelMultiLabel.java b/moa/src/main/java/moa/tasks/EvaluateModelMultiLabel.java index b1e118db7..010e9d25a 100644 --- a/moa/src/main/java/moa/tasks/EvaluateModelMultiLabel.java +++ b/moa/src/main/java/moa/tasks/EvaluateModelMultiLabel.java @@ -156,6 +156,11 @@ public Object doMainTask(TaskMonitor monitor, ObjectRepository repository) { if (outputPredictionResultStream != null) { outputPredictionResultStream.close(); } + try { + evaluator.close(); + } catch (Exception ex) { + throw new RuntimeException("Exception closing evaluator", ex); + } return new LearningEvaluation(evaluator, model); } } diff --git a/moa/src/main/java/moa/tasks/EvaluateModelMultiTarget.java b/moa/src/main/java/moa/tasks/EvaluateModelMultiTarget.java index df5d18631..ded4f6637 100644 --- a/moa/src/main/java/moa/tasks/EvaluateModelMultiTarget.java +++ b/moa/src/main/java/moa/tasks/EvaluateModelMultiTarget.java @@ -152,6 +152,11 @@ public Object doMainTask(TaskMonitor monitor, ObjectRepository repository) { if (outputPredictionResultStream != null) { outputPredictionResultStream.close(); } + try { + evaluator.close(); + } catch (Exception ex) { + throw new RuntimeException("Exception closing evaluator", ex); + } return new LearningEvaluation(evaluator, model); } } diff --git a/moa/src/main/java/moa/tasks/EvaluateModelRegression.java b/moa/src/main/java/moa/tasks/EvaluateModelRegression.java index 34b57232c..69dee8c18 100644 --- a/moa/src/main/java/moa/tasks/EvaluateModelRegression.java +++ b/moa/src/main/java/moa/tasks/EvaluateModelRegression.java @@ -152,6 +152,11 @@ public Object doMainTask(TaskMonitor monitor, ObjectRepository repository) { if (outputPredictionResultStream != null) { outputPredictionResultStream.close(); } + try { + evaluator.close(); + } catch (Exception ex) { + throw new RuntimeException("Exception closing evaluator", ex); + } return new LearningEvaluation(evaluator, model); } } diff --git a/moa/src/main/java/moa/tasks/EvaluatePrequentialMultiLabel.java b/moa/src/main/java/moa/tasks/EvaluatePrequentialMultiLabel.java index 8a5a198a9..21427a7e0 100644 --- a/moa/src/main/java/moa/tasks/EvaluatePrequentialMultiLabel.java +++ b/moa/src/main/java/moa/tasks/EvaluatePrequentialMultiLabel.java @@ -289,6 +289,11 @@ protected Object doMainTask(TaskMonitor monitor, ObjectRepository repository) { if (outputPredictionResultStream != null) { outputPredictionResultStream.close(); } + try { + evaluator.close(); + } catch (Exception ex) { + throw new RuntimeException("Exception closing evaluator", ex); + } return learningCurve; } } diff --git a/moa/src/main/java/moa/tasks/EvaluatePrequentialMultiTarget.java b/moa/src/main/java/moa/tasks/EvaluatePrequentialMultiTarget.java index 28f782665..083bc879c 100644 --- a/moa/src/main/java/moa/tasks/EvaluatePrequentialMultiTarget.java +++ b/moa/src/main/java/moa/tasks/EvaluatePrequentialMultiTarget.java @@ -277,6 +277,11 @@ protected Object doMainTask(TaskMonitor monitor, ObjectRepository repository) { if (outputPredictionResultStream != null) { outputPredictionResultStream.close(); } + try { + evaluator.close(); + } catch (Exception ex) { + throw new RuntimeException("Exception closing evaluator", ex); + } return learningCurve; } } diff --git a/moa/src/main/java/moa/tasks/EvaluatePrequentialMultiTargetSemiSuper.java b/moa/src/main/java/moa/tasks/EvaluatePrequentialMultiTargetSemiSuper.java index 52b24b4dd..1f973bd60 100644 --- a/moa/src/main/java/moa/tasks/EvaluatePrequentialMultiTargetSemiSuper.java +++ b/moa/src/main/java/moa/tasks/EvaluatePrequentialMultiTargetSemiSuper.java @@ -405,7 +405,11 @@ protected Object doMainTask(TaskMonitor monitor, ObjectRepository repository) { if (outputPredictionResultStream != null) { outputPredictionResultStream.close(); } - + try { + evaluator.close(); + } catch (Exception ex) { + throw new RuntimeException("Exception closing evaluator", ex); + } return learningCurve; } } diff --git a/moa/src/main/java/moa/tasks/EvaluatePrequentialRegression.java b/moa/src/main/java/moa/tasks/EvaluatePrequentialRegression.java index 26a3c844c..25fb1b5fd 100644 --- a/moa/src/main/java/moa/tasks/EvaluatePrequentialRegression.java +++ b/moa/src/main/java/moa/tasks/EvaluatePrequentialRegression.java @@ -272,6 +272,11 @@ protected Object doMainTask(TaskMonitor monitor, ObjectRepository repository) { if (outputPredictionResultStream != null) { outputPredictionResultStream.close(); } + try { + evaluator.close(); + } catch (Exception ex) { + throw new RuntimeException("Exception closing evaluator", ex); + } return learningCurve; } } diff --git a/moa/src/main/java/moa/tasks/meta/ALPrequentialEvaluationTask.java b/moa/src/main/java/moa/tasks/meta/ALPrequentialEvaluationTask.java index a2813a1b6..21df6ccc7 100644 --- a/moa/src/main/java/moa/tasks/meta/ALPrequentialEvaluationTask.java +++ b/moa/src/main/java/moa/tasks/meta/ALPrequentialEvaluationTask.java @@ -52,90 +52,90 @@ * @version $Revision: 1 $ */ public class ALPrequentialEvaluationTask extends ALMainTask { - - private static final long serialVersionUID = 1L; - - @Override - public String getPurposeString() { - return "Perform prequential evaluation (testing, then training with" - + " each example in sequence) for an active learning" - + " classifier."; - } - - public ClassOption learnerOption = new ClassOption("learner", 'l', + + private static final long serialVersionUID = 1L; + + @Override + public String getPurposeString() { + return "Perform prequential evaluation (testing, then training with" + + " each example in sequence) for an active learning" + + " classifier."; + } + + public ClassOption learnerOption = new ClassOption("learner", 'l', "Learner to train.", ALClassifier.class, "moa.classifiers.active.ALRandom"); - - public ClassOption streamOption = new ClassOption("stream", 's', + + public ClassOption streamOption = new ClassOption("stream", 's', "Stream to learn from.", ExampleStream.class, "generators.RandomTreeGenerator"); - - public ClassOption evaluatorOption = new ClassOption( - "evaluator", 'e', + + public ClassOption evaluatorOption = new ClassOption( + "evaluator", 'e', "Active Learning classification performance evaluation method.", ALClassificationPerformanceEvaluator.class, "ALWindowClassificationPerformanceEvaluator"); - - public IntOption instanceLimitOption = new IntOption("instanceLimit", 'i', + + public IntOption instanceLimitOption = new IntOption("instanceLimit", 'i', "Maximum number of instances to test/train on (-1 = no limit).", 100000000, -1, Integer.MAX_VALUE); - - public IntOption timeLimitOption = new IntOption("timeLimit", 't', + + public IntOption timeLimitOption = new IntOption("timeLimit", 't', "Maximum number of seconds to test/train for (-1 = no limit).", -1, -1, Integer.MAX_VALUE); - - public IntOption sampleFrequencyOption = new IntOption("sampleFrequency", + + public IntOption sampleFrequencyOption = new IntOption("sampleFrequency", 'f', "How many instances between samples of the learning performance.", 100000, 0, Integer.MAX_VALUE); - - public FileOption dumpFileOption = new FileOption("dumpFile", 'd', + + public FileOption dumpFileOption = new FileOption("dumpFile", 'd', "File to append intermediate csv results to.", null, "csv", true); - - - /** - * Constructor which sets the color coding to black. - */ - public ALPrequentialEvaluationTask() { - this(Color.BLACK); - } - - /** - * Constructor with which a color coding can be set. - * @param colorCoding the color used by the task - */ - public ALPrequentialEvaluationTask(Color colorCoding) { - this.colorCoding = colorCoding; - } - - @Override - public Class getTaskResultType() { - return LearningCurve.class; - } - - @SuppressWarnings("unchecked") - @Override - protected Object doMainTask(TaskMonitor monitor, ObjectRepository repository) { - // get stream - ExampleStream> stream = - (ExampleStream>) - getPreparedClassOption(this.streamOption); - - // initialize learner - ALClassifier learner = - (ALClassifier) getPreparedClassOption(this.learnerOption); - learner.resetLearning(); - learner.setModelContext(stream.getHeader()); - - // get evaluator + + + /** + * Constructor which sets the color coding to black. + */ + public ALPrequentialEvaluationTask() { + this(Color.BLACK); + } + + /** + * Constructor with which a color coding can be set. + * @param colorCoding the color used by the task + */ + public ALPrequentialEvaluationTask(Color colorCoding) { + this.colorCoding = colorCoding; + } + + @Override + public Class getTaskResultType() { + return LearningCurve.class; + } + + @SuppressWarnings("unchecked") + @Override + protected Object doMainTask(TaskMonitor monitor, ObjectRepository repository) { + // get stream + ExampleStream> stream = + (ExampleStream>) + getPreparedClassOption(this.streamOption); + + // initialize learner + ALClassifier learner = + (ALClassifier) getPreparedClassOption(this.learnerOption); + learner.resetLearning(); + learner.setModelContext(stream.getHeader()); + + // get evaluator ALClassificationPerformanceEvaluator evaluator = (ALClassificationPerformanceEvaluator) - getPreparedClassOption(this.evaluatorOption); + getPreparedClassOption(this.evaluatorOption); // initialize learning curve LearningCurve learningCurve = new LearningCurve( - "learning evaluation instances"); + "learning evaluation instances"); - // perform training and testing + // perform training and testing int maxInstances = this.instanceLimitOption.getValue(); int instancesProcessed = 0; int maxSeconds = this.timeLimitOption.getValue(); @@ -149,75 +149,75 @@ protected Object doMainTask(TaskMonitor monitor, ObjectRepository repository) { File dumpFile = this.dumpFileOption.getFile(); PrintStream immediateResultStream = null; if (dumpFile != null) { - try { - if (dumpFile.exists()) { - immediateResultStream = new PrintStream( + try { + if (dumpFile.exists()) { + immediateResultStream = new PrintStream( new FileOutputStream(dumpFile, true), true); } else { immediateResultStream = new PrintStream( new FileOutputStream(dumpFile), true); } - } catch (Exception ex) { + } catch (Exception ex) { throw new RuntimeException( - "Unable to open immediate result file: " + dumpFile, ex); + "Unable to open immediate result file: " + dumpFile, ex); } } monitor.setCurrentActivity("Evaluating learner...", -1.0); while (stream.hasMoreInstances() - && ((maxInstances < 0) - || (instancesProcessed < maxInstances)) - && ((maxSeconds < 0) || (secondsElapsed < maxSeconds))) + && ((maxInstances < 0) + || (instancesProcessed < maxInstances)) + && ((maxSeconds < 0) || (secondsElapsed < maxSeconds))) { - Example trainInst = stream.nextInstance(); - Example testInst = trainInst; - - // predict class for instance - double[] prediction = learner.getVotesForInstance(testInst); - evaluator.addResult(testInst, prediction); - - // train on instance - learner.trainOnInstance(trainInst); - - // check if label was acquired - int labelAcquired = learner.getLastLabelAcqReport(); - evaluator.doLabelAcqReport(trainInst, labelAcquired); - - instancesProcessed++; - - // update learning curve - if (instancesProcessed % this.sampleFrequencyOption.getValue() == 0 - || !stream.hasMoreInstances()) - { - long evaluateTime = - TimingUtils.getNanoCPUTimeOfCurrentThread(); + Example trainInst = stream.nextInstance(); + Example testInst = trainInst; + + // predict class for instance + double[] prediction = learner.getVotesForInstance(testInst); + evaluator.addResult(testInst, prediction); + + // train on instance + learner.trainOnInstance(trainInst); + + // check if label was acquired + int labelAcquired = learner.getLastLabelAcqReport(); + evaluator.doLabelAcqReport(trainInst, labelAcquired); + + instancesProcessed++; + + // update learning curve + if (instancesProcessed % this.sampleFrequencyOption.getValue() == 0 + || !stream.hasMoreInstances()) + { + long evaluateTime = + TimingUtils.getNanoCPUTimeOfCurrentThread(); double time = TimingUtils.nanoTimeToSeconds( - evaluateTime - evaluateStartTime); + evaluateTime - evaluateStartTime); double timeIncrement = TimingUtils.nanoTimeToSeconds( - evaluateTime - lastEvaluateStartTime); + evaluateTime - lastEvaluateStartTime); double RAMHoursIncrement = - learner.measureByteSize() / (1024.0 * 1024.0 * 1024.0); //GBs + learner.measureByteSize() / (1024.0 * 1024.0 * 1024.0); //GBs RAMHoursIncrement *= (timeIncrement / 3600.0); //Hours RAMHours += RAMHoursIncrement; lastEvaluateStartTime = evaluateTime; - - learningCurve.insertEntry(new LearningEvaluation( - new Measurement[]{ - new Measurement( - "learning evaluation instances", - instancesProcessed), - new Measurement( - "evaluation time (" - + (preciseCPUTiming ? "cpu " - : "") + "seconds)", - time), - new Measurement( - "model cost (RAM-Hours)", - RAMHours), - }, - evaluator, learner)); - - if (immediateResultStream != null) { + + learningCurve.insertEntry(new LearningEvaluation( + new Measurement[]{ + new Measurement( + "learning evaluation instances", + instancesProcessed), + new Measurement( + "evaluation time (" + + (preciseCPUTiming ? "cpu " + : "") + "seconds)", + time), + new Measurement( + "model cost (RAM-Hours)", + RAMHours), + }, + evaluator, learner)); + + if (immediateResultStream != null) { if (firstDump) { immediateResultStream.println(learningCurve.headerToString()); firstDump = false; @@ -225,57 +225,61 @@ protected Object doMainTask(TaskMonitor monitor, ObjectRepository repository) { immediateResultStream.println(learningCurve.entryToString(learningCurve.numEntries() - 1)); immediateResultStream.flush(); } - } - - // update monitor - if (instancesProcessed % INSTANCES_BETWEEN_MONITOR_UPDATES == 0 && learningCurve.numEntries() > 0) { - if (monitor.taskShouldAbort()) { + } + + // update monitor + if (instancesProcessed % INSTANCES_BETWEEN_MONITOR_UPDATES == 0 && learningCurve.numEntries() > 0) { + if (monitor.taskShouldAbort()) { return null; } - - long estimatedRemainingInstances = - stream.estimatedRemainingInstances(); - - if (maxInstances > 0) { - long maxRemaining = maxInstances - instancesProcessed; - if ((estimatedRemainingInstances < 0 || estimatedRemainingInstances == 0) - || (maxRemaining < estimatedRemainingInstances)) - { - estimatedRemainingInstances = maxRemaining; - } - } - - - // calculate completion fraction - double fractionComplete = (double) instancesProcessed / - (instancesProcessed + estimatedRemainingInstances); - monitor.setCurrentActivityFractionComplete( - estimatedRemainingInstances < 0 ? - -1.0 : fractionComplete); - - - // TODO currently the preview is sent after each instance - // should be changed later on - if (monitor.resultPreviewRequested() || isSubtask()) { - monitor.setLatestResultPreview(new PreviewCollectionLearningCurveWrapper((LearningCurve)learningCurve.copy(), this.getClass())); + + long estimatedRemainingInstances = + stream.estimatedRemainingInstances(); + + if (maxInstances > 0) { + long maxRemaining = maxInstances - instancesProcessed; + if ((estimatedRemainingInstances < 0 || estimatedRemainingInstances == 0) + || (maxRemaining < estimatedRemainingInstances)) + { + estimatedRemainingInstances = maxRemaining; + } + } + + + // calculate completion fraction + double fractionComplete = (double) instancesProcessed / + (instancesProcessed + estimatedRemainingInstances); + monitor.setCurrentActivityFractionComplete( + estimatedRemainingInstances < 0 ? + -1.0 : fractionComplete); + + + // TODO currently the preview is sent after each instance + // should be changed later on + if (monitor.resultPreviewRequested() || isSubtask()) { + monitor.setLatestResultPreview(new PreviewCollectionLearningCurveWrapper((LearningCurve)learningCurve.copy(), this.getClass())); } - - // update time measurement - secondsElapsed = (int) TimingUtils.nanoTimeToSeconds( - TimingUtils.getNanoCPUTimeOfCurrentThread() + + // update time measurement + secondsElapsed = (int) TimingUtils.nanoTimeToSeconds( + TimingUtils.getNanoCPUTimeOfCurrentThread() - evaluateStartTime); - } + } } if (immediateResultStream != null) { immediateResultStream.close(); } - - return new PreviewCollectionLearningCurveWrapper(learningCurve, this.getClass()); - } - - @Override - public List getSubtaskThreads() { - return new ArrayList(); - } + try { + evaluator.close(); + } catch (Exception ex) { + throw new RuntimeException("Exception closing evaluator", ex); + } + return new PreviewCollectionLearningCurveWrapper(learningCurve, this.getClass()); + } + + @Override + public List getSubtaskThreads() { + return new ArrayList(); + } }