lindong28 commented on code in PR #219:
URL: https://github.com/apache/flink-ml/pull/219#discussion_r1141014031


##########
flink-ml-lib/src/test/java/org/apache/flink/ml/classification/LogisticRegressionTest.java:
##########
@@ -282,6 +287,50 @@ public void testSetModelData() throws Exception {
                 logisticRegression.getRawPredictionCol());
     }
 
+    @Test
+    public void testSaveLoadServableAndPredict() throws Exception {
+        LogisticRegression logisticRegression = new 
LogisticRegression().setWeightCol("weight");
+        LogisticRegressionModel model = 
logisticRegression.fit(binomialDataTable);
+
+        LogisticRegressionModelServable servable =
+                saveAndLoadServable(
+                        tEnv,
+                        model,
+                        tempFolder.newFolder().getAbsolutePath(),
+                        LogisticRegressionModel::loadServable);
+
+        assertEquals("features", servable.getFeaturesCol());
+        assertEquals("prediction", servable.getPredictionCol());
+        assertEquals("rawPrediction", servable.getRawPredictionCol());
+
+        DataFrame output = 
servable.transform(LogisticRegressionModelServableTest.PREDICT_DATA);
+        LogisticRegressionModelServableTest.verifyPredictionResult(
+                output,
+                servable.getFeaturesCol(),
+                servable.getPredictionCol(),
+                servable.getRawPredictionCol());
+    }
+
+    @Test
+    public void testSetModelDataToServable() throws Exception {
+        LogisticRegression logisticRegression = new 
LogisticRegression().setWeightCol("weight");
+        LogisticRegressionModel model = 
logisticRegression.fit(binomialDataTable);
+        byte[] serializedModelData =
+                
LogisticRegressionModelDataUtil.getModelDataByteStream(model.getModelData()[0])
+                        .executeAndCollect()
+                        .next();
+
+        LogisticRegressionModelServable servable = new 
LogisticRegressionModelServable();
+        servable.setModelData(new ByteArrayInputStream(serializedModelData));

Review Comment:
   Would it be useful to forward parameters from model to this servable using 
`ParamUtils.updateExistingParams` so that later tests can follow the same 
practice consistently?



##########
flink-ml-servable-lib/src/test/java/org/apache/flink/ml/classification/LogisticRegressionModelServableTest.java:
##########
@@ -0,0 +1,142 @@
+/*
+ * 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.ml.classification.logisticregression.LogisticRegressionModelData;
+import 
org.apache.flink.ml.classification.logisticregression.LogisticRegressionModelServable;
+import org.apache.flink.ml.linalg.DenseVector;
+import org.apache.flink.ml.linalg.Vector;
+import org.apache.flink.ml.linalg.Vectors;
+import org.apache.flink.ml.servable.api.DataFrame;
+import org.apache.flink.ml.servable.api.Row;
+import org.apache.flink.ml.servable.types.BasicType;
+import org.apache.flink.ml.servable.types.DataTypes;
+
+import org.junit.Test;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+/** Tests the {@link LogisticRegressionModelServable}. */
+public class LogisticRegressionModelServableTest {
+
+    protected static final DataFrame PREDICT_DATA =
+            new DataFrame(
+                    new ArrayList<>(Arrays.asList("features", "label", 
"weight")),
+                    new ArrayList<>(
+                            Arrays.asList(
+                                    DataTypes.VECTOR(BasicType.DOUBLE),
+                                    DataTypes.DOUBLE,
+                                    DataTypes.DOUBLE)),
+                    Arrays.asList(
+                            new Row(
+                                    new ArrayList<>(
+                                            Arrays.asList(Vectors.dense(1, 2, 
3, 4), 0., 1.))),
+                            new Row(
+                                    new ArrayList<>(
+                                            Arrays.asList(Vectors.dense(1, 2, 
3, 4), 0., 2.))),
+                            new Row(
+                                    new ArrayList<>(
+                                            Arrays.asList(Vectors.dense(2, 2, 
3, 4), 0., 3.))),
+                            new Row(
+                                    new ArrayList<>(
+                                            Arrays.asList(Vectors.dense(3, 2, 
3, 4), 0., 4.))),
+                            new Row(
+                                    new ArrayList<>(
+                                            Arrays.asList(Vectors.dense(4, 2, 
3, 4), 0., 5.))),
+                            new Row(
+                                    new ArrayList<>(
+                                            Arrays.asList(Vectors.dense(11, 2, 
3, 4), 1., 1.))),
+                            new Row(
+                                    new ArrayList<>(
+                                            Arrays.asList(Vectors.dense(12, 2, 
3, 4), 1., 2.))),
+                            new Row(
+                                    new ArrayList<>(
+                                            Arrays.asList(Vectors.dense(13, 2, 
3, 4), 1., 3.))),
+                            new Row(
+                                    new ArrayList<>(
+                                            Arrays.asList(Vectors.dense(14, 2, 
3, 4), 1., 4.))),
+                            new Row(
+                                    new ArrayList<>(
+                                            Arrays.asList(Vectors.dense(15, 2, 
3, 4), 1., 5.)))));
+
+    private static final DenseVector COEFFICIENT = Vectors.dense(0.525, 
-0.283, -0.425, -0.567);
+
+    private static final double TOLERANCE = 1e-7;
+
+    @Test
+    public void testParam() {
+        LogisticRegressionModelServable servable = new 
LogisticRegressionModelServable();
+        assertEquals("features", servable.getFeaturesCol());
+        assertEquals("prediction", servable.getPredictionCol());
+        assertEquals("rawPrediction", servable.getRawPredictionCol());
+
+        servable.setFeaturesCol("test_features")
+                .setPredictionCol("test_predictionCol")
+                .setRawPredictionCol("test_rawPredictionCol");
+        assertEquals("test_features", servable.getFeaturesCol());
+        assertEquals("test_predictionCol", servable.getPredictionCol());
+        assertEquals("test_rawPredictionCol", servable.getRawPredictionCol());
+    }
+
+    @Test
+    public void testTransform() throws IOException {
+        LogisticRegressionModelData modelData = new 
LogisticRegressionModelData(COEFFICIENT);

Review Comment:
   I am not sure we need this test given that we don't expect users to 
construct model data directly like this.
   
   Note that we do not have a similar test for LogisticRegressionModel where we 
explicitly construct model data and give it to the LogisticRegressionModel.



##########
flink-ml-lib/src/test/java/org/apache/flink/ml/classification/LogisticRegressionTest.java:
##########
@@ -282,6 +287,50 @@ public void testSetModelData() throws Exception {
                 logisticRegression.getRawPredictionCol());
     }
 
+    @Test
+    public void testSaveLoadServableAndPredict() throws Exception {
+        LogisticRegression logisticRegression = new 
LogisticRegression().setWeightCol("weight");
+        LogisticRegressionModel model = 
logisticRegression.fit(binomialDataTable);
+
+        LogisticRegressionModelServable servable =
+                saveAndLoadServable(
+                        tEnv,
+                        model,
+                        tempFolder.newFolder().getAbsolutePath(),
+                        LogisticRegressionModel::loadServable);
+
+        assertEquals("features", servable.getFeaturesCol());

Review Comment:
   It seems simpler to remove these assertEquals.
   
   If we want to verify that the parameters are properly loaded, we can add a 
dedicated test that sets non-default values for those parameters first.



##########
flink-ml-servable-lib/src/main/java/org/apache/flink/ml/classification/logisticregression/LogisticRegressionModelServable.java:
##########
@@ -0,0 +1,116 @@
+/*
+ * 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.logisticregression;
+
+import org.apache.flink.api.java.tuple.Tuple2;
+import org.apache.flink.ml.linalg.BLAS;
+import org.apache.flink.ml.linalg.DenseVector;
+import org.apache.flink.ml.linalg.Vector;
+import org.apache.flink.ml.linalg.Vectors;
+import org.apache.flink.ml.param.Param;
+import org.apache.flink.ml.servable.api.DataFrame;
+import org.apache.flink.ml.servable.api.ModelServable;
+import org.apache.flink.ml.servable.api.Row;
+import org.apache.flink.ml.servable.types.BasicType;
+import org.apache.flink.ml.servable.types.DataTypes;
+import org.apache.flink.ml.util.ParamUtils;
+import org.apache.flink.ml.util.ServableReadWriteUtils;
+import org.apache.flink.util.Preconditions;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/** A Servable which can be used to classifies data in online inference. */
+public class LogisticRegressionModelServable
+        implements ModelServable<LogisticRegressionModelServable>,
+                LogisticRegressionModelParams<LogisticRegressionModelServable> 
{
+
+    private final Map<Param<?>, Object> paramMap = new HashMap<>();
+
+    private LogisticRegressionModelData modelData;
+
+    public LogisticRegressionModelServable() {
+        ParamUtils.initializeMapWithDefaultValues(paramMap, this);
+    }
+
+    LogisticRegressionModelServable(LogisticRegressionModelData modelData) {
+        this();
+        this.modelData = modelData;
+    }
+
+    @Override
+    public DataFrame transform(DataFrame input) {
+        List<Double> predictionResults = new ArrayList<>();
+        List<DenseVector> rawPredictionResults = new ArrayList<>();
+
+        int featuresColIndex = input.getIndex(getFeaturesCol());
+        for (Row row : input.collect()) {
+            Vector features = (Vector) row.get(featuresColIndex);
+            Tuple2<Double, DenseVector> dataPoint = transform(features);
+            predictionResults.add(dataPoint.f0);
+            rawPredictionResults.add(dataPoint.f1);
+        }
+
+        input.addColumn(getPredictionCol(), DataTypes.DOUBLE, 
predictionResults);
+        input.addColumn(
+                getRawPredictionCol(), DataTypes.VECTOR(BasicType.DOUBLE), 
rawPredictionResults);
+
+        return input;
+    }
+
+    public LogisticRegressionModelServable setModelData(InputStream... 
modelDataInputs)
+            throws IOException {
+        Preconditions.checkArgument(modelDataInputs.length == 1);
+
+        modelData = LogisticRegressionModelData.decode(modelDataInputs[0]);
+        return this;
+    }
+
+    public static LogisticRegressionModelServable load(String path) throws 
IOException {
+        LogisticRegressionModelServable servable =
+                ServableReadWriteUtils.loadServableParam(
+                        path, LogisticRegressionModelServable.class);
+
+        try (InputStream fsDataInputStream = 
ServableReadWriteUtils.loadModelData(path)) {

Review Comment:
   Would it be better to replace `fsDataInputStream` with `modelData`?



-- 
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: issues-unsubscr...@flink.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to