Github user dbtsai commented on the pull request:
https://github.com/apache/spark/pull/1379#issuecomment-67694284
@avulanov I don't check your implementation yet, but I'm ready to have the
optimized MLOR for you to test. Can you try the `LogisticGradient` in
https://github.com/AlpineNow/spark/commits/mlor
```scala
@DeveloperApi
class LogisticGradient extends Gradient {
override def compute(data: Vector, label: Double, weights: Vector):
(Vector, Double) = {
val gradient = Vectors.zeros(weights.size)
val loss = compute(data, label, weights, gradient)
(gradient, loss)
}
override def compute(
data: Vector,
label: Double,
weights: Vector,
cumGradient: Vector): Double = {
assert((weights.size % data.size) == 0)
val dataSize = data.size
// (n + 1) is number of classes
val n = (weights.size / dataSize)
val numerators = Array.ofDim[Double](n)
var denominator = 0.0
var margin = 0.0
val weightsArray = weights match {
case dv: DenseVector => dv.values
case _ =>
throw new IllegalArgumentException(
s"weights only supports dense vector but got type
${weights.getClass}.")
}
val cumGradientArray = cumGradient match {
case dv: DenseVector => dv.values
case _ =>
throw new IllegalArgumentException(
s"cumGradient only supports dense vector but got type
${cumGradient.getClass}.")
}
var i = 0
while (i < n) {
var sum = 0.0
data.foreachActive { (index, value) =>
if (value != 0.0) sum += value * weightsArray((i * dataSize) +
index)
}
if (i == label.toInt - 1) margin = sum
numerators(i) = math.exp(sum)
denominator += numerators(i)
i += 1
}
i = 0
while (i < n) {
val multiplier = numerators(i) / (denominator + 1.0) - {
if (label != 0.0 && label == i + 1) 1.0 else 0.0
}
data.foreachActive { (index, value) =>
if (value != 0.0) cumGradientArray(i * dataSize + index) +=
multiplier * value
}
i += 1
}
if (label > 0.0) {
math.log1p(denominator) - margin
} else {
math.log1p(denominator)
}
}
}
```
---
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]