-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-32140][ML][PySpark] Add training summary to FMClassificationModel #28960
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
6 commits
Select commit
Hold shift + click to select a range
b450a50
[SPARK-32140][ML][PySpark] Add summary to FMClassificationModel
huaxingao 4263515
remove println
huaxingao 755b7f1
fix comment format
huaxingao ec25179
fix python style error
huaxingao 63a1eec
fix MiMa
huaxingao 9a58603
addres comments
huaxingao 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 |
|---|---|---|
|
|
@@ -129,7 +129,20 @@ class GradientDescent private[spark] (private var gradient: Gradient, private va | |
| * @return solution vector | ||
| */ | ||
| def optimize(data: RDD[(Double, Vector)], initialWeights: Vector): Vector = { | ||
| val (weights, _) = GradientDescent.runMiniBatchSGD( | ||
| val (weights, _) = optimizeWithLossReturned(data, initialWeights) | ||
| weights | ||
| } | ||
|
|
||
| /** | ||
| * Runs gradient descent on the given training data. | ||
| * @param data training data | ||
| * @param initialWeights initial weights | ||
| * @return solution vector and loss value in an array | ||
| */ | ||
| def optimizeWithLossReturned( | ||
| data: RDD[(Double, Vector)], | ||
| initialWeights: Vector): (Vector, Array[Double]) = { | ||
| GradientDescent.runMiniBatchSGD( | ||
| data, | ||
| gradient, | ||
| updater, | ||
|
|
@@ -139,7 +152,6 @@ class GradientDescent private[spark] (private var gradient: Gradient, private va | |
| miniBatchFraction, | ||
| initialWeights, | ||
| convergenceTol) | ||
| weights | ||
| } | ||
|
|
||
| } | ||
|
|
@@ -195,7 +207,7 @@ object GradientDescent extends Logging { | |
| s"numIterations=$numIterations and miniBatchFraction=$miniBatchFraction") | ||
| } | ||
|
|
||
| val stochasticLossHistory = new ArrayBuffer[Double](numIterations) | ||
| val stochasticLossHistory = new ArrayBuffer[Double](numIterations + 1) | ||
|
Contributor
Author
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. Make this stochasticLossHistory contain initial state + the state for each iteration, so it is consistent with the objectiveHistory in LogisticRegression and LinearRegression |
||
| // Record previous weight and current one to calculate solution vector difference | ||
|
|
||
| var previousWeights: Option[Vector] = None | ||
|
|
@@ -226,7 +238,7 @@ object GradientDescent extends Logging { | |
|
|
||
| var converged = false // indicates whether converged based on convergenceTol | ||
| var i = 1 | ||
| while (!converged && i <= numIterations) { | ||
| while (!converged && (i <= numIterations + 1)) { | ||
| val bcWeights = data.context.broadcast(weights) | ||
| // Sample a subset (fraction miniBatchFraction) of the total data | ||
| // compute and sum up the subgradients on this subset (this is one map-reduce) | ||
|
|
@@ -249,17 +261,19 @@ object GradientDescent extends Logging { | |
| * and regVal is the regularization value computed in the previous iteration as well. | ||
| */ | ||
| stochasticLossHistory += lossSum / miniBatchSize + regVal | ||
| val update = updater.compute( | ||
| weights, Vectors.fromBreeze(gradientSum / miniBatchSize.toDouble), | ||
| stepSize, i, regParam) | ||
| weights = update._1 | ||
| regVal = update._2 | ||
|
|
||
| previousWeights = currentWeights | ||
| currentWeights = Some(weights) | ||
| if (previousWeights != None && currentWeights != None) { | ||
| converged = isConverged(previousWeights.get, | ||
| currentWeights.get, convergenceTol) | ||
| if (i != (numIterations + 1)) { | ||
| val update = updater.compute( | ||
| weights, Vectors.fromBreeze(gradientSum / miniBatchSize.toDouble), | ||
| stepSize, i, regParam) | ||
| weights = update._1 | ||
| regVal = update._2 | ||
|
|
||
| previousWeights = currentWeights | ||
| currentWeights = Some(weights) | ||
| if (previousWeights != None && currentWeights != None) { | ||
| converged = isConverged(previousWeights.get, | ||
| currentWeights.get, convergenceTol) | ||
| } | ||
| } | ||
| } else { | ||
| logWarning(s"Iteration ($i/$numIterations). The size of sampled batch is zero") | ||
|
|
@@ -271,7 +285,6 @@ object GradientDescent extends Logging { | |
| stochasticLossHistory.takeRight(10).mkString(", "))) | ||
|
|
||
| (weights, stochasticLossHistory.toArray) | ||
|
|
||
| } | ||
|
|
||
| /** | ||
|
|
||
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
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.
Add
with HasWeightColbecauseClassificationSummaryuses weigthCol. However, FM doesn't really support instance weight yet and all the weight are default to 1.0.