use of org.apache.ignite.ml.composition.boosting.convergence.mean.MeanAbsValueConvergenceCheckerFactory in project ignite by apache.
the class GDBOnTreesClassificationExportImportExample 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 classification trainer example started.");
// Start ignite grid.
try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
System.out.println("\n>>> 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 classification trainer.
GDBTrainer trainer = new GDBBinaryClassifierOnTreesTrainer(1.0, 300, 2, 0.).withCheckConvergenceStgyFactory(new MeanAbsValueConvergenceCheckerFactory(0.1));
// Train decision tree model.
GDBModel mdl = trainer.fit(ignite, trainingSet, new DoubleArrayVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.LAST));
System.out.println("\n>>> Exported GDB classification model: " + mdl.toString(true));
predictOnGeneratedData(mdl);
jsonMdlPath = Files.createTempFile(null, null);
mdl.toJSON(jsonMdlPath);
IgniteFunction<Double, Double> lbMapper = lb -> lb > 0.5 ? 1.0 : 0.0;
GDBModel modelImportedFromJSON = GDBModel.fromJSON(jsonMdlPath).withLblMapping(lbMapper);
System.out.println("\n>>> Imported GDB classification model: " + modelImportedFromJSON.toString(true));
predictOnGeneratedData(modelImportedFromJSON);
System.out.println(">>> GDB classification trainer example completed.");
} finally {
if (trainingSet != null)
trainingSet.destroy();
if (jsonMdlPath != null)
Files.deleteIfExists(jsonMdlPath);
}
} finally {
System.out.flush();
}
}
use of org.apache.ignite.ml.composition.boosting.convergence.mean.MeanAbsValueConvergenceCheckerFactory 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();
}
}
use of org.apache.ignite.ml.composition.boosting.convergence.mean.MeanAbsValueConvergenceCheckerFactory in project ignite by apache.
the class GDBOnTreesClassificationTrainerExample method main.
/**
* Run example.
*
* @param args Command line arguments, none required.
*/
public static void main(String... args) {
System.out.println();
System.out.println(">>> GDB classification 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 classification trainer.
GDBTrainer trainer = new GDBBinaryClassifierOnTreesTrainer(1.0, 300, 2, 0.).withCheckConvergenceStgyFactory(new MeanAbsValueConvergenceCheckerFactory(0.1));
// 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.sin(x) < 0 ? 0.0 : 1.0);
}
System.out.println(">>> ---------------------------------");
System.out.println(">>> Count of trees = " + mdl.getModels().size());
System.out.println(">>> ---------------------------------");
System.out.println(">>> GDB classification trainer example completed.");
} finally {
trainingSet.destroy();
}
} finally {
System.out.flush();
}
}
use of org.apache.ignite.ml.composition.boosting.convergence.mean.MeanAbsValueConvergenceCheckerFactory in project ignite by apache.
the class GDBTrainerTest method testClassifier.
/**
*/
private void testClassifier(BiFunction<GDBTrainer, Map<Integer, double[]>, IgniteModel<Vector, Double>> fitter) {
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] });
GDBTrainer trainer = new GDBBinaryClassifierOnTreesTrainer(0.3, 500, 3, 0.0).withUsingIdx(true).withCheckConvergenceStgyFactory(new MeanAbsValueConvergenceCheckerFactory(0.3));
IgniteModel<Vector, Double> mdl = fitter.apply(trainer, learningSample);
int errorsCnt = 0;
for (int j = 0; j < sampleSize; j++) {
double x = xs[j];
double y = ys[j];
double p = mdl.predict(VectorUtils.of(x));
if (p != y)
errorsCnt++;
}
assertEquals(0, errorsCnt);
assertTrue(mdl instanceof ModelsComposition);
ModelsComposition composition = (ModelsComposition) mdl;
composition.getModels().forEach(m -> assertTrue(m instanceof DecisionTreeModel));
assertTrue(composition.getModels().size() < 500);
assertTrue(composition.getPredictionsAggregator() instanceof WeightedPredictionsAggregator);
trainer = trainer.withCheckConvergenceStgyFactory(new ConvergenceCheckerStubFactory());
assertEquals(500, ((ModelsComposition) fitter.apply(trainer, learningSample)).getModels().size());
}
use of org.apache.ignite.ml.composition.boosting.convergence.mean.MeanAbsValueConvergenceCheckerFactory 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);
}
Aggregations