Search in sources :

Example 1 with GDBRegressionOnTreesTrainer

use of org.apache.ignite.ml.tree.boosting.GDBRegressionOnTreesTrainer in project ignite by apache.

the class GDBOnTreesRegressionTrainerExample method main.

/**
 * Run example.
 *
 * @param args Command line arguments, none required.
 */
public static void main(String... args) {
    System.out.println();
    System.out.println(">>> GDB regression trainer example started.");
    // Start ignite grid.
    try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
        System.out.println(">>> Ignite grid started.");
        // Create cache with training data.
        CacheConfiguration<Integer, double[]> trainingSetCfg = createCacheConfiguration();
        IgniteCache<Integer, double[]> trainingSet = null;
        try {
            trainingSet = fillTrainingData(ignite, trainingSetCfg);
            // Create regression trainer.
            GDBTrainer trainer = new GDBRegressionOnTreesTrainer(1.0, 2000, 1, 0.).withCheckConvergenceStgyFactory(new MeanAbsValueConvergenceCheckerFactory(0.001));
            // Train decision tree model.
            GDBModel mdl = trainer.fit(ignite, trainingSet, new DoubleArrayVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.LAST));
            System.out.println(">>> ---------------------------------");
            System.out.println(">>> | Prediction\t| Valid answer \t|");
            System.out.println(">>> ---------------------------------");
            // Calculate score.
            for (int x = -5; x < 5; x++) {
                double predicted = mdl.predict(VectorUtils.of(x));
                System.out.printf(">>> | %.4f\t\t| %.4f\t\t|\n", predicted, Math.pow(x, 2));
            }
            System.out.println(">>> ---------------------------------");
            System.out.println(">>> GDB regression trainer example completed.");
        } finally {
            trainingSet.destroy();
        }
    } finally {
        System.out.flush();
    }
}
Also used : MeanAbsValueConvergenceCheckerFactory(org.apache.ignite.ml.composition.boosting.convergence.mean.MeanAbsValueConvergenceCheckerFactory) DoubleArrayVectorizer(org.apache.ignite.ml.dataset.feature.extractor.impl.DoubleArrayVectorizer) GDBRegressionOnTreesTrainer(org.apache.ignite.ml.tree.boosting.GDBRegressionOnTreesTrainer) GDBTrainer(org.apache.ignite.ml.composition.boosting.GDBTrainer) GDBModel(org.apache.ignite.ml.composition.boosting.GDBModel) Ignite(org.apache.ignite.Ignite)

Example 2 with GDBRegressionOnTreesTrainer

use of org.apache.ignite.ml.tree.boosting.GDBRegressionOnTreesTrainer in project ignite by apache.

the class GDBTrainerTest method testUpdate.

/**
 */
@Test
public void testUpdate() {
    int sampleSize = 100;
    double[] xs = new double[sampleSize];
    double[] ys = new double[sampleSize];
    for (int i = 0; i < sampleSize; i++) {
        xs[i] = i;
        ys[i] = ((int) (xs[i] / 10.0) % 2) == 0 ? -1.0 : 1.0;
    }
    Map<Integer, double[]> learningSample = new HashMap<>();
    for (int i = 0; i < sampleSize; i++) learningSample.put(i, new double[] { xs[i], ys[i] });
    IgniteBiFunction<Integer, double[], Vector> fExtr = (k, v) -> VectorUtils.of(v[0]);
    IgniteBiFunction<Integer, double[], Double> lExtr = (k, v) -> v[1];
    GDBTrainer classifTrainer = new GDBBinaryClassifierOnTreesTrainer(0.3, 500, 3, 0.0).withUsingIdx(true).withCheckConvergenceStgyFactory(new MeanAbsValueConvergenceCheckerFactory(0.3));
    GDBTrainer regressTrainer = new GDBRegressionOnTreesTrainer(0.3, 500, 3, 0.0).withUsingIdx(true).withCheckConvergenceStgyFactory(new MeanAbsValueConvergenceCheckerFactory(0.3));
// testUpdate(learningSample, fExtr, lExtr, classifTrainer);
// testUpdate(learningSample, fExtr, lExtr, regressTrainer);
}
Also used : TrainerTest(org.apache.ignite.ml.common.TrainerTest) WeightedPredictionsAggregator(org.apache.ignite.ml.composition.predictionsaggregator.WeightedPredictionsAggregator) BiFunction(java.util.function.BiFunction) Vector(org.apache.ignite.ml.math.primitives.vector.Vector) Assert.assertTrue(org.junit.Assert.assertTrue) HashMap(java.util.HashMap) Test(org.junit.Test) IgniteModel(org.apache.ignite.ml.IgniteModel) MeanAbsValueConvergenceCheckerFactory(org.apache.ignite.ml.composition.boosting.convergence.mean.MeanAbsValueConvergenceCheckerFactory) IgniteBiFunction(org.apache.ignite.ml.math.functions.IgniteBiFunction) ModelsComposition(org.apache.ignite.ml.composition.ModelsComposition) VectorUtils(org.apache.ignite.ml.math.primitives.vector.VectorUtils) GDBBinaryClassifierOnTreesTrainer(org.apache.ignite.ml.tree.boosting.GDBBinaryClassifierOnTreesTrainer) Map(java.util.Map) LocalDatasetBuilder(org.apache.ignite.ml.dataset.impl.local.LocalDatasetBuilder) DoubleArrayVectorizer(org.apache.ignite.ml.dataset.feature.extractor.impl.DoubleArrayVectorizer) GDBRegressionOnTreesTrainer(org.apache.ignite.ml.tree.boosting.GDBRegressionOnTreesTrainer) DecisionTreeModel(org.apache.ignite.ml.tree.DecisionTreeModel) ConvergenceCheckerStubFactory(org.apache.ignite.ml.composition.boosting.convergence.simple.ConvergenceCheckerStubFactory) Assert.assertEquals(org.junit.Assert.assertEquals) Vectorizer(org.apache.ignite.ml.dataset.feature.extractor.Vectorizer) GDBRegressionOnTreesTrainer(org.apache.ignite.ml.tree.boosting.GDBRegressionOnTreesTrainer) HashMap(java.util.HashMap) GDBBinaryClassifierOnTreesTrainer(org.apache.ignite.ml.tree.boosting.GDBBinaryClassifierOnTreesTrainer) MeanAbsValueConvergenceCheckerFactory(org.apache.ignite.ml.composition.boosting.convergence.mean.MeanAbsValueConvergenceCheckerFactory) Vector(org.apache.ignite.ml.math.primitives.vector.Vector) TrainerTest(org.apache.ignite.ml.common.TrainerTest) Test(org.junit.Test)

