YuriBabak commented on a change in pull request #5871: IGNITE-10700: [ML] 
Working with Binary Objects
URL: https://github.com/apache/ignite/pull/5871#discussion_r249417451
 
 

 ##########
 File path: 
modules/ml/src/test/java/org/apache/ignite/ml/common/KeepBinaryTest.java
 ##########
 @@ -0,0 +1,120 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.ml.common;
+
+import java.util.Random;
+import java.util.UUID;
+import org.apache.ignite.Ignite;
+import org.apache.ignite.IgniteCache;
+import org.apache.ignite.binary.BinaryObject;
+import org.apache.ignite.binary.BinaryObjectBuilder;
+import org.apache.ignite.configuration.CacheConfiguration;
+import org.apache.ignite.internal.util.IgniteUtils;
+import org.apache.ignite.ml.clustering.kmeans.KMeansModel;
+import org.apache.ignite.ml.clustering.kmeans.KMeansTrainer;
+import org.apache.ignite.ml.math.functions.IgniteBiFunction;
+import org.apache.ignite.ml.math.primitives.vector.Vector;
+import org.apache.ignite.ml.math.primitives.vector.VectorUtils;
+import org.apache.ignite.ml.preprocessing.normalization.NormalizationTrainer;
+import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+/**
+ * Test for IGNITE-10700.
+ */
+@RunWith(JUnit4.class)
+public class KeepBinaryTest extends GridCommonAbstractTest {
+    /** Number of nodes in grid. */
+    private static final int NODE_COUNT = 2;
+    /** Number of samples. */
+    public static final int NUMBER_OF_SAMPLES = 1000;
+    /** Half of samples. */
+    public static final int HALF = NUMBER_OF_SAMPLES / 2;
+
+    /** Ignite instance. */
+    private Ignite ignite;
+
+    /** {@inheritDoc} */
+    @Override protected void beforeTestsStarted() throws Exception {
+        for (int i = 1; i <= NODE_COUNT; i++)
+            startGrid(i);
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void afterTestsStopped() {
+        stopAllGrids();
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void beforeTest() {
+        /* Grid instance. */
+        ignite = grid(NODE_COUNT);
+        ignite.configuration().setPeerClassLoadingEnabled(true);
+        
IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName());
+    }
+
+    /**
+     * Startup Ignite, populate cache and train some model.
+     */
+    @Test
+    public void test() {
+        IgniteCache<Integer, BinaryObject> dataCache = populateCache(ignite);
+
+        IgniteBiFunction<Integer, BinaryObject, Vector> featureExtractor
+            = (k, v) -> VectorUtils.of(new double[]{v.field("feat1")});
+        IgniteBiFunction<Integer, BinaryObject, Double> lbExtractor = (k, v) 
-> (double) v.field("label");
+
+        IgniteBiFunction<Integer, BinaryObject, Vector> 
normalizationPreprocessor = new NormalizationTrainer<Integer, BinaryObject>()
+            .withP(1)
+            .fit(
+                ignite,
+                dataCache,
+                featureExtractor
+            );
+
+        KMeansTrainer trainer = new KMeansTrainer();
+
+        KMeansModel kmdl = trainer.fit(ignite, dataCache, 
normalizationPreprocessor, lbExtractor);
+
+        assertTrue(kmdl.predict(VectorUtils.num2Vec(0.0)) == 0.0);
+    }
+
+    /**
+     * Populate cache with binary objects.
+     */
+    private IgniteCache<Integer, BinaryObject> populateCache(Ignite ignite) {
+        CacheConfiguration<Integer, BinaryObject> cacheConfiguration = new 
CacheConfiguration<>();
+        cacheConfiguration.setName("TEST_" + UUID.randomUUID());
+
+        IgniteCache<Integer, BinaryObject> cache = 
ignite.createCache(cacheConfiguration).withKeepBinary();
+
+        BinaryObjectBuilder builder = ignite.binary().builder("testType");
+
+        for (int i = 0; i < NUMBER_OF_SAMPLES; i++) {
+            BinaryObject value = builder.setField("label", (i < HALF)? 0.0 : 
1.0)
+                .setField("feat1", new Random().nextDouble())
 
 Review comment:
   fixed in next commit

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to