-
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
Closed
Closed
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
071ce88
add hinge agg
YY-OnCall 87c7279
resolve conflict
YY-OnCall c8228fb
add unit test
YY-OnCall 94e0250
add 1 un
YY-OnCall bc33cf9
Merge remote-tracking branch 'upstream/master' into svcAggregator
YY-OnCall 3d7bfce
unit test update
YY-OnCall b19ca41
Merge remote-tracking branch 'upstream/master' into svcAggregator
YY-OnCall 30e8646
Merge remote-tracking branch 'upstream/master' into svcAggregator
YY-OnCall 4173084
update std case
YY-OnCall File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
mllib/src/main/scala/org/apache/spark/ml/optim/aggregator/HingeAggregator.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
It seems this only support hinge loss currently. BTW, if we support squared hinge in the future, what is the best way? Add a param loss function for
HingeAggregatoror just add a newSquaredHingeAggregator? The later way should be more clear, but with more code.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.
I would prefer to use SquaredHingeAggregator. API-wise, it looks more intuitive and consistent to me. We can continue the review in the other LinearSVC PR.
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.
Yeah, I agree you for separate
SquaredHingeAggregator. Then we should removesquared_hingefrom here?BTW, you missed right parenthesis here.