-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-21108] [ML] convert LinearSVC to aggregator framework #18315
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 4 commits
071ce88
87c7279
c8228fb
94e0250
bc33cf9
3d7bfce
b19ca41
30e8646
4173084
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,106 @@ | ||
| /* | ||
| * 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.optim.aggregator | ||
|
|
||
| import org.apache.spark.broadcast.Broadcast | ||
| import org.apache.spark.ml.feature.Instance | ||
| import org.apache.spark.ml.linalg._ | ||
|
|
||
| /** | ||
| * HingeAggregator computes the gradient and loss for loss function ("hinge" or | ||
| * "squared_hinge", as used in binary classification for instances in sparse or dense | ||
|
||
| * vector in an online fashion. | ||
| * | ||
| * Two HingeAggregators can be merged together to have a summary of loss and gradient of | ||
| * the corresponding joint dataset. | ||
| * | ||
| * This class standardizes feature values during computation using bcFeaturesStd. | ||
| * | ||
| * @param bcCoefficients The coefficients corresponding to the features. | ||
| * @param fitIntercept Whether to fit an intercept term. | ||
| * @param bcFeaturesStd The standard deviation values of the features. | ||
| */ | ||
| private[ml] class HingeAggregator( | ||
| bcFeaturesStd: Broadcast[Array[Double]], | ||
| fitIntercept: Boolean)(bcCoefficients: Broadcast[Vector]) | ||
| extends DifferentiableLossAggregator[Instance, HingeAggregator] { | ||
|
|
||
| private val numFeatures: Int = bcFeaturesStd.value.length | ||
| private val numFeaturesPlusIntercept: Int = if (fitIntercept) numFeatures + 1 else numFeatures | ||
| @transient private lazy val coefficientsArray = bcCoefficients.value match { | ||
| case DenseVector(values) => values | ||
| case _ => throw new IllegalArgumentException(s"coefficients only supports dense vector" + | ||
| s" but got type ${bcCoefficients.value.getClass}.") | ||
| } | ||
| protected override val dim: Int = numFeaturesPlusIntercept | ||
|
|
||
| /** | ||
| * Add a new training instance to this HingeAggregator, and update the loss and gradient | ||
| * of the objective function. | ||
| * | ||
| * @param instance The instance of data point to be added. | ||
| * @return This HingeAggregator object. | ||
| */ | ||
| def add(instance: Instance): this.type = { | ||
| instance match { case Instance(label, weight, features) => | ||
| require(numFeatures == features.size, s"Dimensions mismatch when adding new instance." + | ||
| s" Expecting $numFeatures but got ${features.size}.") | ||
| require(weight >= 0.0, s"instance weight, $weight has to be >= 0.0") | ||
|
|
||
| if (weight == 0.0) return this | ||
| val localFeaturesStd = bcFeaturesStd.value | ||
| val localCoefficients = coefficientsArray | ||
| val localGradientSumArray = gradientSumArray | ||
|
|
||
| val dotProduct = { | ||
| var sum = 0.0 | ||
| features.foreachActive { (index, value) => | ||
| if (localFeaturesStd(index) != 0.0 && value != 0.0) { | ||
| sum += localCoefficients(index) * value / localFeaturesStd(index) | ||
| } | ||
| } | ||
| if (fitIntercept) sum += localCoefficients(numFeaturesPlusIntercept - 1) | ||
| sum | ||
| } | ||
| // Our loss function with {0, 1} labels is max(0, 1 - (2y - 1) (f_w(x))) | ||
| // Therefore the gradient is -(2y - 1)*x | ||
| val labelScaled = 2 * label - 1.0 | ||
| val loss = if (1.0 > labelScaled * dotProduct) { | ||
| (1.0 - labelScaled * dotProduct) * weight | ||
| } else { | ||
| 0.0 | ||
| } | ||
|
|
||
| if (1.0 > labelScaled * dotProduct) { | ||
| val gradientScale = -labelScaled * weight | ||
| features.foreachActive { (index, value) => | ||
| if (localFeaturesStd(index) != 0.0 && value != 0.0) { | ||
| localGradientSumArray(index) += value * gradientScale / localFeaturesStd(index) | ||
| } | ||
| } | ||
| if (fitIntercept) { | ||
| localGradientSumArray(localGradientSumArray.length - 1) += gradientScale | ||
| } | ||
| } | ||
|
|
||
| lossSum += loss | ||
| weightSum += weight | ||
| this | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,7 +25,8 @@ import org.apache.spark.SparkFunSuite | |
| import org.apache.spark.ml.classification.LinearSVCSuite._ | ||
| import org.apache.spark.ml.feature.{Instance, LabeledPoint} | ||
| import org.apache.spark.ml.linalg.{DenseVector, SparseVector, Vector, Vectors} | ||
| import org.apache.spark.ml.param.{ParamMap, ParamsSuite} | ||
| import org.apache.spark.ml.optim.aggregator.HingeAggregator | ||
| import org.apache.spark.ml.param.ParamsSuite | ||
| import org.apache.spark.ml.util.{DefaultReadWriteTest, MLTestingUtils} | ||
| import org.apache.spark.ml.util.TestingUtils._ | ||
| import org.apache.spark.mllib.util.MLlibTestSparkContext | ||
|
|
@@ -173,7 +174,7 @@ class LinearSVCSuite extends SparkFunSuite with MLlibTestSparkContext with Defau | |
| test("sparse coefficients in SVCAggregator") { | ||
|
||
| val bcCoefficients = spark.sparkContext.broadcast(Vectors.sparse(2, Array(0), Array(1.0))) | ||
| val bcFeaturesStd = spark.sparkContext.broadcast(Array(1.0)) | ||
| val agg = new LinearSVCAggregator(bcCoefficients, bcFeaturesStd, true) | ||
| val agg = new HingeAggregator(bcFeaturesStd, true)(bcCoefficients) | ||
| val thrown = withClue("LinearSVCAggregator cannot handle sparse coefficients") { | ||
| intercept[IllegalArgumentException] { | ||
| agg.add(Instance(1.0, 1.0, Vectors.dense(1.0))) | ||
|
|
||
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.
Minor: The third argument
applyFeaturesStdis a function rather than an array in semantics:In LiR and LoR, we use a function:
I think either is ok, but it's better to keep consistent with other algorithms. We can change here to use function or change the third argument of
L2RegularizationtoOption[Array[Double]]. I'm prefer the former way, what's your opinion? Thanks.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.
Sure. Will change it to function