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

    https://github.com/apache/spark/pull/2451#discussion_r17808193
  
    --- Diff: 
mllib/src/main/scala/org/apache/spark/mllib/optimization/Gradient.scala ---
    @@ -157,3 +157,221 @@ class HingeGradient extends Gradient {
         }
       }
     }
    +
    +/**
    + * :: DeveloperApi ::
    + * Class used to compute the gradient for a loss function, given a series 
of data points.
    + */
    +@DeveloperApi
    +abstract class MultiModelGradient extends Serializable {
    +  /**
    +   * Compute the gradient and loss given the features of all data points.
    +   *
    +   * @param data features for one data point
    +   * @param label label for this data point
    +   * @param weights weights/coefficients corresponding to features
    +   *
    +   * @return (gradient: DenseMatrix, loss: Double)
    +   */
    +  def compute(data: Matrix, label: DenseMatrix,
    +                       weights: DenseMatrix): (DenseMatrix, Matrix)
    +
    +  /**
    +   * Compute the gradient and loss given the features of a series of data 
point,
    +   * add the gradient to a provided matrix to avoid creating new objects, 
and return loss.
    +   *
    +   * @param data features for the data points
    +   * @param label label for the data points
    +   * @param weights weights/coefficients corresponding to features
    +   * @param cumGradient the computed gradient will be added to this matrix
    +   *
    +   * @return loss
    +   */
    +  def compute(data: Matrix, label: DenseMatrix,
    +                       weights: DenseMatrix, cumGradient: DenseMatrix): 
Matrix
    +}
    +
    +/**
    + * :: DeveloperApi ::
    + * Compute gradient and loss for a logistic loss function, as used in 
binary classification.
    + * See also the documentation for the precise formulation.
    + */
    +@DeveloperApi
    +class MultiModelLogisticGradient extends MultiModelGradient {
    +
    +  private def sigmoid(p: DenseMatrix): DenseMatrix = {
    +    def takeSigmoid(p: Double): Double = {
    +      1.0 / (math.exp(-p) + 1.0)
    +    }
    +    p.map(takeSigmoid)
    +  }
    +
    +  override def compute(data: Matrix, label: DenseMatrix,
    +                       weights: DenseMatrix): (DenseMatrix, Matrix) = {
    +    val margin = data transposeMultiply weights
    +    val gradient = DenseMatrix.zeros(weights.numRows, weights.numCols)
    +
    +    gemm(false, false, 1.0, data, 
sigmoid(margin).elementWiseOperateOnColumnsInPlace(_ - _, label),
    +      0.0, gradient)
    +
    +    val negativeLabels = label.compare(0.0, _ == _)
    +    val addMargin = margin.elementWiseOperateOnColumns(_ * _, 
negativeLabels)
    +
    +    val loss = margin.update(v => math.log1p(math.exp(-v))).
    +      elementWiseOperateInPlace(_ + _, addMargin)
    +
    +    val lossVector =
    +      if (data.isInstanceOf[DenseMatrix]) {
    +        val numFeatures = data.numRows
    +        val zeroEntries = data.compare(0.0, _ == _)
    +        val shouldSkip = zeroEntries.colSums.compareInPlace(numFeatures, _ 
== _)
    +        loss.colSums(false, shouldSkip)
    +      } else {
    +        loss.colSums
    +      }
    +    (gradient, lossVector)
    +  }
    +
    +  override def compute(data: Matrix,
    +                       label: DenseMatrix,
    +                       weights: DenseMatrix,
    +                       cumGradient: DenseMatrix): Matrix = {
    +    val margin = data transposeMultiply weights
    +    gemm(false, false, 1.0, data, 
sigmoid(margin).elementWiseOperateOnColumnsInPlace(_ - _, label),
    +      1.0, cumGradient)
    +
    +    val negativeLabels = label.compare(0.0, _ == _)
    +    val addMargin = margin.elementWiseOperateOnColumns(_ * _, 
negativeLabels)
    +
    +    val loss = margin.update(v => math.log1p(math.exp(-v))).
    +      elementWiseOperateInPlace(_ + _, addMargin)
    +
    +    if (data.isInstanceOf[DenseMatrix]) {
    +      val numFeatures = data.numRows
    +      val zeroEntries = data.compare(0.0, _ == _)
    +      val shouldSkip = zeroEntries.colSums.compareInPlace(numFeatures, _ 
== _)
    +      loss.colSums(false, shouldSkip)
    +    } else {
    +      loss.colSums
    +    }
    +  }
    +}
    +
    +/**
    + * :: DeveloperApi ::
    + * Compute gradient and loss for a Least-squared loss function, as used in 
linear regression.
    + * This is correct for the averaged least squares loss function (mean 
squared error)
    + *              L = 1/n ||A weights-y||^2
    + * See also the documentation for the precise formulation.
    + */
    +@DeveloperApi
    +class MultiModelLeastSquaresGradient extends MultiModelGradient {
    +  override def compute(data: Matrix, label: DenseMatrix,
    +                       weights: DenseMatrix): (DenseMatrix, Matrix) = {
    +
    +    val diff = (data transposeMultiply 
weights).elementWiseOperateOnColumnsInPlace(_ - _, label)
    +
    +    val gradient = DenseMatrix.zeros(weights.numRows, weights.numCols)
    +
    +    gemm(false, false, 2.0, data, diff, 0.0, gradient)
    +
    +    val loss = diff.update(v => v * v)
    +
    +    val lossVector =
    +      if (data.isInstanceOf[DenseMatrix]) {
    +        val numFeatures = data.numRows
    +        val zeroEntries = data.compare(0.0, _ == _)
    +        val shouldSkip = zeroEntries.colSums.compareInPlace(numFeatures, _ 
== _)
    +        loss.colSums(false, shouldSkip)
    +      } else {
    +        loss.colSums
    +      }
    +    (gradient, lossVector)
    +  }
    +
    +  override def compute(data: Matrix,
    +                       label: DenseMatrix,
    +                       weights: DenseMatrix,
    +                       cumGradient: DenseMatrix): Matrix = {
    +    val diff = (data transposeMultiply 
weights).elementWiseOperateOnColumnsInPlace(_ - _, label)
    +
    +    gemm(false, false, 2.0, data, diff, 1.0, cumGradient)
    +    val loss = diff.update(v => v * v)
    +
    +    if (data.isInstanceOf[DenseMatrix]) {
    +      val numFeatures = data.numRows
    +      val zeroEntries = data.compare(0.0, _ == _)
    +      val shouldSkip = zeroEntries.colSums.compareInPlace(numFeatures, _ 
== _)
    +      loss.colSums(false, shouldSkip)
    +    } else {
    +      loss.colSums
    +    }
    +  }
    +}
    +
    +
    +/**
    + * :: DeveloperApi ::
    + * Compute gradient and loss for a Hinge loss function, as used in SVM 
binary classification.
    + * See also the documentation for the precise formulation.
    + * NOTE: This assumes that the labels are {0,1}
    + */
    +@DeveloperApi
    +class MultiModelHingeGradient extends MultiModelGradient {
    +  override def compute(data: Matrix, label: DenseMatrix,
    --- End diff --
    
    Ditto about implementing this in terms of the below compute() method.


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

Reply via email to