spark-reviews mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dbtsai <...@git.apache.org>
Subject [GitHub] spark pull request #13796: [SPARK-7159][ML] Add multiclass logistic regressi...
Date Wed, 17 Aug 2016 05:34:25 GMT
Github user dbtsai commented on a diff in the pull request:

    https://github.com/apache/spark/pull/13796#discussion_r75064596
  
    --- 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 --
    
    Why are you not using `foreachActive`? Although we know that `coeffs` is dense array,
but if we implement strong rule which can know which column of `coeffs` will be zeros before
the optimization, we may store it as sparse vector. As a result, using `foreachActive` will
be a good abstraction. 


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Mime
View raw message