You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by "jiangxin369 (via GitHub)" <gi...@apache.org> on 2023/03/03 01:52:19 UTC

[GitHub] [flink-ml] jiangxin369 opened a new pull request, #219: Add Servable for Logistic Regression

jiangxin369 opened a new pull request, #219:
URL: https://github.com/apache/flink-ml/pull/219

   <!--
   *Thank you very much for contributing to Apache Flink ML - we are happy that you want to help us improve Flink ML. To help the community review your contribution in the best possible way, please go through the checklist below, which will get the contribution into a shape in which it can be best reviewed.*
   
   ## Contribution Checklist
   
     - Make sure that the pull request corresponds to one [JIRA issue](https://issues.apache.org/jira/projects/FLINK/issues). Exceptions are made for typos in JavaDoc or documentation files, which need no JIRA issue.
     
     - Name the pull request in the form "[FLINK-XXXX] Title of the pull request", where *FLINK-XXXX* should be replaced by the actual issue number.
     Typo fixes that have no associated JIRA issue should be named following this pattern: `[hotfix] Title of the pull request`.
   
     - Fill out the template below to describe the changes contributed by the pull request. That will give reviewers the context they need to do the review.
     
     - Each commit in the pull request has a meaningful commit message (including the JIRA id)
   
     - Once all items of the checklist are addressed, remove the above text and this checklist, leaving only the filled out template below.
   
   **(The sections below can be removed for hotfixes of typos)**
   -->
   
   ## What is the purpose of the change
   
   Add Servable for Logistic Regression.
   
   ## Brief change log
   
   *(for example:)*
     - Adds Servable for Logistic Regression
   
   ## Does this pull request potentially affect one of the following parts:
   
     - Dependencies (does it add or upgrade a dependency): (no)
     - The public API, i.e., is any changed class annotated with `@Public(Evolving)`: (no)
   
   ## Documentation
   
     - Does this pull request introduce a new feature? (yes)
     - If yes, how is the feature documented? (JavaDocs)
   


-- 
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-unsubscribe@flink.apache.org

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


[GitHub] [flink-ml] jiangxin369 commented on pull request #219: [FLINK-31422] Add Servable for Logistic Regression Model

Posted by "jiangxin369 (via GitHub)" <gi...@apache.org>.
jiangxin369 commented on PR #219:
URL: https://github.com/apache/flink-ml/pull/219#issuecomment-1467877038

   @lindong28 Could you help to review this PR?


-- 
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-unsubscribe@flink.apache.org

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


[GitHub] [flink-ml] jiangxin369 commented on a diff in pull request #219: [FLINK-31422] Add Servable for Logistic Regression Model

Posted by "jiangxin369 (via GitHub)" <gi...@apache.org>.
jiangxin369 commented on code in PR #219:
URL: https://github.com/apache/flink-ml/pull/219#discussion_r1141565978


##########
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 think it is expected to construct model data like this, e.g. for users who want to test if the servable transformation logic is correct without training a model. 
   
   We do not have a similar test for LogisticRegressionModel because the `LogisticRegression` and `LogisticRegressionModel` are in the same module, it is reasonable to validate `fit` and `transform` in the same test case. However, `LogisticRegressionModelServable` is in a separate module, which means it can be used and tested independently.



-- 
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-unsubscribe@flink.apache.org

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


[GitHub] [flink-ml] jiangxin369 commented on a diff in pull request #219: [FLINK-31422] Add Servable for Logistic Regression Model

Posted by "jiangxin369 (via GitHub)" <gi...@apache.org>.
jiangxin369 commented on code in PR #219:
URL: https://github.com/apache/flink-ml/pull/219#discussion_r1141831284


##########
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 agree. In this way, I could remove all tests in `flink-ml-servable-lib` currently. 
   
   I've updated the PR based on your advice, could you have a look again?



-- 
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-unsubscribe@flink.apache.org

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


[GitHub] [flink-ml] lindong28 commented on a diff in pull request #219: [FLINK-31422] Add Servable for Logistic Regression Model

Posted by "lindong28 (via GitHub)" <gi...@apache.org>.
lindong28 commented on code in PR #219:
URL: https://github.com/apache/flink-ml/pull/219#discussion_r1139907982


##########
flink-ml-lib/src/main/java/org/apache/flink/ml/classification/logisticregression/LogisticRegressionModel.java:
##########
@@ -149,22 +152,14 @@ public Row map(Row dataPoint) {
                                 getRuntimeContext().getBroadcastVariable(broadcastModelKey).get(0);
                 coefficient = modelData.coefficient;
             }
-            DenseVector features = ((Vector) dataPoint.getField(featuresCol)).toDense();
-            Row predictionResult = predictOneDataPoint(features, coefficient);
-            return Row.join(dataPoint, predictionResult);
-        }
-    }
+            Vector features = (Vector) dataPoint.getField(featuresCol);
+
+            LogisticRegressionModelServable servable =
+                    new LogisticRegressionModelServable(

Review Comment:
   Making `LogisticRegressionModelServable(LogisticRegressionModelData) ` protected sounds good to me.
   
   However, asking developers to identify and forward the selectively parameters from model to servable is not very convenient and introduces chances of human error. It will much more convenient for Flink ML to provide infra that algorithm developers can use to forward parameters, similar to how the existing Estimators forward parameters to Model using `ParamUtils.updateExistingParams`.



-- 
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-unsubscribe@flink.apache.org

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


[GitHub] [flink-ml] jiangxin369 commented on pull request #219: [FLINK-31422] Add Servable for Logistic Regression Model

Posted by "jiangxin369 (via GitHub)" <gi...@apache.org>.
jiangxin369 commented on PR #219:
URL: https://github.com/apache/flink-ml/pull/219#issuecomment-1474774669

   @lindong28 Thanks for the review, could you help review again?


-- 
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-unsubscribe@flink.apache.org

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


[GitHub] [flink-ml] jiangxin369 commented on a diff in pull request #219: [FLINK-31422] Add Servable for Logistic Regression Model

Posted by "jiangxin369 (via GitHub)" <gi...@apache.org>.
jiangxin369 commented on code in PR #219:
URL: https://github.com/apache/flink-ml/pull/219#discussion_r1136472957


##########
flink-ml-servable-lib/src/test/java/org/apache/flink/ml/classification/logisticregression/LogisticRegressionModelServableTest.java:
##########
@@ -0,0 +1,153 @@
+/*
+ * 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.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.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 {

Review Comment:
   It is covered in `LogisticRegressionTest#testSaveLoadServable`.



-- 
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-unsubscribe@flink.apache.org

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


[GitHub] [flink-ml] jiangxin369 commented on pull request #219: [FLINK-31422] Add Servable for Logistic Regression Model

Posted by "jiangxin369 (via GitHub)" <gi...@apache.org>.
jiangxin369 commented on PR #219:
URL: https://github.com/apache/flink-ml/pull/219#issuecomment-1471180452

   @lindong28 The PR is updated, could you have another look?


-- 
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-unsubscribe@flink.apache.org

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


[GitHub] [flink-ml] lindong28 commented on a diff in pull request #219: [FLINK-31422] Add Servable for Logistic Regression Model

Posted by "lindong28 (via GitHub)" <gi...@apache.org>.
lindong28 commented on code in PR #219:
URL: https://github.com/apache/flink-ml/pull/219#discussion_r1141624836


##########
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:
   The separation of `flink-ml-lib` and `flink-ml-servable-lib` modules is mostly an implementation detail and should not affect how users use `Model` and `Servable` classes, right?



-- 
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-unsubscribe@flink.apache.org

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


[GitHub] [flink-ml] lindong28 commented on a diff in pull request #219: [FLINK-31422] Add Servable for Logistic Regression Model

Posted by "lindong28 (via GitHub)" <gi...@apache.org>.
lindong28 commented on code in PR #219:
URL: https://github.com/apache/flink-ml/pull/219#discussion_r1136431138


##########
flink-ml-servable-lib/src/main/java/org/apache/flink/ml/classification/logisticregression/LogisticRegressionModelServable.java:
##########
@@ -0,0 +1,148 @@
+/*
+ * 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.core.memory.DataInputViewStreamWrapper;
+import org.apache.flink.core.memory.DataOutputViewStreamWrapper;
+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.linalg.typeinfo.DenseVectorSerializer;
+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.ByteArrayOutputStream;
+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 DenseVector coefficient;
+
+    public LogisticRegressionModelServable() {
+        ParamUtils.initializeMapWithDefaultValues(paramMap, this);
+    }
+
+    @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 = predictOneDataPoint(features, coefficient);
+            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);
+
+        DataInputViewStreamWrapper inputViewStreamWrapper =
+                new DataInputViewStreamWrapper(modelDataInputs[0]);
+
+        DenseVectorSerializer serializer = new DenseVectorSerializer();
+        coefficient = serializer.deserialize(inputViewStreamWrapper);
+
+        return this;
+    }
+
+    public static LogisticRegressionModelServable load(String path) throws IOException {
+        LogisticRegressionModelServable servable =
+                ServableReadWriteUtils.loadServableParam(
+                        path, LogisticRegressionModelServable.class);
+
+        try (InputStream fsDataInputStream = ServableReadWriteUtils.loadModelData(path)) {
+            DataInputViewStreamWrapper dataInputViewStreamWrapper =
+                    new DataInputViewStreamWrapper(fsDataInputStream);
+            DenseVectorSerializer serializer = new DenseVectorSerializer();
+            servable.coefficient = serializer.deserialize(dataInputViewStreamWrapper);
+            return servable;
+        }
+    }
+
+    /**
+     * The main logic that predicts one input data point.
+     *
+     * @param feature The input feature.
+     * @param coefficient The model parameters.
+     * @return The prediction label and the raw probabilities.
+     */
+    public static Tuple2<Double, DenseVector> predictOneDataPoint(
+            Vector feature, DenseVector coefficient) {
+        double dotValue = BLAS.dot(feature, coefficient);
+        double prob = 1 - 1.0 / (1.0 + Math.exp(dotValue));
+        return Tuple2.of(dotValue >= 0 ? 1. : 0., Vectors.dense(1 - prob, prob));
+    }
+
+    /**
+     * Serializes the model data into byte array which can be saved to external storage and then be
+     * used to update the Servable by `TransformerServable::setModelData` method.
+     *
+     * @param modelData The model data to be serialized.
+     * @return The serialized model data in byte array.
+     */
+    public static byte[] serialize(DenseVector modelData) throws IOException {
+        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
+
+        DataOutputViewStreamWrapper outputViewStreamWrapper =
+                new DataOutputViewStreamWrapper(outputStream);
+
+        DenseVectorSerializer serializer = new DenseVectorSerializer();
+        serializer.serialize(modelData, outputViewStreamWrapper);
+
+        return outputStream.toByteArray();
+    }
+
+    @Override
+    public Map<Param<?>, Object> getParamMap() {
+        return paramMap;
+    }
+
+    public DenseVector getCoefficient() {

Review Comment:
   Is it necessary to add this public API for users to get servable model data? This public API was not discussed in any existing FLIP.



##########
flink-ml-lib/src/test/java/org/apache/flink/ml/classification/LogisticRegressionTest.java:
##########
@@ -253,6 +255,25 @@ public void testSaveLoadAndPredict() throws Exception {
                 logisticRegression.getRawPredictionCol());
     }
 
+    @Test
+    public void testSaveLoadServable() 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());
+
+        assertArrayEquals(expectedCoefficient, servable.getCoefficient().values, 0.1);

