use of org.apache.ignite.ml.regressions.logistic.LogisticRegressionSGDTrainer in project ignite by apache.
the class CrossValidationTest method testRandomSearch.
/**
*/
@Test
public void testRandomSearch() {
Map<Integer, double[]> data = new HashMap<>();
for (int i = 0; i < twoLinearlySeparableClasses.length; i++) data.put(i, twoLinearlySeparableClasses[i]);
LogisticRegressionSGDTrainer trainer = new LogisticRegressionSGDTrainer().withUpdatesStgy(new UpdatesStrategy<>(new SimpleGDUpdateCalculator(0.2), SimpleGDParameterUpdate.SUM_LOCAL, SimpleGDParameterUpdate.AVG)).withMaxIterations(100000).withLocIterations(100).withBatchSize(14).withSeed(123L);
Vectorizer<Integer, double[], Integer, Double> vectorizer = new DoubleArrayVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.FIRST);
ParamGrid paramGrid = new ParamGrid().withParameterSearchStrategy(new RandomStrategy().withMaxTries(10).withSeed(1234L).withSatisfactoryFitness(0.9)).addHyperParam("maxIterations", trainer::withMaxIterations, new Double[] { 10.0, 100.0, 1000.0, 10000.0 }).addHyperParam("locIterations", trainer::withLocIterations, new Double[] { 10.0, 100.0, 1000.0, 10000.0 }).addHyperParam("batchSize", trainer::withBatchSize, new Double[] { 1.0, 2.0, 4.0, 8.0, 16.0 });
DebugCrossValidation<LogisticRegressionModel, Integer, double[]> scoreCalculator = (DebugCrossValidation<LogisticRegressionModel, Integer, double[]>) new DebugCrossValidation<LogisticRegressionModel, Integer, double[]>().withUpstreamMap(data).withAmountOfParts(1).withTrainer(trainer).withMetric(MetricName.ACCURACY).withPreprocessor(vectorizer).withAmountOfFolds(4).isRunningOnPipeline(false).withParamGrid(paramGrid);
CrossValidationResult crossValidationRes = scoreCalculator.tuneHyperParameters();
assertEquals(crossValidationRes.getBestAvgScore(), 0.9343858500738256, 1e-6);
assertEquals(crossValidationRes.getScoringBoard().size(), 10);
}
use of org.apache.ignite.ml.regressions.logistic.LogisticRegressionSGDTrainer in project ignite by apache.
the class OneVsRestTrainerTest method testTrainWithTheLinearlySeparableCase.
/**
* Test trainer on 2 linearly separable sets.
*/
@Test
public void testTrainWithTheLinearlySeparableCase() {
Map<Integer, double[]> cacheMock = new HashMap<>();
for (int i = 0; i < twoLinearlySeparableClasses.length; i++) cacheMock.put(i, twoLinearlySeparableClasses[i]);
LogisticRegressionSGDTrainer binaryTrainer = new LogisticRegressionSGDTrainer().withUpdatesStgy(new UpdatesStrategy<>(new SimpleGDUpdateCalculator(0.2), SimpleGDParameterUpdate.SUM_LOCAL, SimpleGDParameterUpdate.AVG)).withMaxIterations(1000).withLocIterations(10).withBatchSize(100).withSeed(123L);
OneVsRestTrainer<LogisticRegressionModel> trainer = new OneVsRestTrainer<>(binaryTrainer);
MultiClassModel mdl = trainer.fit(cacheMock, parts, new DoubleArrayVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.FIRST));
Assert.assertTrue(!mdl.toString().isEmpty());
Assert.assertTrue(!mdl.toString(true).isEmpty());
Assert.assertTrue(!mdl.toString(false).isEmpty());
TestUtils.assertEquals(1, mdl.predict(VectorUtils.of(-100, 0)), PRECISION);
TestUtils.assertEquals(0, mdl.predict(VectorUtils.of(100, 0)), PRECISION);
}
use of org.apache.ignite.ml.regressions.logistic.LogisticRegressionSGDTrainer in project ignite by apache.
the class OneVsRestTrainerTest method testUpdate.
/**
*/
@Test
public void testUpdate() {
Map<Integer, double[]> cacheMock = new HashMap<>();
for (int i = 0; i < twoLinearlySeparableClasses.length; i++) cacheMock.put(i, twoLinearlySeparableClasses[i]);
LogisticRegressionSGDTrainer binaryTrainer = new LogisticRegressionSGDTrainer().withUpdatesStgy(new UpdatesStrategy<>(new SimpleGDUpdateCalculator(0.2), SimpleGDParameterUpdate.SUM_LOCAL, SimpleGDParameterUpdate.AVG)).withMaxIterations(1000).withLocIterations(10).withBatchSize(100).withSeed(123L);
OneVsRestTrainer<LogisticRegressionModel> trainer = new OneVsRestTrainer<>(binaryTrainer);
Vectorizer<Integer, double[], Integer, Double> vectorizer = new DoubleArrayVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.FIRST);
MultiClassModel originalMdl = trainer.fit(cacheMock, parts, vectorizer);
MultiClassModel updatedOnSameDS = trainer.update(originalMdl, cacheMock, parts, vectorizer);
MultiClassModel updatedOnEmptyDS = trainer.update(originalMdl, new HashMap<>(), parts, vectorizer);
List<Vector> vectors = Arrays.asList(VectorUtils.of(-100, 0), VectorUtils.of(100, 0));
for (Vector vec : vectors) {
TestUtils.assertEquals(originalMdl.predict(vec), updatedOnSameDS.predict(vec), PRECISION);
TestUtils.assertEquals(originalMdl.predict(vec), updatedOnEmptyDS.predict(vec), PRECISION);
}
}
use of org.apache.ignite.ml.regressions.logistic.LogisticRegressionSGDTrainer in project ignite by apache.
the class MLDeployingTest method fitAndTestModel.
/**
*/
private void fitAndTestModel(CacheBasedDatasetBuilder<Integer, Vector> datasetBuilder, Preprocessor<Integer, Vector> preprocessor) {
LogisticRegressionSGDTrainer trainer = new LogisticRegressionSGDTrainer();
LogisticRegressionModel mdl = trainer.fit(datasetBuilder, preprocessor);
// For this case any answer is valid.
assertEquals(0., mdl.predict(VectorUtils.of(0., 0.)), 1.);
}
Aggregations