use of org.apache.ignite.ml.composition.ModelsComposition in project ignite by apache.
the class GDBOnTreesLearningStrategy method update.
/**
* {@inheritDoc}
*/
@Override
public <K, V> List<IgniteModel<Vector, Double>> update(GDBModel mdlToUpdate, DatasetBuilder<K, V> datasetBuilder, Preprocessor<K, V> vectorizer) {
LearningEnvironment environment = envBuilder.buildForTrainer();
environment.initDeployingContext(vectorizer);
DatasetTrainer<? extends IgniteModel<Vector, Double>, Double> trainer = baseMdlTrainerBuilder.get();
assert trainer instanceof DecisionTreeTrainer;
DecisionTreeTrainer decisionTreeTrainer = (DecisionTreeTrainer) trainer;
List<IgniteModel<Vector, Double>> models = initLearningState(mdlToUpdate);
ConvergenceChecker<K, V> convCheck = checkConvergenceStgyFactory.create(sampleSize, externalLbToInternalMapping, loss, datasetBuilder, vectorizer);
try (Dataset<EmptyContext, DecisionTreeData> dataset = datasetBuilder.build(envBuilder, new EmptyContextBuilder<>(), new DecisionTreeDataBuilder<>(vectorizer, useIdx), environment)) {
for (int i = 0; i < cntOfIterations; i++) {
double[] weights = Arrays.copyOf(compositionWeights, models.size());
WeightedPredictionsAggregator aggregator = new WeightedPredictionsAggregator(weights, meanLbVal);
ModelsComposition currComposition = new ModelsComposition(models, aggregator);
if (convCheck.isConverged(dataset, currComposition))
break;
dataset.compute(part -> {
if (part.getCopiedOriginalLabels() == null)
part.setCopiedOriginalLabels(Arrays.copyOf(part.getLabels(), part.getLabels().length));
for (int j = 0; j < part.getLabels().length; j++) {
double mdlAnswer = currComposition.predict(VectorUtils.of(part.getFeatures()[j]));
double originalLbVal = externalLbToInternalMapping.apply(part.getCopiedOriginalLabels()[j]);
part.getLabels()[j] = -loss.gradient(sampleSize, originalLbVal, mdlAnswer);
}
});
long startTs = System.currentTimeMillis();
models.add(decisionTreeTrainer.fit(dataset));
double learningTime = (double) (System.currentTimeMillis() - startTs) / 1000.0;
trainerEnvironment.logger(getClass()).log(MLLogger.VerboseLevel.LOW, "One model training time was %.2fs", learningTime);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
compositionWeights = Arrays.copyOf(compositionWeights, models.size());
return models;
}
use of org.apache.ignite.ml.composition.ModelsComposition 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);
}
Aggregations