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

    https://github.com/apache/spark/pull/18305#discussion_r127874833
  
    --- Diff: 
mllib/src/main/scala/org/apache/spark/ml/optim/loss/DifferentiableRegularization.scala
 ---
    @@ -32,40 +34,45 @@ private[ml] trait DifferentiableRegularization[T] 
extends DiffFunction[T] {
     }
     
     /**
    - * A Breeze diff function for computing the L2 regularized loss and 
gradient of an array of
    + * A Breeze diff function for computing the L2 regularized loss and 
gradient of a vector of
      * coefficients.
      *
      * @param regParam The magnitude of the regularization.
      * @param shouldApply A function (Int => Boolean) indicating whether a 
given index should have
      *                    regularization applied to it.
    - * @param featuresStd Option indicating whether the regularization should 
be scaled by the standard
    - *                    deviation of the features.
    + * @param applyFeaturesStd Option for a function which maps coefficient 
index (column major) to the
    + *                         feature standard deviation. If `None`, no 
standardization is applied.
      */
     private[ml] class L2Regularization(
    -    val regParam: Double,
    +    override val regParam: Double,
         shouldApply: Int => Boolean,
    -    featuresStd: Option[Array[Double]]) extends 
DifferentiableRegularization[Array[Double]] {
    +    applyFeaturesStd: Option[Int => Double]) extends 
DifferentiableRegularization[Vector] {
     
    -  override def calculate(coefficients: Array[Double]): (Double, 
Array[Double]) = {
    -    var sum = 0.0
    -    val gradient = new Array[Double](coefficients.length)
    -    coefficients.indices.filter(shouldApply).foreach { j =>
    -      val coef = coefficients(j)
    -      featuresStd match {
    -        case Some(stds) =>
    -          val std = stds(j)
    -          if (std != 0.0) {
    -            val temp = coef / (std * std)
    -            sum += coef * temp
    -            gradient(j) = regParam * temp
    -          } else {
    -            0.0
    +  override def calculate(coefficients: Vector): (Double, Vector) = {
    +    coefficients match {
    +      case dv: DenseVector =>
    +        var sum = 0.0
    +        val gradient = new Array[Double](dv.size)
    +        dv.values.indices.filter(shouldApply).foreach { j =>
    +          val coef = coefficients(j)
    +          applyFeaturesStd match {
    +            case Some(getStd) =>
    +              val std = getStd(j)
    +              if (std != 0.0) {
    +                val temp = coef / (std * std)
    +                sum += coef * temp
    +                gradient(j) = regParam * temp
    +              } else {
    +                0.0
    +              }
    +            case None =>
    +              sum += coef * coef
    +              gradient(j) = coef * regParam
    --- End diff --
    
    Trivial, to match `regParam * temp` above, how about using `regParam * 
coef`?


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