-
Notifications
You must be signed in to change notification settings - Fork 28.9k
[SPARK-5726] [MLLIB] Elementwise (Hadamard) Vector Product Transformer #4580
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
4922722
cb520e6
e436896
1dffeee
37d4705
ded3ac6
4595165
90f7e39
fac12ad
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| /* | ||
| * 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.spark.ml.feature | ||
|
|
||
| import org.apache.spark.annotation.AlphaComponent | ||
| import org.apache.spark.ml.UnaryTransformer | ||
| import org.apache.spark.ml.param.Param | ||
| import org.apache.spark.mllib.feature | ||
| import org.apache.spark.mllib.linalg.{Vector, VectorUDT} | ||
| import org.apache.spark.sql.types.DataType | ||
|
|
||
| /** | ||
| * :: AlphaComponent :: | ||
| * Outputs the Hadamard product (i.e., the element-wise product) of each input vector with a | ||
| * provided "weight" vector. In other words, it scales each column of the dataset by a scalar | ||
| * multiplier. | ||
| */ | ||
| @AlphaComponent | ||
| class ElementwiseProduct extends UnaryTransformer[Vector, Vector, ElementwiseProduct] { | ||
|
|
||
| /** | ||
| * the vector to multiply with input vectors | ||
| * @group param | ||
| */ | ||
| val scalingVec: Param[Vector] = new Param(this, "scalingVector", "vector for hadamard product") | ||
|
|
||
| /** @group setParam */ | ||
| def setScalingVec(value: Vector): this.type = set(scalingVec, value) | ||
|
|
||
| /** @group getParam */ | ||
| def getScalingVec: Vector = getOrDefault(scalingVec) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add tag: |
||
|
|
||
| override protected def createTransformFunc: Vector => Vector = { | ||
| require(params.contains(scalingVec), s"transformation requires a weight vector") | ||
| val elemScaler = new feature.ElementwiseProduct($(scalingVec)) | ||
| elemScaler.transform | ||
| } | ||
|
|
||
| override protected def outputDataType: DataType = new VectorUDT() | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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. | ||
| */ | ||
|
|
||
| package org.apache.spark.mllib.feature | ||
|
|
||
| import org.apache.spark.annotation.Experimental | ||
| import org.apache.spark.mllib.linalg._ | ||
|
|
||
| /** | ||
| * :: Experimental :: | ||
| * Outputs the Hadamard product (i.e., the element-wise product) of each input vector with a | ||
| * provided "weight" vector. In other words, it scales each column of the dataset by a scalar | ||
| * multiplier. | ||
| * @param scalingVector The values used to scale the reference vector's individual components. | ||
| */ | ||
| @Experimental | ||
| class ElementwiseProduct(val scalingVector: Vector) extends VectorTransformer { | ||
|
|
||
| /** | ||
| * Does the hadamard product transformation. | ||
| * | ||
| * @param vector vector to be transformed. | ||
| * @return transformed vector. | ||
| */ | ||
| override def transform(vector: Vector): Vector = { | ||
| require(vector.size == scalingVector.size, | ||
| s"vector sizes do not match: Expected ${scalingVector.size} but found ${vector.size}") | ||
| vector match { | ||
| case dv: DenseVector => | ||
| val values: Array[Double] = dv.values.clone() | ||
| val dim = scalingVector.size | ||
| var i = 0 | ||
| while (i < dim) { | ||
| values(i) *= scalingVector(i) | ||
| i += 1 | ||
| } | ||
| Vectors.dense(values) | ||
| case SparseVector(size, indices, vs) => | ||
| val values = vs.clone() | ||
| val dim = values.length | ||
| var i = 0 | ||
| while (i < dim) { | ||
| values(i) *= scalingVector(indices(i)) | ||
| i += 1 | ||
| } | ||
| Vectors.sparse(size, indices, values) | ||
| case v => throw new IllegalArgumentException("Does not support vector type " + v.getClass) | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| /* | ||
| * 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.spark.mllib.feature | ||
|
|
||
| import org.scalatest.FunSuite | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please organize imports: Put non-Spark imports before Spark ones, with a blank line in between |
||
|
|
||
| import org.apache.spark.mllib.linalg.{DenseVector, SparseVector, Vectors} | ||
| import org.apache.spark.mllib.util.MLlibTestSparkContext | ||
| import org.apache.spark.mllib.util.TestingUtils._ | ||
|
|
||
| class ElementwiseProductSuite extends FunSuite with MLlibTestSparkContext { | ||
|
|
||
| test("elementwise (hadamard) product should properly apply vector to dense data set") { | ||
| val denseData = Array( | ||
| Vectors.dense(1.0, 4.0, 1.9, -9.0) | ||
| ) | ||
| val scalingVec = Vectors.dense(2.0, 0.5, 0.0, 0.25) | ||
| val transformer = new ElementwiseProduct(scalingVec) | ||
| val transformedData = transformer.transform(sc.makeRDD(denseData)) | ||
| val transformedVecs = transformedData.collect() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. collect() isn't guaranteed to return data in the same order. I'd recommend testing using per-row transform(); transforming RDDs is already tested elsewhere since you're using the VectorTransformer abstraction.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oops, I was confused. (There's only 1 element...) |
||
| val transformedVec = transformedVecs(0) | ||
| val expectedVec = Vectors.dense(2.0, 2.0, 0.0, -2.25) | ||
| assert(transformedVec ~== expectedVec absTol 1E-5, | ||
| s"Expected transformed vector $expectedVec but found $transformedVec") | ||
| } | ||
|
|
||
| test("elementwise (hadamard) product should properly apply vector to sparse data set") { | ||
| val sparseData = Array( | ||
| Vectors.sparse(3, Seq((1, -1.0), (2, -3.0))) | ||
| ) | ||
| val dataRDD = sc.parallelize(sparseData, 3) | ||
| val scalingVec = Vectors.dense(1.0, 0.0, 0.5) | ||
| val transformer = new ElementwiseProduct(scalingVec) | ||
| val data2 = sparseData.map(transformer.transform) | ||
| val data2RDD = transformer.transform(dataRDD) | ||
|
|
||
| assert((sparseData, data2, data2RDD.collect()).zipped.forall { | ||
| case (v1: DenseVector, v2: DenseVector, v3: DenseVector) => true | ||
| case (v1: SparseVector, v2: SparseVector, v3: SparseVector) => true | ||
| case _ => false | ||
| }, "The vector type should be preserved after hadamard product") | ||
|
|
||
| assert((data2, data2RDD.collect()).zipped.forall((v1, v2) => v1 ~== v2 absTol 1E-5)) | ||
| assert(data2(0) ~== Vectors.sparse(3, Seq((1, 0.0), (2, -1.5))) absTol 1E-5) | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add tag:
@group setParam