Github user sethah commented on a diff in the pull request: https://github.com/apache/spark/pull/13796#discussion_r74832263 --- Diff: mllib/src/main/scala/org/apache/spark/ml/classification/LogisticRegression.scala --- @@ -944,13 +955,140 @@ class BinaryLogisticRegressionSummary private[classification] ( private class LogisticAggregator( private val numFeatures: Int, numClasses: Int, - fitIntercept: Boolean) extends Serializable { + fitIntercept: Boolean, + multinomial: Boolean, + standardize: Boolean) extends Serializable { private var weightSum = 0.0 private var lossSum = 0.0 + private val totalCoefficientLength = { + val cols = if (fitIntercept) numFeatures + 1 else numFeatures + val rows = if (multinomial) numClasses else math.max(1, numClasses - 1) + rows * cols + } + private val gradientSumArray = - Array.ofDim[Double](if (fitIntercept) numFeatures + 1 else numFeatures) + Array.ofDim[Double](totalCoefficientLength) + + /** Update gradient and loss using binary loss function. */ + private def binaryUpdateInPlace( + features: Vector, + weight: Double, + label: Double, + coefficients: Array[Double], + gradient: Array[Double], + featuresStd: Array[Double], + numFeaturesPlusIntercept: Int, + standardize: Boolean): Unit = { + val margin = - { + var sum = 0.0 + features.foreachActive { (index, value) => + if (featuresStd(index) != 0.0 && value != 0.0) { + val x = if (standardize) value / featuresStd(index) else value + sum += coefficients(index) * x + } + } + sum + { + if (fitIntercept) coefficients(numFeaturesPlusIntercept - 1) else 0.0 + } + } + + val multiplier = weight * (1.0 / (1.0 + math.exp(margin)) - label) + + features.foreachActive { (index, value) => + if (featuresStd(index) != 0.0 && value != 0.0) { + val x = if (standardize) value / featuresStd(index) else value + gradient(index) += multiplier * x + } + } + + if (fitIntercept) { + gradient(numFeaturesPlusIntercept - 1) += multiplier + } + + if (label > 0) { + // The following is equivalent to log(1 + exp(margin)) but more numerically stable. + lossSum += weight * MLUtils.log1pExp(margin) + } else { + lossSum += weight * (MLUtils.log1pExp(margin) - margin) + } + } + + /** Update gradient and loss using multinomial loss function. */ --- End diff -- I've replaced `multinomial` with `multinomial (softmax)` in a couple comments in the code. I think as long as we're clear on the problem formulation (which will be made explicit when the derivation is added to `LogisticAggregator`) then we shouldn't have to worry too much about semantics. Let me know if you had something else in mind or you see other places we should update.
--- 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 infrastruct...@apache.org or file a JIRA ticket with INFRA. --- --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org