Search in sources :

Example 16 with SimpleGDUpdateCalculator

use of org.apache.ignite.ml.optimization.updatecalculators.SimpleGDUpdateCalculator 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);
    }
}
Also used : LogisticRegressionSGDTrainer(org.apache.ignite.ml.regressions.logistic.LogisticRegressionSGDTrainer) HashMap(java.util.HashMap) LogisticRegressionModel(org.apache.ignite.ml.regressions.logistic.LogisticRegressionModel) SimpleGDUpdateCalculator(org.apache.ignite.ml.optimization.updatecalculators.SimpleGDUpdateCalculator) Vector(org.apache.ignite.ml.math.primitives.vector.Vector) TrainerTest(org.apache.ignite.ml.common.TrainerTest) Test(org.junit.Test)

Aggregations

SimpleGDUpdateCalculator (org.apache.ignite.ml.optimization.updatecalculators.SimpleGDUpdateCalculator)16 LogisticRegressionSGDTrainer (org.apache.ignite.ml.regressions.logistic.LogisticRegressionSGDTrainer)12 LogisticRegressionModel (org.apache.ignite.ml.regressions.logistic.LogisticRegressionModel)11 Vector (org.apache.ignite.ml.math.primitives.vector.Vector)10 Test (org.junit.Test)10 HashMap (java.util.HashMap)7 Ignite (org.apache.ignite.Ignite)6 TrainerTest (org.apache.ignite.ml.common.TrainerTest)6 DoubleArrayVectorizer (org.apache.ignite.ml.dataset.feature.extractor.impl.DoubleArrayVectorizer)5 SandboxMLCache (org.apache.ignite.examples.ml.util.SandboxMLCache)3 MLPArchitecture (org.apache.ignite.ml.nn.architecture.MLPArchitecture)3 SimpleGDParameterUpdate (org.apache.ignite.ml.optimization.updatecalculators.SimpleGDParameterUpdate)3 ParamGrid (org.apache.ignite.ml.selection.paramgrid.ParamGrid)3 FileNotFoundException (java.io.FileNotFoundException)2 OnMajorityPredictionsAggregator (org.apache.ignite.ml.composition.predictionsaggregator.OnMajorityPredictionsAggregator)2 StackedVectorDatasetTrainer (org.apache.ignite.ml.composition.stacking.StackedVectorDatasetTrainer)2 Matrix (org.apache.ignite.ml.math.primitives.matrix.Matrix)2 DenseMatrix (org.apache.ignite.ml.math.primitives.matrix.impl.DenseMatrix)2 VectorUtils (org.apache.ignite.ml.math.primitives.vector.VectorUtils)2 MLPTrainer (org.apache.ignite.ml.nn.MLPTrainer)2