Github user BaiGang commented on a diff in the pull request:
https://github.com/apache/spark/pull/1237#discussion_r14283042
--- Diff:
mllib/src/main/scala/org/apache/spark/mllib/optimization/Gradient.scala ---
@@ -175,3 +175,80 @@ class HingeGradient extends Gradient {
}
}
}
+
+/**
+ * :: DeveloperApi ::
+ * Compute gradient and loss for MLE of Poisson Regression with log link
function.
+ * The gradient is calculated as follows:
+ * f' = x_i*(exp(x_i*w)-y_i)
+ */
+@DeveloperApi
+class PoissonGradient extends Gradient {
+ def fact(n: Int): Int =
+ (1 to n).foldLeft(1) { _ * _ }
+
+ override def compute(data: Vector, label: Double, weights: Vector):
(Vector, Double) = {
+ val brzData = data.toBreeze
+ val brzWeights = weights.toBreeze
+ val dotProd = brzWeights.dot(brzData)
+ val diff = math.exp(dotProd) - label
+ val loss = -dotProd * label + math.exp(dotProd) + fact(label.toInt)
--- End diff --
We can safely remove the fact(.) part, because it has virtually nothing to
do with the resulted weights.
---
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.
---