Example 3 with GDBRegressionOnTreesTrainer

use of org.apache.ignite.ml.tree.boosting.GDBRegressionOnTreesTrainer in project ignite by apache.

the class GDBOnTreesRegressionExportImportExample method main.

/**
 * Run example.
 *
 * @param args Command line arguments, none required.
 */
public static void main(String[] args) throws IOException {
    System.out.println();
    System.out.println(">>> GDB regression trainer example started.");
    // Start ignite grid.
    try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
        System.out.println(">>> Ignite grid started.");
        // Create cache with training data.
        CacheConfiguration<Integer, double[]> trainingSetCfg = createCacheConfiguration();
        IgniteCache<Integer, double[]> trainingSet = null;
        Path jsonMdlPath = null;
        try {
            trainingSet = fillTrainingData(ignite, trainingSetCfg);
            // Create regression trainer.
            GDBTrainer trainer = new GDBRegressionOnTreesTrainer(1.0, 2000, 1, 0.).withCheckConvergenceStgyFactory(new MeanAbsValueConvergenceCheckerFactory(0.001));
            // Train decision tree model.
            GDBModel mdl = trainer.fit(ignite, trainingSet, new DoubleArrayVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.LAST));
            System.out.println("\n>>> Exported GDB regression model: " + mdl.toString(true));
            predictOnGeneratedData(mdl);
            jsonMdlPath = Files.createTempFile(null, null);
            mdl.toJSON(jsonMdlPath);
            IgniteFunction<Double, Double> lbMapper = lb -> lb;
            GDBModel modelImportedFromJSON = GDBModel.fromJSON(jsonMdlPath).withLblMapping(lbMapper);
            System.out.println("\n>>> Imported GDB regression model: " + modelImportedFromJSON.toString(true));
            predictOnGeneratedData(modelImportedFromJSON);
            System.out.println(">>> GDB regression trainer example completed.");
        } finally {
            if (trainingSet != null)
                trainingSet.destroy();
            if (jsonMdlPath != null)
                Files.deleteIfExists(jsonMdlPath);
        }
    } finally {
        System.out.flush();
    }
}
Also used : Path(java.nio.file.Path) Files(java.nio.file.Files) IgniteFunction(org.apache.ignite.ml.math.functions.IgniteFunction) IOException(java.io.IOException) Ignite(org.apache.ignite.Ignite) IgniteCache(org.apache.ignite.IgniteCache) RendezvousAffinityFunction(org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction) MeanAbsValueConvergenceCheckerFactory(org.apache.ignite.ml.composition.boosting.convergence.mean.MeanAbsValueConvergenceCheckerFactory) GDBTrainer(org.apache.ignite.ml.composition.boosting.GDBTrainer) Ignition(org.apache.ignite.Ignition) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) VectorUtils(org.apache.ignite.ml.math.primitives.vector.VectorUtils) DoubleArrayVectorizer(org.apache.ignite.ml.dataset.feature.extractor.impl.DoubleArrayVectorizer) GDBModel(org.apache.ignite.ml.composition.boosting.GDBModel) GDBRegressionOnTreesTrainer(org.apache.ignite.ml.tree.boosting.GDBRegressionOnTreesTrainer) NotNull(org.jetbrains.annotations.NotNull) Path(java.nio.file.Path) Vectorizer(org.apache.ignite.ml.dataset.feature.extractor.Vectorizer) DoubleArrayVectorizer(org.apache.ignite.ml.dataset.feature.extractor.impl.DoubleArrayVectorizer) GDBRegressionOnTreesTrainer(org.apache.ignite.ml.tree.boosting.GDBRegressionOnTreesTrainer) GDBTrainer(org.apache.ignite.ml.composition.boosting.GDBTrainer) GDBModel(org.apache.ignite.ml.composition.boosting.GDBModel) MeanAbsValueConvergenceCheckerFactory(org.apache.ignite.ml.composition.boosting.convergence.mean.MeanAbsValueConvergenceCheckerFactory) Ignite(org.apache.ignite.Ignite)

