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

    https://github.com/apache/spark/pull/18305#discussion_r124615145
  
    --- Diff: 
mllib/src/main/scala/org/apache/spark/ml/optim/loss/DifferentiableRegularization.scala
 ---
    @@ -38,34 +40,39 @@ private[ml] trait DifferentiableRegularization[T] 
extends DiffFunction[T] {
      * @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 featuresStd 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]] {
    +    featuresStd: 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)
    +          featuresStd 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
               }
    -        case None =>
    -          sum += coef * coef
    -          gradient(j) = coef * regParam
    -      }
    +        }
    +        (0.5 * sum * regParam, Vectors.dense(gradient))
    +      case _: SparseVector =>
    +        throw new IllegalArgumentException("SparseVector is not currently 
supported.")
    --- End diff --
    
    done


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