Review Comment:
   Should we verify the result of the `servable.transform(...)` similar to `PipelineTest#testPipelineModelServable`?



##########
flink-ml-servable-lib/src/test/java/org/apache/flink/ml/classification/logisticregression/LogisticRegressionModelServableTest.java:
##########
@@ -0,0 +1,153 @@
+/*
+ * 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.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.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 {

Review Comment:
   Should we have a test that covers the processing of saving model and loading servable, similar to `PipelineTest#testPipelineModelServable`?



##########
flink-ml-lib/src/main/java/org/apache/flink/ml/classification/logisticregression/LogisticRegressionModel.java:
##########
@@ -150,21 +153,9 @@ public Row map(Row dataPoint) {
                 coefficient = modelData.coefficient;
             }
             DenseVector features = ((Vector) dataPoint.getField(featuresCol)).toDense();
-            Row predictionResult = predictOneDataPoint(features, coefficient);
-            return Row.join(dataPoint, predictionResult);
+            Tuple2<Double, DenseVector> predictionResult =
+                    LogisticRegressionModelServable.predictOneDataPoint(features, coefficient);

Review Comment:
   Would it be more readable to instantiate an instance of `LogisticRegressionModelServable` and call `LogisticRegressionModelServable#transform(features)` here?



##########
flink-ml-servable-lib/src/main/java/org/apache/flink/ml/classification/logisticregression/LogisticRegressionModelServable.java:
##########
@@ -0,0 +1,148 @@
+/*
+ * 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.core.memory.DataInputViewStreamWrapper;
+import org.apache.flink.core.memory.DataOutputViewStreamWrapper;
+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.linalg.typeinfo.DenseVectorSerializer;
+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.ByteArrayOutputStream;
+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 DenseVector coefficient;
+
+    public LogisticRegressionModelServable() {
+        ParamUtils.initializeMapWithDefaultValues(paramMap, this);
+    }
+
+    @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 = predictOneDataPoint(features, coefficient);
+            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);
+
+        DataInputViewStreamWrapper inputViewStreamWrapper =
+                new DataInputViewStreamWrapper(modelDataInputs[0]);
+
+        DenseVectorSerializer serializer = new DenseVectorSerializer();
+        coefficient = serializer.deserialize(inputViewStreamWrapper);
+
+        return this;
+    }
+
+    public static LogisticRegressionModelServable load(String path) throws IOException {
+        LogisticRegressionModelServable servable =
+                ServableReadWriteUtils.loadServableParam(
+                        path, LogisticRegressionModelServable.class);
+
+        try (InputStream fsDataInputStream = ServableReadWriteUtils.loadModelData(path)) {
+            DataInputViewStreamWrapper dataInputViewStreamWrapper =
+                    new DataInputViewStreamWrapper(fsDataInputStream);
+            DenseVectorSerializer serializer = new DenseVectorSerializer();
+            servable.coefficient = serializer.deserialize(dataInputViewStreamWrapper);
+            return servable;
+        }
+    }
+
+    /**
+     * The main logic that predicts one input data point.
+     *
+     * @param feature The input feature.
+     * @param coefficient The model parameters.
+     * @return The prediction label and the raw probabilities.
+     */
+    public static Tuple2<Double, DenseVector> predictOneDataPoint(

Review Comment:
   Should we make this method protected so that it won't be used by code outside Flink ML repo to do online prediction?
   
   Would it be better to name this method `Tuple2<Double, DenseVector> transform(Vector feature)` so that it is more consistent with `DataFrame transform(DataFrame input)`?



##########
flink-ml-servable-lib/src/main/java/org/apache/flink/ml/classification/logisticregression/LogisticRegressionModelServable.java:
##########
@@ -0,0 +1,148 @@
+/*
+ * 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.core.memory.DataInputViewStreamWrapper;
+import org.apache.flink.core.memory.DataOutputViewStreamWrapper;
+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.linalg.typeinfo.DenseVectorSerializer;
+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.ByteArrayOutputStream;
+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 DenseVector coefficient;
+
+    public LogisticRegressionModelServable() {
+        ParamUtils.initializeMapWithDefaultValues(paramMap, this);
+    }
+
+    @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 = predictOneDataPoint(features, coefficient);
+            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);
+
+        DataInputViewStreamWrapper inputViewStreamWrapper =
+                new DataInputViewStreamWrapper(modelDataInputs[0]);
+
+        DenseVectorSerializer serializer = new DenseVectorSerializer();
+        coefficient = serializer.deserialize(inputViewStreamWrapper);
+
+        return this;
+    }
+
+    public static LogisticRegressionModelServable load(String path) throws IOException {
+        LogisticRegressionModelServable servable =
+                ServableReadWriteUtils.loadServableParam(
+                        path, LogisticRegressionModelServable.class);
+
+        try (InputStream fsDataInputStream = ServableReadWriteUtils.loadModelData(path)) {
+            DataInputViewStreamWrapper dataInputViewStreamWrapper =
+                    new DataInputViewStreamWrapper(fsDataInputStream);
+            DenseVectorSerializer serializer = new DenseVectorSerializer();
+            servable.coefficient = serializer.deserialize(dataInputViewStreamWrapper);
+            return servable;
+        }
+    }
+
+    /**
+     * The main logic that predicts one input data point.
+     *
+     * @param feature The input feature.
+     * @param coefficient The model parameters.
+     * @return The prediction label and the raw probabilities.
+     */
+    public static Tuple2<Double, DenseVector> predictOneDataPoint(
+            Vector feature, DenseVector coefficient) {
+        double dotValue = BLAS.dot(feature, coefficient);
+        double prob = 1 - 1.0 / (1.0 + Math.exp(dotValue));
+        return Tuple2.of(dotValue >= 0 ? 1. : 0., Vectors.dense(1 - prob, prob));
+    }
+
+    /**
+     * Serializes the model data into byte array which can be saved to external storage and then be
+     * used to update the Servable by `TransformerServable::setModelData` method.
+     *
+     * @param modelData The model data to be serialized.
+     * @return The serialized model data in byte array.
+     */
+    public static byte[] serialize(DenseVector modelData) throws IOException {

Review Comment:
   Does this mean that users need to know the internal data structure/type of the model data in order to use this public method?



##########
flink-ml-servable-lib/src/test/java/org/apache/flink/ml/classification/logisticregression/LogisticRegressionModelServableTest.java:
##########
@@ -0,0 +1,153 @@
+/*
+ * 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.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.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 {
+
+    private 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 {
+        LogisticRegressionModelServable servable = new LogisticRegressionModelServable();
+
+        servable.setModelData(
+                new ByteArrayInputStream(LogisticRegressionModelServable.serialize(COEFFICIENT)));
+
+        DataFrame output = servable.transform(PREDICT_DATA);
+
+        verifyPredictionResult(
+                output,
+                servable.getFeaturesCol(),
+                servable.getPredictionCol(),
+                servable.getRawPredictionCol());
+    }
+
+    @Test
+    public void testSetModelData() throws IOException {

Review Comment:
   This test looks **exactly** the same as `testTransform()`. What is the purpose of having both tests?



-- 
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-unsubscribe@flink.apache.org

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


[GitHub] [flink-ml] lindong28 commented on a diff in pull request #219: [FLINK-31422] Add Servable for Logistic Regression Model

Posted by "lindong28 (via GitHub)" <gi...@apache.org>.
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-unsubscribe@flink.apache.org

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


[GitHub] [flink-ml] lindong28 commented on a diff in pull request #219: [FLINK-31422] Add Servable for Logistic Regression Model

Posted by "lindong28 (via GitHub)" <gi...@apache.org>.
lindong28 commented on code in PR #219:
URL: https://github.com/apache/flink-ml/pull/219#discussion_r1141855665


##########
flink-ml-servable-lib/src/main/java/org/apache/flink/ml/classification/logisticregression/LogisticRegressionModelData.java:
##########
@@ -0,0 +1,80 @@
+/*
+ * 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.annotation.VisibleForTesting;
+import org.apache.flink.core.memory.DataInputViewStreamWrapper;
+import org.apache.flink.core.memory.DataOutputViewStreamWrapper;
+import org.apache.flink.ml.linalg.DenseVector;
+import org.apache.flink.ml.linalg.typeinfo.DenseVectorSerializer;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+/** Model data of {@link LogisticRegressionModelServable}. */
+public class LogisticRegressionModelData {
+
+    public DenseVector coefficient;
+
+    public long modelVersion;
+
+    public LogisticRegressionModelData() {}
+
+    public LogisticRegressionModelData(DenseVector coefficient) {

Review Comment:
   Would it be simpler to remove this constructor and let users instantiate `LogisticRegressionModelData(.., 0)` similar to what is done in `LogisticRegression#fit`?



-- 
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-unsubscribe@flink.apache.org

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


[GitHub] [flink-ml] lindong28 commented on a diff in pull request #219: [FLINK-31422] Add Servable for Logistic Regression Model

Posted by "lindong28 (via GitHub)" <gi...@apache.org>.
lindong28 commented on code in PR #219:
URL: https://github.com/apache/flink-ml/pull/219#discussion_r1139907982


##########
flink-ml-lib/src/main/java/org/apache/flink/ml/classification/logisticregression/LogisticRegressionModel.java:
##########
@@ -149,22 +152,14 @@ public Row map(Row dataPoint) {
                                 getRuntimeContext().getBroadcastVariable(broadcastModelKey).get(0);
                 coefficient = modelData.coefficient;
             }
-            DenseVector features = ((Vector) dataPoint.getField(featuresCol)).toDense();
-            Row predictionResult = predictOneDataPoint(features, coefficient);
-            return Row.join(dataPoint, predictionResult);
-        }
-    }
+            Vector features = (Vector) dataPoint.getField(featuresCol);
+
+            LogisticRegressionModelServable servable =
+                    new LogisticRegressionModelServable(

Review Comment:
   Making `LogisticRegressionModelServable(LogisticRegressionModelData) ` protected sounds OK to me.
   
   However, asking developers to identify and forward selected parameters from model to servable is not very convenient and introduces chances of human error. It will much more convenient for Flink ML to provide infra that algorithm developers can use to forward parameters, similar to how the existing Estimators forward parameters to Model using `ParamUtils.updateExistingParams`.



-- 
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-unsubscribe@flink.apache.org

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


[GitHub] [flink-ml] lindong28 commented on a diff in pull request #219: [FLINK-31422] Add Servable for Logistic Regression Model

Posted by "lindong28 (via GitHub)" <gi...@apache.org>.
lindong28 commented on code in PR #219:
URL: https://github.com/apache/flink-ml/pull/219#discussion_r1141624836


##########
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:
   The separate of `flink-ml-lib` and `flink-ml-servable-lib` is an implementation detail and should not affect how users use `Servable` classes, right?



##########
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:
   The separate of `flink-ml-lib` and `flink-ml-servable-lib` is an implementation detail and should not affect how users use `Model` and `Servable` classes, right?



-- 
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-unsubscribe@flink.apache.org

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


[GitHub] [flink-ml] lindong28 commented on a diff in pull request #219: [FLINK-31422] Add Servable for Logistic Regression Model

Posted by "lindong28 (via GitHub)" <gi...@apache.org>.
lindong28 commented on code in PR #219:
URL: https://github.com/apache/flink-ml/pull/219#discussion_r1141624836


##########
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:
   The separate of `flink-ml-lib` and `flink-ml-servable-lib` modules is mostly an implementation detail and should not affect how users use `Model` and `Servable` classes, right?



-- 
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-unsubscribe@flink.apache.org

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


[GitHub] [flink-ml] jiangxin369 commented on a diff in pull request #219: [FLINK-31422] Add Servable for Logistic Regression Model

Posted by "jiangxin369 (via GitHub)" <gi...@apache.org>.
jiangxin369 commented on code in PR #219:
URL: https://github.com/apache/flink-ml/pull/219#discussion_r1136491018


##########
flink-ml-lib/src/test/java/org/apache/flink/ml/classification/LogisticRegressionTest.java:
##########
@@ -253,6 +255,25 @@ public void testSaveLoadAndPredict() throws Exception {
                 logisticRegression.getRawPredictionCol());
     }
 
+    @Test
+    public void testSaveLoadServable() 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());
+
+        assertArrayEquals(expectedCoefficient, servable.getCoefficient().values, 0.1);

Review Comment:
   I don't think we really need to verify the result of the `servable.transform(...)`, because it has been covered in `LogisticRegressionModelServableTest#testTransform`. If you are worrying about the consistency of test cases of `PipelineTest` and `LogisticRegressionModelServableTest`, I think we should change the `PipelineTest#testPipelineModelServable` to only verify the model data of each stages.



-- 
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-unsubscribe@flink.apache.org

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


[GitHub] [flink-ml] lindong28 commented on a diff in pull request #219: [FLINK-31422] Add Servable for Logistic Regression Model

Posted by "lindong28 (via GitHub)" <gi...@apache.org>.
lindong28 commented on code in PR #219:
URL: https://github.com/apache/flink-ml/pull/219#discussion_r1136518610


##########
flink-ml-servable-lib/src/test/java/org/apache/flink/ml/classification/logisticregression/LogisticRegressionModelServableTest.java:
##########
@@ -0,0 +1,153 @@
+/*
+ * 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.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.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 {

Review Comment:
   `LogisticRegressionTest#testSaveLoadServable` currently does not verify the result of `LogisticRegressionModelServable#transform`.
   
   We need to have a test that shows that we can save/load servable and use the loaded servable for online prediction, similar to why we have both LogisticRegressionTest#testFitAndPredict and LogisticRegressionTest#testSaveLoadAndPredict.



-- 
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-unsubscribe@flink.apache.org

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


[GitHub] [flink-ml] jiangxin369 commented on a diff in pull request #219: [FLINK-31422] Add Servable for Logistic Regression Model

Posted by "jiangxin369 (via GitHub)" <gi...@apache.org>.
jiangxin369 commented on code in PR #219:
URL: https://github.com/apache/flink-ml/pull/219#discussion_r1139895407


##########
flink-ml-lib/src/main/java/org/apache/flink/ml/classification/logisticregression/LogisticRegressionModel.java:
##########
@@ -149,22 +152,14 @@ public Row map(Row dataPoint) {
                                 getRuntimeContext().getBroadcastVariable(broadcastModelKey).get(0);
                 coefficient = modelData.coefficient;
             }
-            DenseVector features = ((Vector) dataPoint.getField(featuresCol)).toDense();
-            Row predictionResult = predictOneDataPoint(features, coefficient);
-            return Row.join(dataPoint, predictionResult);
-        }
-    }
+            Vector features = (Vector) dataPoint.getField(featuresCol);
+
+            LogisticRegressionModelServable servable =
+                    new LogisticRegressionModelServable(

Review Comment:
   1. I think we'd better keep the constructor but make it protected or default. Because without the `LogisticRegressionModelServable(LogisticRegressionModelData)` constructor, every time we want to construct the `LogisticRegressionModelServable` with an existing `LogisticRegressionModelData`, we need to serialize the instance of `LogisticRegressionModelData` to bytes and then deserialize the bytes via `setModelData(InputStream... modelDataInputs)`, it may be expensive if the model data is large.
   2.   Yes we should forward parameters from model to servable, but the `LogisticRegressionModelServable` doesn't need any parameters from model to do predictions. If we are supporting `MinMaxScalerModelServable`, developers can pass the params by `servable.setMin()` directly.



-- 
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-unsubscribe@flink.apache.org

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


[GitHub] [flink-ml] jiangxin369 commented on a diff in pull request #219: [FLINK-31422] Add Servable for Logistic Regression Model

Posted by "jiangxin369 (via GitHub)" <gi...@apache.org>.
jiangxin369 commented on code in PR #219:
URL: https://github.com/apache/flink-ml/pull/219#discussion_r1139895407


##########
flink-ml-lib/src/main/java/org/apache/flink/ml/classification/logisticregression/LogisticRegressionModel.java:
##########
@@ -149,22 +152,14 @@ public Row map(Row dataPoint) {
                                 getRuntimeContext().getBroadcastVariable(broadcastModelKey).get(0);
                 coefficient = modelData.coefficient;
             }
-            DenseVector features = ((Vector) dataPoint.getField(featuresCol)).toDense();
-            Row predictionResult = predictOneDataPoint(features, coefficient);
-            return Row.join(dataPoint, predictionResult);
-        }
-    }
+            Vector features = (Vector) dataPoint.getField(featuresCol);
+
+            LogisticRegressionModelServable servable =
+                    new LogisticRegressionModelServable(

Review Comment:
   1. Without the `LogisticRegressionModelServable(LogisticRegressionModelData)` constructor, every time we want to construct the `LogisticRegressionModelServable` with an existing `LogisticRegressionModelData`, we need to serialize the instance of `LogisticRegressionModelData` to bytes and then deserialize the bytes via `setModelData(InputStream... modelDataInputs)`, it may be expensive if the model data is large. I prefer to keep the constructor but make it `protected` or `default`.
   2.   Yes we should forward parameters from model to servable, but the `LogisticRegressionModelServable` doesn't need any parameters from model to do predictions. If we are supporting `MinMaxScalerModelServable`, developers can pass the params by `servable.setMin()` directly.



-- 
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-unsubscribe@flink.apache.org

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


[GitHub] [flink-ml] jiangxin369 commented on a diff in pull request #219: [FLINK-31422] Add Servable for Logistic Regression Model

Posted by "jiangxin369 (via GitHub)" <gi...@apache.org>.
jiangxin369 commented on code in PR #219:
URL: https://github.com/apache/flink-ml/pull/219#discussion_r1136472041


##########
flink-ml-servable-lib/src/test/java/org/apache/flink/ml/classification/logisticregression/LogisticRegressionModelServableTest.java:
##########
@@ -0,0 +1,153 @@
+/*
+ * 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.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.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 {
+
+    private 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 {
+        LogisticRegressionModelServable servable = new LogisticRegressionModelServable();
+
+        servable.setModelData(
+                new ByteArrayInputStream(LogisticRegressionModelServable.serialize(COEFFICIENT)));
+
+        DataFrame output = servable.transform(PREDICT_DATA);
+
+        verifyPredictionResult(
+                output,
+                servable.getFeaturesCol(),
+                servable.getPredictionCol(),
+                servable.getRawPredictionCol());
+    }
+
+    @Test
+    public void testSetModelData() throws IOException {

Review Comment:
   Yes, I should have deleted it.



-- 
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-unsubscribe@flink.apache.org

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


[GitHub] [flink-ml] lindong28 commented on a diff in pull request #219: [FLINK-31422] Add Servable for Logistic Regression Model

Posted by "lindong28 (via GitHub)" <gi...@apache.org>.
lindong28 commented on code in PR #219:
URL: https://github.com/apache/flink-ml/pull/219#discussion_r1136518610


##########
flink-ml-servable-lib/src/test/java/org/apache/flink/ml/classification/logisticregression/LogisticRegressionModelServableTest.java:
##########
@@ -0,0 +1,153 @@
+/*
+ * 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.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.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 {

Review Comment:
   `LogisticRegressionTest#testSaveLoadServable` currently does not verify the result of `LogisticRegressionModelServable#transform`.



-- 
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-unsubscribe@flink.apache.org

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


[GitHub] [flink-ml] lindong28 commented on a diff in pull request #219: [FLINK-31422] Add Servable for Logistic Regression Model

Posted by "lindong28 (via GitHub)" <gi...@apache.org>.
lindong28 commented on code in PR #219:
URL: https://github.com/apache/flink-ml/pull/219#discussion_r1136518106


##########
flink-ml-lib/src/test/java/org/apache/flink/ml/classification/LogisticRegressionTest.java:
##########
@@ -253,6 +255,25 @@ public void testSaveLoadAndPredict() throws Exception {
                 logisticRegression.getRawPredictionCol());
     }
 
+    @Test
+    public void testSaveLoadServable() 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());
+
+        assertArrayEquals(expectedCoefficient, servable.getCoefficient().values, 0.1);

Review Comment:
   `LogisticRegressionModelServableTest#testTransform` does not save/load servable.
   
   We need to have a test that shows that we can save/load servable and use the loaded servable for online prediction, similar to why we have both `LogisticRegressionTest#testFitAndPredict` and `LogisticRegressionTest#testSaveLoadAndPredict`.



-- 
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-unsubscribe@flink.apache.org

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


[GitHub] [flink-ml] lindong28 commented on a diff in pull request #219: [FLINK-31422] Add Servable for Logistic Regression Model

Posted by "lindong28 (via GitHub)" <gi...@apache.org>.
lindong28 commented on code in PR #219:
URL: https://github.com/apache/flink-ml/pull/219#discussion_r1139907982


##########
flink-ml-lib/src/main/java/org/apache/flink/ml/classification/logisticregression/LogisticRegressionModel.java:
##########
@@ -149,22 +152,14 @@ public Row map(Row dataPoint) {
                                 getRuntimeContext().getBroadcastVariable(broadcastModelKey).get(0);
                 coefficient = modelData.coefficient;
             }
-            DenseVector features = ((Vector) dataPoint.getField(featuresCol)).toDense();
-            Row predictionResult = predictOneDataPoint(features, coefficient);
-            return Row.join(dataPoint, predictionResult);
-        }
-    }
+            Vector features = (Vector) dataPoint.getField(featuresCol);
+
+            LogisticRegressionModelServable servable =
+                    new LogisticRegressionModelServable(

Review Comment:
   Making `LogisticRegressionModelServable(LogisticRegressionModelData) ` protected sounds OK to me.
   
   However, asking developers to identify and forward the selectively parameters from model to servable is not very convenient and introduces chances of human error. It will much more convenient for Flink ML to provide infra that algorithm developers can use to forward parameters, similar to how the existing Estimators forward parameters to Model using `ParamUtils.updateExistingParams`.



-- 
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-unsubscribe@flink.apache.org

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


[GitHub] [flink-ml] lindong28 commented on a diff in pull request #219: [FLINK-31422] Add Servable for Logistic Regression Model

Posted by "lindong28 (via GitHub)" <gi...@apache.org>.
lindong28 commented on code in PR #219:
URL: https://github.com/apache/flink-ml/pull/219#discussion_r1138192339


##########
flink-ml-lib/src/main/java/org/apache/flink/ml/classification/logisticregression/LogisticRegressionModel.java:
##########
@@ -149,22 +152,14 @@ public Row map(Row dataPoint) {
                                 getRuntimeContext().getBroadcastVariable(broadcastModelKey).get(0);
                 coefficient = modelData.coefficient;
             }
-            DenseVector features = ((Vector) dataPoint.getField(featuresCol)).toDense();
-            Row predictionResult = predictOneDataPoint(features, coefficient);
-            return Row.join(dataPoint, predictionResult);
-        }
-    }
+            Vector features = (Vector) dataPoint.getField(featuresCol);
+
+            LogisticRegressionModelServable servable =

Review Comment:
   Would it be more performant to initialize the `LogisticRegressionModelServable` only once and re-use it for predictions?



##########
flink-ml-lib/src/main/java/org/apache/flink/ml/classification/logisticregression/LogisticRegressionModel.java:
##########
@@ -149,22 +152,14 @@ public Row map(Row dataPoint) {
                                 getRuntimeContext().getBroadcastVariable(broadcastModelKey).get(0);
                 coefficient = modelData.coefficient;
             }
-            DenseVector features = ((Vector) dataPoint.getField(featuresCol)).toDense();
-            Row predictionResult = predictOneDataPoint(features, coefficient);
-            return Row.join(dataPoint, predictionResult);
-        }
-    }
+            Vector features = (Vector) dataPoint.getField(featuresCol);
+
+            LogisticRegressionModelServable servable =
+                    new LogisticRegressionModelServable(

Review Comment:
   Can we set model data using `LogisticRegressionModelServable#setModelData`, instead of introducing the `LogisticRegressionModelServable(LogisticRegressionModelData)` constructor?
   
   Also, should we forward all parameters from `LogisticRegressionModelParams` to this servable instance?
   
   Note that servables such as `MinMaxScalerModelServable` might rely on   parameters (e.g. MinMaxScalerParams#getMin) to do online transformation. It will be useful to consistently forward parameters from model to servable and figure out the required infra in this PR.



##########
flink-ml-servable-lib/src/main/java/org/apache/flink/ml/classification/logisticregression/LogisticRegressionModelServable.java:
##########
@@ -0,0 +1,132 @@
+/*
+ * 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.core.memory.DataInputViewStreamWrapper;
+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.linalg.typeinfo.DenseVectorSerializer;
+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);
+    }
+
+    public 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);
+
+        DataInputViewStreamWrapper inputViewStreamWrapper =
+                new DataInputViewStreamWrapper(modelDataInputs[0]);
+
+        DenseVectorSerializer serializer = new DenseVectorSerializer();

Review Comment:
   It seems that the logic of deserialization model data is duplicated between `LogisticRegressionModelServable#load` and `LogisticRegressionModelServable#setModelData`.
   
   And it might be more readable to put the method that de-serialize the model data in the same class as the method that serialize the model data.



##########
flink-ml-servable-lib/src/main/java/org/apache/flink/ml/classification/logisticregression/LogisticRegressionModelServable.java:
##########
@@ -0,0 +1,132 @@
+/*
+ * 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.core.memory.DataInputViewStreamWrapper;
+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.linalg.typeinfo.DenseVectorSerializer;
+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);
+    }
+
+    public LogisticRegressionModelServable(LogisticRegressionModelData modelData) {
+        this();

Review Comment:
   It seems unreadable that the instance created using this constructor does not have `paramMap` initialized.



##########
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);
+        List<LogisticRegressionModelData> modelData =

Review Comment:
   The code requires users to explicitly deal with `LogisticRegressionModel`, which seems different from what we want.
   
   How about we add the method `public static LogisticRegressionModelDataUtil#DataStream<byte[]> getModelDataByteStream(Table modelData)` and do the following:
   
   ```
   byte[] modelData =
           (byte[]) IteratorUtils.toList(
                   LogisticRegressionModelDataUtil.getModelDataByteStream(model.getModelData()[0])
                           .executeAndCollect()).get(0);
   
   LogisticRegressionModelServable servable = new LogisticRegressionModelServable();
   servable.setModelData(new ByteArrayInputStream(modelData));
   ```
   
   And this approach would not need `LogisticRegressionModelData#serialize` as a public method.



##########
flink-ml-servable-lib/src/main/java/org/apache/flink/ml/classification/logisticregression/LogisticRegressionModelServable.java:
##########
@@ -0,0 +1,132 @@
+/*
+ * 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.core.memory.DataInputViewStreamWrapper;
+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.linalg.typeinfo.DenseVectorSerializer;
+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);
+    }
+
+    public 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);
+
+        DataInputViewStreamWrapper inputViewStreamWrapper =
+                new DataInputViewStreamWrapper(modelDataInputs[0]);
+
+        DenseVectorSerializer serializer = new DenseVectorSerializer();
+
+        DenseVector coefficient = serializer.deserialize(inputViewStreamWrapper);
+        long modelVersion = inputViewStreamWrapper.readLong();
+        modelData = new LogisticRegressionModelData(coefficient, modelVersion);
+
+        return this;
+    }
+
+    public static LogisticRegressionModelServable load(String path) throws IOException {
+        LogisticRegressionModelServable servable =
+                ServableReadWriteUtils.loadServableParam(
+                        path, LogisticRegressionModelServable.class);
+
+        try (InputStream fsDataInputStream = ServableReadWriteUtils.loadModelData(path)) {
+            DataInputViewStreamWrapper dataInputViewStreamWrapper =
+                    new DataInputViewStreamWrapper(fsDataInputStream);
+            DenseVectorSerializer serializer = new DenseVectorSerializer();
+            DenseVector coefficient = serializer.deserialize(dataInputViewStreamWrapper);
+            long modelVersion = dataInputViewStreamWrapper.readLong();
+
+            servable.modelData = new LogisticRegressionModelData(coefficient, modelVersion);

Review Comment:
   Would it be simpler to set model data via `servable#setModelData`?



-- 
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-unsubscribe@flink.apache.org

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


[GitHub] [flink-ml] lindong28 commented on a diff in pull request #219: [FLINK-31422] Add Servable for Logistic Regression Model

Posted by "lindong28 (via GitHub)" <gi...@apache.org>.
lindong28 commented on code in PR #219:
URL: https://github.com/apache/flink-ml/pull/219#discussion_r1141624836


##########
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:
   The separate of `flink-ml-lib` and `flink-ml-servable-lib` is mostly an implementation detail and should not affect how users use `Model` and `Servable` classes, right?



-- 
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-unsubscribe@flink.apache.org

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


[GitHub] [flink-ml] lindong28 closed pull request #219: [FLINK-31422] Add Servable for Logistic Regression Model

Posted by "lindong28 (via GitHub)" <gi...@apache.org>.
lindong28 closed pull request #219: [FLINK-31422] Add Servable for Logistic Regression Model
URL: https://github.com/apache/flink-ml/pull/219


-- 
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-unsubscribe@flink.apache.org

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