zhipeng93 commented on a change in pull request #28: URL: https://github.com/apache/flink-ml/pull/28#discussion_r757227812
########## File path: flink-ml-lib/src/main/java/org/apache/flink/ml/classification/linear/LogisticRegressionModel.java ########## @@ -0,0 +1,213 @@ +/* + * 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.linear; + +import org.apache.flink.api.common.eventtime.WatermarkStrategy; +import org.apache.flink.api.common.functions.RichMapFunction; +import org.apache.flink.api.common.typeinfo.BasicTypeInfo; +import org.apache.flink.api.common.typeinfo.PrimitiveArrayTypeInfo; +import org.apache.flink.api.common.typeinfo.TypeInformation; +import org.apache.flink.api.connector.source.Source; +import org.apache.flink.api.java.tuple.Tuple2; +import org.apache.flink.api.java.typeutils.RowTypeInfo; +import org.apache.flink.connector.file.sink.FileSink; +import org.apache.flink.connector.file.src.FileSource; +import org.apache.flink.core.fs.Path; +import org.apache.flink.ml.api.core.Model; +import org.apache.flink.ml.classification.linear.LogisticRegressionModelData.LogisticRegressionModelDataEncoder; +import org.apache.flink.ml.classification.linear.LogisticRegressionModelData.LogisticRegressionModelDataStreamFormat; +import org.apache.flink.ml.common.broadcast.BroadcastUtils; +import org.apache.flink.ml.common.linalg.BLAS; +import org.apache.flink.ml.param.Param; +import org.apache.flink.ml.util.ParamUtils; +import org.apache.flink.ml.util.ReadWriteUtils; +import org.apache.flink.streaming.api.datastream.DataStream; +import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment; +import org.apache.flink.streaming.api.functions.sink.filesystem.bucketassigners.BasePathBucketAssigner; +import org.apache.flink.streaming.api.functions.sink.filesystem.rollingpolicies.OnCheckpointRollingPolicy; +import org.apache.flink.streaming.api.operators.AbstractUdfStreamOperator; +import org.apache.flink.streaming.api.operators.OneInputStreamOperator; +import org.apache.flink.streaming.runtime.streamrecord.StreamRecord; +import org.apache.flink.table.api.Table; +import org.apache.flink.table.api.bridge.java.StreamTableEnvironment; +import org.apache.flink.table.api.internal.TableImpl; +import org.apache.flink.types.Row; + +import java.io.IOException; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +/** This class implements {@link Model} for {@link LogisticRegression}. */ +public class LogisticRegressionModel + implements Model<LogisticRegressionModel>, + LogisticRegressionModelParams<LogisticRegressionModel> { + + private Map<Param<?>, Object> paramMap = new HashMap<>(); + + private Table model; + + public LogisticRegressionModel() { + ParamUtils.initializeMapWithDefaultValues(this.paramMap, this); + } + + @Override + public Map<Param<?>, Object> getParamMap() { + return paramMap; + } + + @Override + public void save(String path) throws IOException { + StreamTableEnvironment tEnv = + (StreamTableEnvironment) ((TableImpl) model).getTableEnvironment(); + String dataPath = ReadWriteUtils.getDataPath(path); + FileSink<LogisticRegressionModelData> sink = + FileSink.forRowFormat(new Path(dataPath), new LogisticRegressionModelDataEncoder()) + .withRollingPolicy(OnCheckpointRollingPolicy.build()) + .withBucketAssigner(new BasePathBucketAssigner<>()) + .build(); + ReadWriteUtils.saveMetadata(this, path); + tEnv.toDataStream(model) + .map(x -> (LogisticRegressionModelData) x.getField(0)) + .sinkTo(sink) + .setParallelism(1); + } + + public static LogisticRegressionModel load(StreamExecutionEnvironment env, String path) + throws IOException { + StreamTableEnvironment tEnv = StreamTableEnvironment.create(env); + Source<LogisticRegressionModelData, ?, ?> source = + FileSource.forRecordStreamFormat( + new LogisticRegressionModelDataStreamFormat(), + ReadWriteUtils.getDataPaths(path)) + .build(); + LogisticRegressionModel model = ReadWriteUtils.loadStageParam(path); + DataStream<LogisticRegressionModelData> modelData = + env.fromSource(source, WatermarkStrategy.noWatermarks(), "modelData"); + model.setModelData(tEnv.fromDataStream(modelData)); + return model; + } + + @Override + public LogisticRegressionModel setModelData(Table... inputs) { + model = inputs[0]; + return this; + } + + @Override + public Table[] getModelData() { + return new Table[] {model}; + } + + @Override + public Table[] transform(Table... inputs) { + StreamTableEnvironment tEnv = + (StreamTableEnvironment) ((TableImpl) inputs[0]).getTableEnvironment(); + + DataStream<Row> data = tEnv.toDataStream(inputs[0]); + final String broadcastModelName = "broadcastModel"; + DataStream<LogisticRegressionModelData> modelData = + tEnv.toDataStream(model).map(x -> (LogisticRegressionModelData) x.getField(0)); Review comment: Nice improvement! We can actually go one more step --- - add two more static method in `ReadWriteUtils`, i.e., `static void saveModelData(Table model, String path, ModelEncoder encoder)` and `static Table loadModelData(StreamExecutionEnvironment env, String path, ModelDecoder decoder)`. - add two more static method in `LogisticRegressionModelData`, i.e., `getModelDataEncoder` and `getModelDataDecoder`. In this case, we can cover everything about model data in one class. We may probably come up with a new interface here. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@flink.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org