use of org.apache.ignite.ml.trainers.AdaptableDatasetModel in project ignite by apache.
the class StackingTest method testSimpleStack.
/**
* Tests simple stack training.
*/
@Test
public void testSimpleStack() {
StackedDatasetTrainer<Vector, Vector, Double, LinearRegressionModel, Double> trainer = new StackedDatasetTrainer<>();
UpdatesStrategy<SmoothParametrized, SimpleGDParameterUpdate> updatesStgy = new UpdatesStrategy<>(new SimpleGDUpdateCalculator(0.2), SimpleGDParameterUpdate.SUM_LOCAL, SimpleGDParameterUpdate.AVG);
MLPArchitecture arch = new MLPArchitecture(2).withAddedLayer(10, true, Activators.RELU).withAddedLayer(1, false, Activators.SIGMOID);
MLPTrainer<SimpleGDParameterUpdate> trainer1 = new MLPTrainer<>(arch, LossFunctions.MSE, updatesStgy, 3000, 10, 50, 123L);
// Convert model trainer to produce Vector -> Vector model
DatasetTrainer<AdaptableDatasetModel<Vector, Vector, Matrix, Matrix, MultilayerPerceptron>, Double> mlpTrainer = AdaptableDatasetTrainer.of(trainer1).beforeTrainedModel((Vector v) -> new DenseMatrix(v.asArray(), 1)).afterTrainedModel((Matrix mtx) -> mtx.getRow(0)).withConvertedLabels(VectorUtils::num2Arr);
final double factor = 3;
StackedModel<Vector, Vector, Double, LinearRegressionModel> mdl = trainer.withAggregatorTrainer(new LinearRegressionLSQRTrainer().withConvertedLabels(x -> x * factor)).addTrainer(mlpTrainer).withAggregatorInputMerger(VectorUtils::concat).withSubmodelOutput2VectorConverter(IgniteFunction.identity()).withVector2SubmodelInputConverter(IgniteFunction.identity()).withOriginalFeaturesKept(IgniteFunction.identity()).withEnvironmentBuilder(TestUtils.testEnvBuilder()).fit(getCacheMock(xor), parts, new DoubleArrayVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.LAST));
assertEquals(0.0 * factor, mdl.predict(VectorUtils.of(0.0, 0.0)), 0.3);
assertEquals(1.0 * factor, mdl.predict(VectorUtils.of(0.0, 1.0)), 0.3);
assertEquals(1.0 * factor, mdl.predict(VectorUtils.of(1.0, 0.0)), 0.3);
assertEquals(0.0 * factor, mdl.predict(VectorUtils.of(1.0, 1.0)), 0.3);
}
use of org.apache.ignite.ml.trainers.AdaptableDatasetModel in project ignite by apache.
the class BaggingTest method testNaiveBaggingLogRegression.
/**
* Test that bagged log regression makes correct predictions.
*/
@Test
public void testNaiveBaggingLogRegression() {
Map<Integer, double[]> cacheMock = getCacheMock(twoLinearlySeparableClasses);
DatasetTrainer<LogisticRegressionModel, Double> trainer = new LogisticRegressionSGDTrainer().withUpdatesStgy(new UpdatesStrategy<>(new SimpleGDUpdateCalculator(0.2), SimpleGDParameterUpdate.SUM_LOCAL, SimpleGDParameterUpdate.AVG)).withMaxIterations(30000).withLocIterations(100).withBatchSize(10).withSeed(123L);
BaggedTrainer<Double> baggedTrainer = TrainerTransformers.makeBagged(trainer, 7, 0.7, 2, 2, new OnMajorityPredictionsAggregator()).withEnvironmentBuilder(TestUtils.testEnvBuilder());
BaggedModel mdl = baggedTrainer.fit(cacheMock, parts, new DoubleArrayVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.FIRST));
Vector weights = ((LogisticRegressionModel) ((AdaptableDatasetModel) ((ModelsParallelComposition) ((AdaptableDatasetModel) mdl.model()).innerModel()).submodels().get(0)).innerModel()).weights();
TestUtils.assertEquals(firstMdlWeights.get(parts), weights, 0.0);
TestUtils.assertEquals(0, mdl.predict(VectorUtils.of(100, 10)), PRECISION);
TestUtils.assertEquals(1, mdl.predict(VectorUtils.of(10, 100)), PRECISION);
}
Aggregations