Github user jkbradley commented on a diff in the pull request:
https://github.com/apache/spark/pull/4906#discussion_r26179917
--- Diff:
mllib/src/main/scala/org/apache/spark/mllib/tree/model/treeEnsembleModels.scala
---
@@ -108,6 +110,53 @@ class GradientBoostedTreesModel(
}
override protected def formatVersion: String =
TreeEnsembleModel.SaveLoadV1_0.thisFormatVersion
+
+ /**
+ * Method to compute error or loss for every iteration of gradient
boosting.
+ * @param data: RDD of [[org.apache.spark.mllib.regression.LabeledPoint]]
+ * @param loss: evaluation metric.
+ * @return an array with index i having the losses or errors for the
ensemble
+ * containing trees 1 to i + 1
+ */
+ def evaluateEachIteration(
+ data: RDD[LabeledPoint],
+ loss: Loss) : Array[Double] = {
+
+ val sc = data.sparkContext
+ val remappedData = algo match {
+ case Classification => data.map(x => new LabeledPoint((x.label * 2)
- 1, x.features))
+ case _ => data
+ }
+ val initialTree = trees(0)
+ val numIterations = trees.length
+ val evaluationArray = Array.fill(numIterations)(0.0)
+
+ // Initial weight is 1.0
+ var predictionErrorModel = remappedData.map {i =>
+ val pred = initialTree.predict(i.features)
+ val error = loss.computeError(i, pred)
+ (pred, error)
+ }
+ evaluationArray(0) = predictionErrorModel.values.mean()
+
+ // Avoid the model being copied across numIterations.
+ val broadcastTrees = sc.broadcast(trees)
+ val broadcastWeights = sc.broadcast(treeWeights)
+
+ (1 until numIterations).map {nTree =>
+ predictionErrorModel = (remappedData zip predictionErrorModel) map {
+ case (point, (pred, error)) => {
+ val newPred = pred + (
+ broadcastTrees.value(nTree).predict(point.features) *
broadcastWeights.value(nTree))
+ val newError = loss.computeError(point, newPred)
+ (newPred, newError)
+ }
+ }
+ evaluationArray(nTree) = predictionErrorModel.values.mean()
+ }
+ evaluationArray
--- End diff --
You might want to explicitly unpersist the broadcast values before
returning. They will get unpersisted once their values go out of scope, but it
might take longer.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]