spark-reviews mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jkbradley <...@git.apache.org>
Subject [GitHub] spark pull request: [WIP][SPARK-1486][MLlib] Multi Model Training ...
Date Fri, 19 Sep 2014 20:42:22 GMT
Github user jkbradley commented on a diff in the pull request:

    https://github.com/apache/spark/pull/2451#discussion_r17808151
  
    --- 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, _ == _)
    --- End diff --
    
    This applies elsewhere too, but I won't repeat the comment.


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