weibozhao commented on a change in pull request #60: URL: https://github.com/apache/flink-ml/pull/60#discussion_r810862420
########## File path: flink-ml-lib/src/test/java/org/apache/flink/ml/classification/OnlineModelSaveLoadTest.java ########## @@ -0,0 +1,128 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.flink.ml.classification; + +import org.apache.flink.api.common.restartstrategy.RestartStrategies; +import org.apache.flink.configuration.Configuration; +import org.apache.flink.ml.classification.logisticregression.LogisticRegressionModel; +import org.apache.flink.ml.classification.logisticregression.LogisticRegressionModelData; +import org.apache.flink.ml.linalg.DenseVector; +import org.apache.flink.ml.linalg.Vectors; +import org.apache.flink.streaming.api.environment.ExecutionCheckpointingOptions; +import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment; +import org.apache.flink.table.api.DataTypes; +import org.apache.flink.table.api.Schema; +import org.apache.flink.table.api.Table; +import org.apache.flink.table.api.bridge.java.StreamTableEnvironment; +import org.apache.flink.types.Row; + +import org.apache.commons.collections.IteratorUtils; +import org.junit.Assert; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import static org.apache.flink.ml.util.ReadWriteUtils.loadModelData; + +/** Tests online LogisticRegression model save and load. */ +public class OnlineModelSaveLoadTest { + @Rule public final TemporaryFolder tempFolder = new TemporaryFolder(); + + StreamTableEnvironment tEnv; + Schema modelSchema = Schema.newBuilder().column("f0", DataTypes.of(DenseVector.class)).build(); + Schema dataSchema = + Schema.newBuilder() + .column("f0", DataTypes.of(Double.class)) + .column("f1", DataTypes.of(DenseVector.class)) + .build(); + + private static final List<Row> modelData = + new ArrayList<>( + Arrays.asList( + Row.of(Vectors.dense(2.0, 4.5, 3.0)), + Row.of(Vectors.dense(2.1, 4.6, 3.1)), + Row.of(Vectors.dense(20.1, 5.6, 3.1)), + Row.of(Vectors.dense(2.1, 4.7, 3.1)))); + + private static final List<Row> validData = + new ArrayList<>( + Arrays.asList( + Row.of(1.0, Vectors.dense(1.0, 3.5, -4.0)), + Row.of(0.0, Vectors.dense(1.1, -8.6, 3.3)))); + + @Test + public void saveAndLoadOnlineModel() throws Exception { + Configuration config = new Configuration(); + config.set(ExecutionCheckpointingOptions.ENABLE_CHECKPOINTS_AFTER_TASKS_FINISH, true); + StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(config); + env.enableCheckpointing(100); + env.setRestartStrategy(RestartStrategies.noRestart()); + tEnv = StreamTableEnvironment.create(env); + env.setParallelism(1); + + String tmpPath = tempFolder.newFolder().getAbsolutePath(); + + /* Constructs online LogisticRegression model. */ + Table lrModelStream = + tEnv.fromDataStream(env.fromCollection(modelData), modelSchema).as("coefficient"); + LogisticRegressionModel lr = new LogisticRegressionModel(); + /* Saves online model to given path (tmpPath). */ + lr.setModelData(lrModelStream); + lr.save(tmpPath); + env.execute(); + + /* Constructs validated data table. */ + Table validDataTable = + tEnv.fromDataStream(env.fromCollection(validData), dataSchema).as("label, vec"); + + Process proc = Runtime.getRuntime().exec("ls " + tmpPath + "/data"); + BufferedReader bufferedReader = + new BufferedReader(new InputStreamReader(proc.getInputStream())); + + /* Loads every LogisticRegression model in model path and validates it. */ + String modelVersion; + while ((modelVersion = bufferedReader.readLine()) != null) { + if (!"metadata".equals(modelVersion)) { + LogisticRegressionModel lrModel = + new LogisticRegressionModel().setFeaturesCol("vec"); + Table modelData = + tEnv.fromDataStream( + loadModelData( + env, + tmpPath, + new LogisticRegressionModelData.ModelDataDecoder(), + modelVersion)) + .as("label, vec"); Review comment: The validated data has feature(vec) and label. I will check the prediction result agree with given label. ########## File path: flink-ml-lib/src/test/java/org/apache/flink/ml/classification/OnlineModelSaveLoadTest.java ########## @@ -0,0 +1,128 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.flink.ml.classification; + +import org.apache.flink.api.common.restartstrategy.RestartStrategies; +import org.apache.flink.configuration.Configuration; +import org.apache.flink.ml.classification.logisticregression.LogisticRegressionModel; +import org.apache.flink.ml.classification.logisticregression.LogisticRegressionModelData; +import org.apache.flink.ml.linalg.DenseVector; +import org.apache.flink.ml.linalg.Vectors; +import org.apache.flink.streaming.api.environment.ExecutionCheckpointingOptions; +import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment; +import org.apache.flink.table.api.DataTypes; +import org.apache.flink.table.api.Schema; +import org.apache.flink.table.api.Table; +import org.apache.flink.table.api.bridge.java.StreamTableEnvironment; +import org.apache.flink.types.Row; + +import org.apache.commons.collections.IteratorUtils; +import org.junit.Assert; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import static org.apache.flink.ml.util.ReadWriteUtils.loadModelData; + +/** Tests online LogisticRegression model save and load. */ +public class OnlineModelSaveLoadTest { + @Rule public final TemporaryFolder tempFolder = new TemporaryFolder(); + + StreamTableEnvironment tEnv; + Schema modelSchema = Schema.newBuilder().column("f0", DataTypes.of(DenseVector.class)).build(); + Schema dataSchema = + Schema.newBuilder() + .column("f0", DataTypes.of(Double.class)) + .column("f1", DataTypes.of(DenseVector.class)) + .build(); + + private static final List<Row> modelData = + new ArrayList<>( + Arrays.asList( + Row.of(Vectors.dense(2.0, 4.5, 3.0)), + Row.of(Vectors.dense(2.1, 4.6, 3.1)), + Row.of(Vectors.dense(20.1, 5.6, 3.1)), + Row.of(Vectors.dense(2.1, 4.7, 3.1)))); + + private static final List<Row> validData = + new ArrayList<>( + Arrays.asList( + Row.of(1.0, Vectors.dense(1.0, 3.5, -4.0)), + Row.of(0.0, Vectors.dense(1.1, -8.6, 3.3)))); + + @Test + public void saveAndLoadOnlineModel() throws Exception { + Configuration config = new Configuration(); + config.set(ExecutionCheckpointingOptions.ENABLE_CHECKPOINTS_AFTER_TASKS_FINISH, true); + StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(config); + env.enableCheckpointing(100); + env.setRestartStrategy(RestartStrategies.noRestart()); + tEnv = StreamTableEnvironment.create(env); + env.setParallelism(1); + + String tmpPath = tempFolder.newFolder().getAbsolutePath(); + + /* Constructs online LogisticRegression model. */ + Table lrModelStream = + tEnv.fromDataStream(env.fromCollection(modelData), modelSchema).as("coefficient"); + LogisticRegressionModel lr = new LogisticRegressionModel(); + /* Saves online model to given path (tmpPath). */ + lr.setModelData(lrModelStream); + lr.save(tmpPath); + env.execute(); + + /* Constructs validated data table. */ + Table validDataTable = + tEnv.fromDataStream(env.fromCollection(validData), dataSchema).as("label, vec"); + + Process proc = Runtime.getRuntime().exec("ls " + tmpPath + "/data"); + BufferedReader bufferedReader = + new BufferedReader(new InputStreamReader(proc.getInputStream())); + + /* Loads every LogisticRegression model in model path and validates it. */ + String modelVersion; + while ((modelVersion = bufferedReader.readLine()) != null) { + if (!"metadata".equals(modelVersion)) { + LogisticRegressionModel lrModel = Review comment: OK -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
