Github user sethah commented on a diff in the pull request:

    https://github.com/apache/spark/pull/13796#discussion_r75204183
  
    --- Diff: 
mllib/src/main/scala/org/apache/spark/ml/classification/LogisticRegression.scala
 ---
    @@ -1082,57 +1343,62 @@ private class LogisticCostFun(
         fitIntercept: Boolean,
         standardization: Boolean,
         bcFeaturesStd: Broadcast[Array[Double]],
    -    regParamL2: Double) extends DiffFunction[BDV[Double]] {
    +    regParamL2: Double,
    +    multinomial: Boolean) extends DiffFunction[BDV[Double]] {
     
       val featuresStd = bcFeaturesStd.value
     
       override def calculate(coefficients: BDV[Double]): (Double, BDV[Double]) 
= {
    -    val numFeatures = featuresStd.length
         val coeffs = Vectors.fromBreeze(coefficients)
         val bcCoeffs = instances.context.broadcast(coeffs)
    -    val n = coeffs.size
    +    val localFeaturesStd = featuresStd
    +    val numFeatures = localFeaturesStd.length
    +    val numFeaturesPlusIntercept = if (fitIntercept) numFeatures + 1 else 
numFeatures
     
         val logisticAggregator = {
    -      val seqOp = (c: LogisticAggregator, instance: Instance) => 
c.add(instance)
    +      val seqOp = (c: LogisticAggregator, instance: Instance) =>
    +        c.add(instance)
           val combOp = (c1: LogisticAggregator, c2: LogisticAggregator) => 
c1.merge(c2)
     
           instances.treeAggregate(
    -        new LogisticAggregator(bcCoeffs, bcFeaturesStd, numFeatures, 
numClasses, fitIntercept)
    +        new LogisticAggregator(bcCoeffs, bcFeaturesStd, numFeatures, 
numClasses, fitIntercept,
    +          multinomial)
           )(seqOp, combOp)
         }
     
         val totalGradientArray = logisticAggregator.gradient.toArray
    -
         // regVal is the sum of coefficients squares excluding intercept for 
L2 regularization.
         val regVal = if (regParamL2 == 0.0) {
           0.0
         } else {
    +      val K = if (multinomial) numClasses else numClasses - 1
           var sum = 0.0
    -      coeffs.foreachActive { (index, value) =>
    -        // If `fitIntercept` is true, the last term which is intercept 
doesn't
    -        // contribute to the regularization.
    -        if (index != numFeatures) {
    +      (0 until K).foreach { k =>
    +        var j = 0
    +        while (j < numFeatures) {
               // The following code will compute the loss of the 
regularization; also
               // the gradient of the regularization, and add back to 
totalGradientArray.
    +          val value = coeffs(k * numFeaturesPlusIntercept + j)
    --- End diff --
    
    Good suggestion. I changed this to use `forEachActive`.


---
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]

Reply via email to