MeanAbsValueConvergenceChecker extends ConvergenceChecker<K, V> { @Override public Double computeMeanErrorOnDataset( Dataset<EmptyContext, ? extends FeatureMatrixWithLabelsOnHeapData> dataset, ModelsComposition mdl) { IgniteBiTuple<Double, Long> sumAndCnt = dataset.compute( partition -> computeStatisticOnPartition(mdl, partition), this::reduce ); if (sumAndCnt == null || sumAndCnt.getValue() == 0) return Double.NaN; return sumAndCnt.getKey() / sumAndCnt.getValue(); } MeanAbsValueConvergenceChecker(long sampleSize, IgniteFunction<Double, Double> externalLbToInternalMapping,
        Loss loss, DatasetBuilder<K, V> datasetBuilder, Preprocessor<K, V> preprocessor, double precision); @Override Double computeMeanErrorOnDataset(
        Dataset<EmptyContext, ? extends FeatureMatrixWithLabelsOnHeapData> dataset,
        ModelsComposition mdl);  }
@Test public void testConvergenceChecking() { LocalDatasetBuilder<Integer, LabeledVector<Double>> datasetBuilder = new LocalDatasetBuilder<>(data, 1); ConvergenceChecker<Integer, LabeledVector<Double>> checker = createChecker( new MeanAbsValueConvergenceCheckerFactory(0.1), datasetBuilder); double error = checker.computeError(VectorUtils.of(1, 2), 4.0, notConvergedMdl); LearningEnvironmentBuilder envBuilder = TestUtils.testEnvBuilder(); Assert.assertEquals(1.9, error, 0.01); Assert.assertFalse(checker.isConverged(envBuilder, datasetBuilder, notConvergedMdl)); Assert.assertTrue(checker.isConverged(envBuilder, datasetBuilder, convergedMdl)); try (LocalDataset<EmptyContext, FeatureMatrixWithLabelsOnHeapData> dataset = datasetBuilder.build( envBuilder, new EmptyContextBuilder<>(), new FeatureMatrixWithLabelsOnHeapDataBuilder<>(vectorizer), envBuilder.buildForTrainer())) { double onDSError = checker.computeMeanErrorOnDataset(dataset, notConvergedMdl); Assert.assertEquals(1.55, onDSError, 0.01); } catch (Exception e) { throw new RuntimeException(e); } }
@Test public void testConvergenceCheckingWithAnomaliesInData() { data.put(666, VectorUtils.of(10, 11).labeled(100000.0)); LocalDatasetBuilder<Integer, LabeledVector<Double>> datasetBuilder = new LocalDatasetBuilder<>(data, 1); ConvergenceChecker<Integer, LabeledVector<Double>> checker = createChecker( new MeanAbsValueConvergenceCheckerFactory(0.1), datasetBuilder); try (LocalDataset<EmptyContext, FeatureMatrixWithLabelsOnHeapData> dataset = datasetBuilder.build( TestUtils.testEnvBuilder(), new EmptyContextBuilder<>(), new FeatureMatrixWithLabelsOnHeapDataBuilder<>(vectorizer), TestUtils.testEnvBuilder().buildForTrainer())) { double onDSError = checker.computeMeanErrorOnDataset(dataset, notConvergedMdl); Assert.assertEquals(9090.41, onDSError, 0.01); } catch (Exception e) { throw new RuntimeException(e); } }