-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-2979][MLlib] Improve the convergence rate by minimizing the condition number #1897
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 all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
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 |
|---|---|---|
|
|
@@ -185,6 +185,63 @@ class LogisticRegressionSuite extends FunSuite with LocalSparkContext with Match | |
| // Test prediction on Array. | ||
| validatePrediction(validationData.map(row => model.predict(row.features)), validationData) | ||
| } | ||
|
|
||
| test("numerical stability of scaling features using logistic regression with LBFGS") { | ||
| /** | ||
| * If we rescale the features, the condition number will be changed so the convergence rate | ||
| * and the solution will not equal to the original solution multiple by the scaling factor | ||
| * which it should be. | ||
| * | ||
| * However, since in the LogisticRegressionWithLBFGS, we standardize the training dataset first, | ||
| * no matter how we multiple a scaling factor into the dataset, the convergence rate should be | ||
| * the same, and the solution should equal to the original solution multiple by the scaling | ||
| * factor. | ||
| */ | ||
|
|
||
| val nPoints = 10000 | ||
| val A = 2.0 | ||
| val B = -1.5 | ||
|
|
||
| val testData = LogisticRegressionSuite.generateLogisticInput(A, B, nPoints, 42) | ||
|
|
||
| val initialWeights = Vectors.dense(0.0) | ||
|
|
||
| val testRDD1 = sc.parallelize(testData, 2) | ||
|
|
||
| val testRDD2 = sc.parallelize( | ||
| testData.map(x => LabeledPoint(x.label, Vectors.fromBreeze(x.features.toBreeze * 1.0E3))), 2) | ||
|
|
||
| val testRDD3 = sc.parallelize( | ||
| testData.map(x => LabeledPoint(x.label, Vectors.fromBreeze(x.features.toBreeze * 1.0E6))), 2) | ||
|
|
||
| testRDD1.cache() | ||
| testRDD2.cache() | ||
| testRDD3.cache() | ||
|
|
||
| val lrA = new LogisticRegressionWithLBFGS().setIntercept(true) | ||
| val lrB = new LogisticRegressionWithLBFGS().setIntercept(true).setFeatureScaling(false) | ||
|
|
||
| val modelA1 = lrA.run(testRDD1, initialWeights) | ||
| val modelA2 = lrA.run(testRDD2, initialWeights) | ||
| val modelA3 = lrA.run(testRDD3, initialWeights) | ||
|
|
||
| val modelB1 = lrB.run(testRDD1, initialWeights) | ||
| val modelB2 = lrB.run(testRDD2, initialWeights) | ||
| val modelB3 = lrB.run(testRDD3, initialWeights) | ||
|
|
||
| // For model trained with feature standardization, the weights should | ||
| // be the same in the scaled space. Note that the weights here are already | ||
| // in the original space, we transform back to scaled space to compare. | ||
| assert(modelA1.weights(0) ~== modelA2.weights(0) * 1.0E3 absTol 0.01) | ||
| assert(modelA1.weights(0) ~== modelA3.weights(0) * 1.0E6 absTol 0.01) | ||
|
|
||
| // Training data with different scales without feature standardization | ||
| // will not yield the same result in the scaled space due to poor | ||
| // convergence rate. | ||
| assert(modelB1.weights(0) !~== modelB2.weights(0) * 1.0E3 absTol 0.1) | ||
|
Contributor
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. need a comment about the purpose of the tests here |
||
| assert(modelB1.weights(0) !~== modelB3.weights(0) * 1.0E6 absTol 0.1) | ||
| } | ||
|
|
||
| } | ||
|
|
||
| class LogisticRegressionClusterSuite extends FunSuite with LocalClusterSparkContext { | ||
|
|
||
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.
should use
inputitselfThere 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's not identical map. It's converting labeledPoint to tuple of response and feature vector for optimizer.
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.
Sorry, I didn't realize that.