IGNITE-8907: [ML] Using vectors in featureExtractor
[ignite.git] / modules / ml / src / test / java / org / apache / ignite / ml / tree / performance / DecisionTreeMNISTIntegrationTest.java
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.ignite.ml.tree.performance;
19
20 import java.io.IOException;
21 import org.apache.ignite.Ignite;
22 import org.apache.ignite.IgniteCache;
23 import org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction;
24 import org.apache.ignite.configuration.CacheConfiguration;
25 import org.apache.ignite.internal.util.IgniteUtils;
26 import org.apache.ignite.ml.math.VectorUtils;
27 import org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector;
28 import org.apache.ignite.ml.nn.performance.MnistMLPTestUtil;
29 import org.apache.ignite.ml.tree.DecisionTreeClassificationTrainer;
30 import org.apache.ignite.ml.tree.DecisionTreeNode;
31 import org.apache.ignite.ml.tree.impurity.util.SimpleStepFunctionCompressor;
32 import org.apache.ignite.ml.util.MnistUtils;
33 import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
34
35 /**
36 * Tests {@link DecisionTreeClassificationTrainer} on the MNIST dataset that require to start the whole Ignite
37 * infrastructure. For manual run.
38 */
39 public class DecisionTreeMNISTIntegrationTest extends GridCommonAbstractTest {
40 /** Number of nodes in grid */
41 private static final int NODE_COUNT = 3;
42
43 /** Ignite instance. */
44 private Ignite ignite;
45
46 /** {@inheritDoc} */
47 @Override protected void beforeTestsStarted() throws Exception {
48 for (int i = 1; i <= NODE_COUNT; i++)
49 startGrid(i);
50 }
51
52 /** {@inheritDoc} */
53 @Override protected void afterTestsStopped() {
54 stopAllGrids();
55 }
56
57 /**
58 * {@inheritDoc}
59 */
60 @Override protected void beforeTest() throws Exception {
61 /* Grid instance. */
62 ignite = grid(NODE_COUNT);
63 ignite.configuration().setPeerClassLoadingEnabled(true);
64 IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName());
65 }
66
67 /** Tests on the MNIST dataset. For manual run. */
68 public void testMNIST() throws IOException {
69 CacheConfiguration<Integer, MnistUtils.MnistLabeledImage> trainingSetCacheCfg = new CacheConfiguration<>();
70 trainingSetCacheCfg.setAffinity(new RendezvousAffinityFunction(false, 10));
71 trainingSetCacheCfg.setName("MNIST_TRAINING_SET");
72
73 IgniteCache<Integer, MnistUtils.MnistLabeledImage> trainingSet = ignite.createCache(trainingSetCacheCfg);
74
75 int i = 0;
76 for (MnistUtils.MnistLabeledImage e : MnistMLPTestUtil.loadTrainingSet(60_000))
77 trainingSet.put(i++, e);
78
79 DecisionTreeClassificationTrainer trainer = new DecisionTreeClassificationTrainer(
80 8,
81 0,
82 new SimpleStepFunctionCompressor<>());
83
84 DecisionTreeNode mdl = trainer.fit(
85 ignite,
86 trainingSet,
87 (k, v) -> VectorUtils.of(v.getPixels()),
88 (k, v) -> (double) v.getLabel()
89 );
90
91 int correctAnswers = 0;
92 int incorrectAnswers = 0;
93
94 for (MnistUtils.MnistLabeledImage e : MnistMLPTestUtil.loadTestSet(10_000)) {
95 double res = mdl.apply(new DenseLocalOnHeapVector(e.getPixels()));
96
97 if (res == e.getLabel())
98 correctAnswers++;
99 else
100 incorrectAnswers++;
101 }
102
103 double accuracy = 1.0 * correctAnswers / (correctAnswers + incorrectAnswers);
104
105 assertTrue(accuracy > 0.8);
106 }
107 }