IGNITE-8907: [ML] Using vectors in featureExtractor
[ignite.git] / examples / src / main / java / org / apache / ignite / examples / ml / regression / linear / LinearRegressionSGDTrainerExample.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.examples.ml.regression.linear;
19
20 import java.util.Arrays;
21 import java.util.UUID;
22 import javax.cache.Cache;
23 import org.apache.ignite.Ignite;
24 import org.apache.ignite.IgniteCache;
25 import org.apache.ignite.Ignition;
26 import org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction;
27 import org.apache.ignite.cache.query.QueryCursor;
28 import org.apache.ignite.cache.query.ScanQuery;
29 import org.apache.ignite.configuration.CacheConfiguration;
30 import org.apache.ignite.ml.math.VectorUtils;
31 import org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector;
32 import org.apache.ignite.ml.nn.UpdatesStrategy;
33 import org.apache.ignite.ml.optimization.updatecalculators.RPropParameterUpdate;
34 import org.apache.ignite.ml.optimization.updatecalculators.RPropUpdateCalculator;
35 import org.apache.ignite.ml.regressions.linear.LinearRegressionModel;
36 import org.apache.ignite.ml.regressions.linear.LinearRegressionSGDTrainer;
37 import org.apache.ignite.thread.IgniteThread;
38
39 /**
40 * Run linear regression model over cached dataset.
41 *
42 * @see LinearRegressionSGDTrainer
43 */
44 public class LinearRegressionSGDTrainerExample {
45 /** */
46 private static final double[][] data = {
47 {8, 78, 284, 9.100000381, 109},
48 {9.300000191, 68, 433, 8.699999809, 144},
49 {7.5, 70, 739, 7.199999809, 113},
50 {8.899999619, 96, 1792, 8.899999619, 97},
51 {10.19999981, 74, 477, 8.300000191, 206},
52 {8.300000191, 111, 362, 10.89999962, 124},
53 {8.800000191, 77, 671, 10, 152},
54 {8.800000191, 168, 636, 9.100000381, 162},
55 {10.69999981, 82, 329, 8.699999809, 150},
56 {11.69999981, 89, 634, 7.599999905, 134},
57 {8.5, 149, 631, 10.80000019, 292},
58 {8.300000191, 60, 257, 9.5, 108},
59 {8.199999809, 96, 284, 8.800000191, 111},
60 {7.900000095, 83, 603, 9.5, 182},
61 {10.30000019, 130, 686, 8.699999809, 129},
62 {7.400000095, 145, 345, 11.19999981, 158},
63 {9.600000381, 112, 1357, 9.699999809, 186},
64 {9.300000191, 131, 544, 9.600000381, 177},
65 {10.60000038, 80, 205, 9.100000381, 127},
66 {9.699999809, 130, 1264, 9.199999809, 179},
67 {11.60000038, 140, 688, 8.300000191, 80},
68 {8.100000381, 154, 354, 8.399999619, 103},
69 {9.800000191, 118, 1632, 9.399999619, 101},
70 {7.400000095, 94, 348, 9.800000191, 117},
71 {9.399999619, 119, 370, 10.39999962, 88},
72 {11.19999981, 153, 648, 9.899999619, 78},
73 {9.100000381, 116, 366, 9.199999809, 102},
74 {10.5, 97, 540, 10.30000019, 95},
75 {11.89999962, 176, 680, 8.899999619, 80},
76 {8.399999619, 75, 345, 9.600000381, 92},
77 {5, 134, 525, 10.30000019, 126},
78 {9.800000191, 161, 870, 10.39999962, 108},
79 {9.800000191, 111, 669, 9.699999809, 77},
80 {10.80000019, 114, 452, 9.600000381, 60},
81 {10.10000038, 142, 430, 10.69999981, 71},
82 {10.89999962, 238, 822, 10.30000019, 86},
83 {9.199999809, 78, 190, 10.69999981, 93},
84 {8.300000191, 196, 867, 9.600000381, 106},
85 {7.300000191, 125, 969, 10.5, 162},
86 {9.399999619, 82, 499, 7.699999809, 95},
87 {9.399999619, 125, 925, 10.19999981, 91},
88 {9.800000191, 129, 353, 9.899999619, 52},
89 {3.599999905, 84, 288, 8.399999619, 110},
90 {8.399999619, 183, 718, 10.39999962, 69},
91 {10.80000019, 119, 540, 9.199999809, 57},
92 {10.10000038, 180, 668, 13, 106},
93 {9, 82, 347, 8.800000191, 40},
94 {10, 71, 345, 9.199999809, 50},
95 {11.30000019, 118, 463, 7.800000191, 35},
96 {11.30000019, 121, 728, 8.199999809, 86},
97 {12.80000019, 68, 383, 7.400000095, 57},
98 {10, 112, 316, 10.39999962, 57},
99 {6.699999809, 109, 388, 8.899999619, 94}
100 };
101
102 /** Run example. */
103 public static void main(String[] args) throws InterruptedException {
104 System.out.println();
105 System.out.println(">>> Linear regression model over sparse distributed matrix API usage example started.");
106 // Start ignite grid.
107 try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
108 System.out.println(">>> Ignite grid started.");
109 IgniteThread igniteThread = new IgniteThread(ignite.configuration().getIgniteInstanceName(),
110 LinearRegressionSGDTrainerExample.class.getSimpleName(), () -> {
111
112 IgniteCache<Integer, double[]> dataCache = getTestCache(ignite);
113
114 System.out.println(">>> Create new linear regression trainer object.");
115 LinearRegressionSGDTrainer<?> trainer = new LinearRegressionSGDTrainer<>(new UpdatesStrategy<>(
116 new RPropUpdateCalculator(),
117 RPropParameterUpdate::sumLocal,
118 RPropParameterUpdate::avg
119 ), 100000, 10, 100, 123L);
120
121 System.out.println(">>> Perform the training to get the model.");
122 LinearRegressionModel mdl = trainer.fit(
123 ignite,
124 dataCache,
125 (k, v) -> VectorUtils.of(Arrays.copyOfRange(v, 1, v.length)),
126 (k, v) -> v[0]
127 );
128
129 System.out.println(">>> Linear regression model: " + mdl);
130
131 System.out.println(">>> ---------------------------------");
132 System.out.println(">>> | Prediction\t| Ground Truth\t|");
133 System.out.println(">>> ---------------------------------");
134
135 try (QueryCursor<Cache.Entry<Integer, double[]>> observations = dataCache.query(new ScanQuery<>())) {
136 for (Cache.Entry<Integer, double[]> observation : observations) {
137 double[] val = observation.getValue();
138 double[] inputs = Arrays.copyOfRange(val, 1, val.length);
139 double groundTruth = val[0];
140
141 double prediction = mdl.apply(new DenseLocalOnHeapVector(inputs));
142
143 System.out.printf(">>> | %.4f\t\t| %.4f\t\t|\n", prediction, groundTruth);
144 }
145 }
146
147 System.out.println(">>> ---------------------------------");
148 });
149
150 igniteThread.start();
151
152 igniteThread.join();
153 }
154 }
155
156 /**
157 * Fills cache with data and returns it.
158 *
159 * @param ignite Ignite instance.
160 * @return Filled Ignite Cache.
161 */
162 private static IgniteCache<Integer, double[]> getTestCache(Ignite ignite) {
163 CacheConfiguration<Integer, double[]> cacheConfiguration = new CacheConfiguration<>();
164 cacheConfiguration.setName("TEST_" + UUID.randomUUID());
165 cacheConfiguration.setAffinity(new RendezvousAffinityFunction(false, 10));
166
167 IgniteCache<Integer, double[]> cache = ignite.createCache(cacheConfiguration);
168
169 for (int i = 0; i < data.length; i++)
170 cache.put(i, data[i]);
171
172 return cache;
173 }
174 }