yunfengzhou-hub commented on code in PR #135: URL: https://github.com/apache/flink-ml/pull/135#discussion_r934072380
########## flink-ml-python/pyflink/ml/lib/feature/elementwiseproduct.py: ########## @@ -0,0 +1,73 @@ +################################################################################ +# 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. +################################################################################ + +from pyflink.ml.core.param import ParamValidators, Param, VectorParam +from pyflink.ml.core.wrapper import JavaWithParams +from pyflink.ml.lib.feature.common import JavaFeatureTransformer +from pyflink.ml.lib.param import HasInputCol, HasOutputCol +from pyflink.ml.core.linalg import Vector + + +class _ElementwiseProductParams( + JavaWithParams, + HasInputCol, + HasOutputCol +): + """ + Params for :class:`ElementwiseProduct`. + """ + + SCALING_VEC: Param[Vector] = VectorParam( + "scaling_vec", + "The scaling vector of the product op.", + None, + ParamValidators.not_null()) + + def __init__(self, java_params): + super(_ElementwiseProductParams, self).__init__(java_params) + + def set_scaling_vec(self, value: Vector): + return self.set(self.SCALING_VEC, value) + + def get_scaling_vec(self) -> Vector: + return self.get(self.SCALING_VEC) + + @property + def scaling_vec(self) -> Vector: + return self.get_scaling_vec() + + +class ElementwiseProduct(JavaFeatureTransformer, _ElementwiseProductParams): + """ + A transformer that combines a given list of input columns into a vector column. Types of + input columns must be either vector or numerical value. + + The `keep` option of :class:HasHandleInvalid means that we output bad rows with output column + set to null. Review Comment: Let's keep the description the same as that in Java. ########## flink-ml-python/pyflink/examples/ml/feature/elementwiseproduct_example.py: ########## @@ -0,0 +1,64 @@ +################################################################################ +# 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. +################################################################################ + +# Simple program that creates a ElementwiseProduct instance and uses it for feature +# engineering. +# +# Before executing this program, please make sure you have followed Flink ML's +# quick start guideline to set up Flink ML and Flink environment. The guideline +# can be found at +# +# https://nightlies.apache.org/flink/flink-ml-docs-master/docs/try-flink-ml/quick-start/ + +from pyflink.common import Types +from pyflink.datastream import StreamExecutionEnvironment +from pyflink.ml.core.linalg import Vectors, DenseVectorTypeInfo +from pyflink.ml.lib.feature.elementwiseproduct import ElementwiseProduct +from pyflink.table import StreamTableEnvironment + +# create a new StreamExecutionEnvironment +env = StreamExecutionEnvironment.get_execution_environment() + +# create a StreamTableEnvironment +t_env = StreamTableEnvironment.create(env) + +# generate input data +input_data_table = t_env.from_data_stream( + env.from_collection([ + (1, Vectors.dense(2.1, 3.1)), + (2, Vectors.dense(1.1, 3.3)) + ], + type_info=Types.ROW_NAMED( + ['id', 'vec'], + [Types.INT(), DenseVectorTypeInfo()]))) + +# create a elementwise product object and initialize its parameters Review Comment: nit: `an`. ########## flink-ml-python/pyflink/ml/lib/feature/elementwiseproduct.py: ########## @@ -0,0 +1,73 @@ +################################################################################ +# 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. +################################################################################ + +from pyflink.ml.core.param import ParamValidators, Param, VectorParam +from pyflink.ml.core.wrapper import JavaWithParams +from pyflink.ml.lib.feature.common import JavaFeatureTransformer +from pyflink.ml.lib.param import HasInputCol, HasOutputCol +from pyflink.ml.core.linalg import Vector + + +class _ElementwiseProductParams( + JavaWithParams, + HasInputCol, + HasOutputCol +): + """ + Params for :class:`ElementwiseProduct`. + """ + + SCALING_VEC: Param[Vector] = VectorParam( + "scaling_vec", + "The scaling vector of the product op.", Review Comment: Let's keep the description the same as that in Java. ########## flink-ml-python/pyflink/ml/core/param.py: ########## @@ -333,6 +334,17 @@ def __init__(self, name: str, description: str, default_value: Optional[str], super(StringParam, self).__init__(name, str, "str", description, default_value, validator) +class VectorParam(Param[Vector]): Review Comment: Let's add test cases for this parameter in python as well. Relevant test cases seem to be in `test_stage.py`. ########## flink-ml-lib/src/main/java/org/apache/flink/ml/feature/elementwiseproduct/ElementwiseProduct.java: ########## @@ -0,0 +1,112 @@ +/* + * 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.feature.elementwiseproduct; + +import org.apache.flink.api.common.functions.MapFunction; +import org.apache.flink.api.java.typeutils.RowTypeInfo; +import org.apache.flink.ml.api.Transformer; +import org.apache.flink.ml.common.datastream.TableUtils; +import org.apache.flink.ml.linalg.BLAS; +import org.apache.flink.ml.linalg.Vector; +import org.apache.flink.ml.linalg.typeinfo.VectorTypeInfo; +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.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 org.apache.flink.util.Preconditions; + +import org.apache.commons.lang3.ArrayUtils; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +/** + * ElementwiseProduct is transformer that multiplies each input vector with a given scaling vector + * element by element. ElementwiseProduct has another name called Hadamard product. + * + * <p>If input vector is null, then return null value instead of product result. + * + * <p>If input vector size not equals scaling vector size then throw Exceptions. Review Comment: Could you please refractor these two paragraphs? Currently, there are some grammar errors and ambiguity in them. For example, who will return what kind of exception? ########## flink-ml-python/pyflink/ml/lib/feature/tests/test_elementwiseproduct.py: ########## @@ -0,0 +1,76 @@ +################################################################################ +# 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. +################################################################################ +import os + +from pyflink.common import Types + +from pyflink.ml.core.linalg import Vectors, DenseVectorTypeInfo +from pyflink.ml.lib.feature.elementwiseproduct import ElementwiseProduct +from pyflink.ml.tests.test_utils import PyFlinkMLTestCase + + +class ElementwiseProductTest(PyFlinkMLTestCase): + def setUp(self): + super(ElementwiseProductTest, self).setUp() + self.input_data_table = self.t_env.from_data_stream( + self.env.from_collection([ + (0, + Vectors.dense(2.1, 3.1)), + (1, + Vectors.dense(1.1, 3.3)), + ], + type_info=Types.ROW_NAMED( + ['id', 'vec'], + [Types.INT(), DenseVectorTypeInfo()]))) + + self.expected_output_data_1 = Vectors.dense(2.3100000000000005, 3.4100000000000006) + self.expected_output_data_2 = Vectors.dense(1.2100000000000002, 3.63) + + def test_param(self): + elementwise_product = ElementwiseProduct() + + self.assertEqual('input', elementwise_product.get_input_col()) + self.assertEqual('output', elementwise_product.get_output_col()) + + elementwise_product.set_input_col('vec') \ + .set_output_col('output_vec') \ + .set_scaling_vec(Vectors.dense(1.1, 1.1)) + + self.assertEqual('vec', elementwise_product.get_input_col()) + self.assertEqual(Vectors.dense(1.1, 1.1), elementwise_product.get_scaling_vec()) + self.assertEqual('output_vec', elementwise_product.get_output_col()) + + def test_save_load_transform(self): + elementwise_product = ElementwiseProduct() \ + .set_input_col('vec') \ + .set_output_col('output_vec') \ + .set_scaling_vec(Vectors.dense(1.1, 1.1)) + + path = os.path.join(self.temp_dir, 'test_save_load_transform_elementwise_product') + elementwise_product.save(path) + elementwise_product = ElementwiseProduct.load(self.t_env, path) + + output_table = elementwise_product.transform(self.input_data_table)[0] + actual_outputs = [(result[0], result[2]) for result in + self.t_env.to_data_stream(output_table).execute_and_collect()] + + for actual_output in actual_outputs: Review Comment: Let's check the size of `actual_outputs` here. ########## flink-ml-python/pyflink/ml/lib/feature/tests/test_elementwiseproduct.py: ########## @@ -0,0 +1,76 @@ +################################################################################ +# 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. +################################################################################ +import os + +from pyflink.common import Types + +from pyflink.ml.core.linalg import Vectors, DenseVectorTypeInfo +from pyflink.ml.lib.feature.elementwiseproduct import ElementwiseProduct +from pyflink.ml.tests.test_utils import PyFlinkMLTestCase + + +class ElementwiseProductTest(PyFlinkMLTestCase): + def setUp(self): + super(ElementwiseProductTest, self).setUp() + self.input_data_table = self.t_env.from_data_stream( + self.env.from_collection([ + (0, + Vectors.dense(2.1, 3.1)), + (1, + Vectors.dense(1.1, 3.3)), + ], + type_info=Types.ROW_NAMED( + ['id', 'vec'], + [Types.INT(), DenseVectorTypeInfo()]))) + + self.expected_output_data_1 = Vectors.dense(2.3100000000000005, 3.4100000000000006) + self.expected_output_data_2 = Vectors.dense(1.2100000000000002, 3.63) Review Comment: Let's apply the changes in Java to Python test cases as well. -- 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]
