weibozhao commented on code in PR #83:
URL: https://github.com/apache/flink-ml/pull/83#discussion_r868785218


##########
flink-ml-lib/src/test/java/org/apache/flink/ml/classification/OnlineLogisticRegressionTest.java:
##########
@@ -0,0 +1,444 @@
+/*
+ * 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.flink.ml.classification;
+
+import org.apache.flink.api.common.functions.MapFunction;
+import org.apache.flink.api.common.restartstrategy.RestartStrategies;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.api.common.typeinfo.Types;
+import org.apache.flink.api.java.tuple.Tuple2;
+import org.apache.flink.api.java.typeutils.RowTypeInfo;
+import org.apache.flink.configuration.Configuration;
+import 
org.apache.flink.ml.classification.logisticregression.LogisticRegression;
+import 
org.apache.flink.ml.classification.logisticregression.OnlineLogisticRegression;
+import 
org.apache.flink.ml.classification.logisticregression.OnlineLogisticRegressionModel;
+import org.apache.flink.ml.feature.MinMaxScalerTest;
+import org.apache.flink.ml.linalg.DenseVector;
+import org.apache.flink.ml.linalg.SparseVector;
+import org.apache.flink.ml.linalg.Vectors;
+import org.apache.flink.ml.util.StageTestUtils;
+import org.apache.flink.streaming.api.datastream.DataStream;
+import 
org.apache.flink.streaming.api.environment.ExecutionCheckpointingOptions;
+import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
+import org.apache.flink.streaming.api.functions.source.SourceFunction;
+import org.apache.flink.table.api.Table;
+import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
+import org.apache.flink.table.api.internal.TableImpl;
+import org.apache.flink.test.util.AbstractTestBase;
+import org.apache.flink.types.Row;
+
+import org.apache.commons.collections.IteratorUtils;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import static junit.framework.TestCase.assertEquals;
+
+/** Tests {@link OnlineLogisticRegression} and {@link 
OnlineLogisticRegressionModel}. */
+public class OnlineLogisticRegressionTest extends AbstractTestBase {
+    @Rule public final TemporaryFolder tempFolder = new TemporaryFolder();
+    private StreamExecutionEnvironment env;
+    private StreamTableEnvironment tEnv;
+    private Table trainDenseTable;
+    private static final String LABEL_COL = "label";
+    private static final String PREDICT_COL = "prediction";
+    private static final String FEATURE_COL = "features";
+    private static final String MODEL_VERSION_COL = "modelVersion";
+    private static final double[] ONE_ARRAY = new double[] {1.0, 1.0, 1.0};
+    private static final List<Row> TRAIN_DENSE_ROWS =
+            Arrays.asList(
+                    Row.of(Vectors.dense(0.1, 2.), 0.),
+                    Row.of(Vectors.dense(0.2, 2.), 0.),
+                    Row.of(Vectors.dense(0.3, 2.), 0.),
+                    Row.of(Vectors.dense(0.4, 2.), 0.),
+                    Row.of(Vectors.dense(0.5, 2.), 0.),
+                    Row.of(Vectors.dense(11., 12.), 1.),
+                    Row.of(Vectors.dense(12., 11.), 1.),
+                    Row.of(Vectors.dense(13., 12.), 1.),
+                    Row.of(Vectors.dense(14., 12.), 1.),
+                    Row.of(Vectors.dense(15., 12.), 1.));
+
+    private static final List<Row> PREDICT_DENSE_ROWS =
+            Arrays.asList(
+                    Row.of(Vectors.dense(0.8, 2.7), 0.),
+                    Row.of(Vectors.dense(0.8, 2.4), 0.),
+                    Row.of(Vectors.dense(0.7, 2.3), 0.),
+                    Row.of(Vectors.dense(0.4, 2.7), 0.),
+                    Row.of(Vectors.dense(0.5, 2.8), 0.),
+                    Row.of(Vectors.dense(10.2, 12.1), 1.),
+                    Row.of(Vectors.dense(13.3, 13.1), 1.),
+                    Row.of(Vectors.dense(13.5, 12.2), 1.),
+                    Row.of(Vectors.dense(14.9, 12.5), 1.),
+                    Row.of(Vectors.dense(15.5, 11.2), 1.));
+
+    private static final List<Row> TRAIN_SPARSE_ROWS =
+            Arrays.asList(
+                    Row.of(Vectors.sparse(10, new int[] {1, 3, 4}, ONE_ARRAY), 
0.),
+                    Row.of(Vectors.sparse(10, new int[] {0, 2, 3}, ONE_ARRAY), 
0.),
+                    Row.of(Vectors.sparse(10, new int[] {0, 3, 4}, ONE_ARRAY), 
0.),
+                    Row.of(Vectors.sparse(10, new int[] {2, 3, 4}, ONE_ARRAY), 
0.),
+                    Row.of(Vectors.sparse(10, new int[] {1, 3, 4}, ONE_ARRAY), 
0.),
+                    Row.of(Vectors.sparse(10, new int[] {6, 7, 8}, ONE_ARRAY), 
1.),
+                    Row.of(Vectors.sparse(10, new int[] {6, 8, 9}, ONE_ARRAY), 
1.),
+                    Row.of(Vectors.sparse(10, new int[] {5, 8, 9}, ONE_ARRAY), 
1.),
+                    Row.of(Vectors.sparse(10, new int[] {5, 6, 8}, ONE_ARRAY), 
1.),
+                    Row.of(Vectors.sparse(10, new int[] {5, 6, 7}, ONE_ARRAY), 
1.));
+
+    private static final List<Row> PREDICT_SPARSE_ROWS =
+            Arrays.asList(
+                    Row.of(Vectors.sparse(10, new int[] {1, 2, 4}, ONE_ARRAY), 
0.),
+                    Row.of(Vectors.sparse(10, new int[] {2, 3, 4}, ONE_ARRAY), 
0.),
+                    Row.of(Vectors.sparse(10, new int[] {0, 2, 4}, ONE_ARRAY), 
0.),
+                    Row.of(Vectors.sparse(10, new int[] {1, 3, 4}, ONE_ARRAY), 
0.),
+                    Row.of(Vectors.sparse(10, new int[] {6, 7, 9}, ONE_ARRAY), 
1.),
+                    Row.of(Vectors.sparse(10, new int[] {7, 8, 9}, ONE_ARRAY), 
1.),
+                    Row.of(Vectors.sparse(10, new int[] {5, 7, 9}, ONE_ARRAY), 
1.),
+                    Row.of(Vectors.sparse(10, new int[] {5, 6, 7}, ONE_ARRAY), 
1.),
+                    Row.of(Vectors.sparse(10, new int[] {5, 8, 9}, ONE_ARRAY), 
1.));
+
+    private Table initDenseModel;
+
+    @Before
+    public void before() throws Exception {
+        Configuration config = new Configuration();
+        
config.set(ExecutionCheckpointingOptions.ENABLE_CHECKPOINTS_AFTER_TASKS_FINISH, 
true);
+        env = StreamExecutionEnvironment.getExecutionEnvironment(config);
+        env.setParallelism(4);
+        env.enableCheckpointing(100);
+        env.setRestartStrategy(RestartStrategies.noRestart());
+        tEnv = StreamTableEnvironment.create(env);
+        DataStream<Row> dataStream = env.fromCollection(TRAIN_DENSE_ROWS);
+        trainDenseTable = tEnv.fromDataStream(dataStream).as(FEATURE_COL, 
LABEL_COL);
+        initDenseModel =
+                tEnv.fromDataStream(
+                        env.fromElements(Row.of(new DenseVector(new double[] 
{0.0, 0.0}), 0L)));
+    }
+
+    @Test
+    public void testFit() throws Exception {
+        Table onlinePredictTable = getTable(1, 1000, TRAIN_DENSE_ROWS, 4, 
false);
+        Table models =
+                new OnlineLogisticRegression()
+                        .setFeaturesCol(FEATURE_COL)
+                        .setInitialModelData(initDenseModel)
+                        .setGlobalBatchSize(100)
+                        .setLabelCol(LABEL_COL)
+                        .fit(onlinePredictTable)
+                        .getModelData()[0];
+        List<Row> modelList = 
IteratorUtils.toList(tEnv.toDataStream(models).executeAndCollect());
+        assertEquals(10, modelList.size());
+    }
+
+    @Test
+    public void testFitWithInitLrModel() throws Exception {
+        Table initLrModel =
+                new LogisticRegression()
+                        .setFeaturesCol(FEATURE_COL)
+                        .setLabelCol(LABEL_COL)
+                        .fit(trainDenseTable)
+                        .getModelData()[0];
+        Table onlineTrainTable = getTable(50, 1000, TRAIN_DENSE_ROWS, 4, 
false);
+        Table models =
+                new OnlineLogisticRegression()
+                        .setFeaturesCol(FEATURE_COL)
+                        .setInitialModelData(initLrModel)
+                        .setGlobalBatchSize(100)
+                        .setLabelCol(LABEL_COL)
+                        .fit(onlineTrainTable)
+                        .getModelData()[0];
+        List<Row> modelList = 
IteratorUtils.toList(tEnv.toDataStream(models).executeAndCollect());
+        assertEquals(10, modelList.size());
+    }
+
+    @Test
+    public void testDenseFitAndPredict() throws Exception {
+        Table onlineTrainTable = getTable(2, 2000, TRAIN_DENSE_ROWS, 2, false);
+        Table onlinePredictTable = getTable(2, 3000, PREDICT_DENSE_ROWS, 2, 
false);
+        OnlineLogisticRegressionModel model =
+                new OnlineLogisticRegression()
+                        .setFeaturesCol(FEATURE_COL)
+                        .setInitialModelData(initDenseModel)
+                        .setGlobalBatchSize(500)
+                        .setLabelCol(LABEL_COL)
+                        .fit(onlineTrainTable);
+        Table resultTable = 
model.setPredictionCol(PREDICT_COL).transform(onlinePredictTable)[0];
+        verifyPredictionResult(resultTable, 4);
+    }
+
+    @Test
+    public void testSparseFitAndPredict() throws Exception {
+        double[] doubleArray = new double[] {0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 
0.1, 0.1, 0.1, 0.1};
+        Table initSparseModel =
+                tEnv.fromDataStream(env.fromElements(Row.of(new 
DenseVector(doubleArray), 0L)));
+        Table onlineTrainTable = getTable(5, 2000, TRAIN_SPARSE_ROWS, 4, true);
+        Table onlinePredictTable = getTable(5, 3000, PREDICT_SPARSE_ROWS, 4, 
true);
+        OnlineLogisticRegressionModel model =
+                new OnlineLogisticRegression()
+                        .setFeaturesCol(FEATURE_COL)
+                        .setInitialModelData(initSparseModel)
+                        .setGlobalBatchSize(500)
+                        .setLabelCol(LABEL_COL)
+                        .fit(onlineTrainTable);
+        Table resultTable = 
model.setPredictionCol(PREDICT_COL).transform(onlinePredictTable)[0];
+        verifyPredictionResult(resultTable, 4);

Review Comment:
   I will refine it later.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to