Example 4 with GDBRegressionOnTreesTrainer

use of org.apache.ignite.ml.tree.boosting.GDBRegressionOnTreesTrainer in project ignite by apache.

the class GDBTrainerTest method testFitRegression.

/**
 */
@Test
public void testFitRegression() {
    int size = 100;
    double[] xs = new double[size];
    double[] ys = new double[size];
    double from = -5.0;
    double to = 5.0;
    double step = Math.abs(from - to) / size;
    Map<Integer, double[]> learningSample = new HashMap<>();
    for (int i = 0; i < size; i++) {
        xs[i] = from + step * i;
        ys[i] = 2 * xs[i];
        learningSample.put(i, new double[] { xs[i], ys[i] });
    }
    GDBTrainer trainer = new GDBRegressionOnTreesTrainer(1.0, 2000, 3, 0.0).withUsingIdx(true);
    IgniteModel<Vector, Double> mdl = trainer.fit(learningSample, 1, new DoubleArrayVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.LAST));
    double mse = 0.0;
    for (int j = 0; j < size; j++) {
        double x = xs[j];
        double y = ys[j];
        double p = mdl.predict(VectorUtils.of(x));
        mse += Math.pow(y - p, 2);
    }
    mse /= size;
    assertEquals(0.0, mse, 0.0001);
    ModelsComposition composition = (ModelsComposition) mdl;
    assertTrue(!composition.toString().isEmpty());
    assertTrue(!composition.toString(true).isEmpty());
    assertTrue(!composition.toString(false).isEmpty());
    composition.getModels().forEach(m -> assertTrue(m instanceof DecisionTreeModel));
    assertEquals(2000, composition.getModels().size());
    assertTrue(composition.getPredictionsAggregator() instanceof WeightedPredictionsAggregator);
    trainer = trainer.withCheckConvergenceStgyFactory(new MeanAbsValueConvergenceCheckerFactory(0.1));
    assertTrue(trainer.fit(learningSample, 1, new DoubleArrayVectorizer<Integer>().labeled(1)).getModels().size() < 2000);
}
Also used : DoubleArrayVectorizer(org.apache.ignite.ml.dataset.feature.extractor.impl.DoubleArrayVectorizer) GDBRegressionOnTreesTrainer(org.apache.ignite.ml.tree.boosting.GDBRegressionOnTreesTrainer) HashMap(java.util.HashMap) DecisionTreeModel(org.apache.ignite.ml.tree.DecisionTreeModel) WeightedPredictionsAggregator(org.apache.ignite.ml.composition.predictionsaggregator.WeightedPredictionsAggregator) ModelsComposition(org.apache.ignite.ml.composition.ModelsComposition) MeanAbsValueConvergenceCheckerFactory(org.apache.ignite.ml.composition.boosting.convergence.mean.MeanAbsValueConvergenceCheckerFactory) Vector(org.apache.ignite.ml.math.primitives.vector.Vector) TrainerTest(org.apache.ignite.ml.common.TrainerTest) Test(org.junit.Test)

Aggregations

MeanAbsValueConvergenceCheckerFactory (org.apache.ignite.ml.composition.boosting.convergence.mean.MeanAbsValueConvergenceCheckerFactory)4 DoubleArrayVectorizer (org.apache.ignite.ml.dataset.feature.extractor.impl.DoubleArrayVectorizer)4 GDBRegressionOnTreesTrainer (org.apache.ignite.ml.tree.boosting.GDBRegressionOnTreesTrainer)4 HashMap (java.util.HashMap)2 Ignite (org.apache.ignite.Ignite)2 TrainerTest (org.apache.ignite.ml.common.TrainerTest)2 ModelsComposition (org.apache.ignite.ml.composition.ModelsComposition)2 GDBModel (org.apache.ignite.ml.composition.boosting.GDBModel)2 GDBTrainer (org.apache.ignite.ml.composition.boosting.GDBTrainer)2 WeightedPredictionsAggregator (org.apache.ignite.ml.composition.predictionsaggregator.WeightedPredictionsAggregator)2 Vectorizer (org.apache.ignite.ml.dataset.feature.extractor.Vectorizer)2 Vector (org.apache.ignite.ml.math.primitives.vector.Vector)2 VectorUtils (org.apache.ignite.ml.math.primitives.vector.VectorUtils)2 DecisionTreeModel (org.apache.ignite.ml.tree.DecisionTreeModel)2 Test (org.junit.Test)2 IOException (java.io.IOException)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 Map (java.util.Map)1 BiFunction (java.util.function.BiFunction)1