-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-4583] [mllib] LogLoss for GradientBoostedTrees fix + doc updates #3439
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 2 commits
e57897a
5e52bff
ed5da2c
a27eb6d
cfec17e
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 |
|---|---|---|
|
|
@@ -17,50 +17,48 @@ | |
|
|
||
| package org.apache.spark.mllib.tree.loss | ||
|
|
||
| import org.apache.spark.SparkContext._ | ||
| import org.apache.spark.annotation.DeveloperApi | ||
| import org.apache.spark.mllib.regression.LabeledPoint | ||
| import org.apache.spark.mllib.tree.model.TreeEnsembleModel | ||
| import org.apache.spark.rdd.RDD | ||
|
|
||
| /** | ||
| * :: DeveloperApi :: | ||
| * Class for least squares error loss calculation. | ||
| * Class for squared error loss calculation. | ||
| * | ||
| * The features x and the corresponding label y is predicted using the function F. | ||
| * For each instance: | ||
| * Loss: (y - F)**2/2 | ||
| * Negative gradient: y - F | ||
| * The squared (L2) error is defined as: | ||
| * (y - F(x))**2 | ||
| * where y is the label and F(x) is the model prediction for features x. | ||
| */ | ||
| @DeveloperApi | ||
| object SquaredError extends Loss { | ||
|
|
||
| /** | ||
| * Method to calculate the gradients for the gradient boosting calculation for least | ||
| * squares error calculation. | ||
| * @param model Model of the weak learner | ||
| * The gradient with respect to F(x) is: - 2 (y - F(x)) | ||
| * @param model Ensemble model | ||
| * @param point Instance of the training dataset | ||
| * @return Loss gradient | ||
| */ | ||
| override def gradient( | ||
| model: TreeEnsembleModel, | ||
| point: LabeledPoint): Double = { | ||
| model.predict(point.features) - point.label | ||
| 2.0 * (model.predict(point.features) - point.label) | ||
| } | ||
|
|
||
| /** | ||
| * Method to calculate error of the base learner for the gradient boosting calculation. | ||
| * Method to calculate loss of the base learner for the gradient boosting calculation. | ||
| * Note: This method is not used by the gradient boosting algorithm but is useful for debugging | ||
| * purposes. | ||
| * @param model Model of the weak learner. | ||
| * @param model Ensemble model | ||
| * @param data Training dataset: RDD of [[org.apache.spark.mllib.regression.LabeledPoint]]. | ||
| * @return | ||
| * @return Mean squared error of model on data | ||
|
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.
Member
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. I'll remove the 1/2. It's probably better to have an odd loss (which only experts need to know about) than to have an odd name (which everyone needs to recognize). |
||
| */ | ||
| override def computeError(model: TreeEnsembleModel, data: RDD[LabeledPoint]): Double = { | ||
| data.map { y => | ||
| val err = model.predict(y.features) - y.label | ||
| err * err | ||
| }.mean() | ||
| } | ||
|
|
||
| } | ||
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.
There is an issue with numerical stability. Maybe we can fix it in this PR. The problem appears when
w = -2.0 * point.label * predictionis large.math.exp(w)would overflow whilemath.log(1 + math.exp(w))should be close tow. Whenw < 0, we can useOtherwise, we should use
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.
Will do!