-
Notifications
You must be signed in to change notification settings - Fork 29k
[WIP][SPARK-1486][MLlib] Multi Model Training with Gradient Descent #2451
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
5138d3f
4362ff1
8dcb763
41b2da3
56d7c85
848406c
eeb13eb
a85ccb7
d510c8f
418def8
f79db9c
d162684
272feb9
5e7d744
2ea711c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,9 +17,11 @@ | |
|
|
||
| package org.apache.spark.mllib.optimization | ||
|
|
||
| import breeze.linalg.{DenseMatrix => BDM, DenseVector => BDV} | ||
|
|
||
| import org.apache.spark.annotation.DeveloperApi | ||
| import org.apache.spark.mllib.linalg.{Vector, Vectors} | ||
| import org.apache.spark.mllib.linalg.BLAS.{axpy, dot, scal} | ||
| import org.apache.spark.mllib.linalg._ | ||
| import org.apache.spark.mllib.linalg.BLAS.{axpy, dot, gemm, scal} | ||
|
|
||
| /** | ||
| * :: DeveloperApi :: | ||
|
|
@@ -157,3 +159,250 @@ 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) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should fit on 1 line. Also check methods below |
||
|
|
||
| /** | ||
| * 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, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can this be implemented using the below compute method to avoid code duplication? |
||
| weights: DenseMatrix): (DenseMatrix, Vector) = { | ||
| 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): Vector = { | ||
| 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, _ == _) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this really worthwhile? Computation is still linear in the size of the data, and the computation for colSums is pretty light.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This applies elsewhere too, but I won't repeat the comment. |
||
| 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 { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. At some point, we should rename this to SquaredError (not LeastSquares). |
||
| override def compute(data: Matrix, label: DenseMatrix, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. line formatting
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto about computing in terms of below compute() method. |
||
| weights: DenseMatrix): (DenseMatrix, Vector) = { | ||
|
|
||
| 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): Vector = { | ||
| 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, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto about implementing this in terms of the below compute() method. |
||
| weights: DenseMatrix): (DenseMatrix, Vector) = { | ||
|
|
||
| val dotProduct = (data transposeMultiply weights).toBreeze | ||
| val brzData = data.toBreeze | ||
| // Our loss function with {0, 1} labels is max(0, 1 - (2y – 1) (f_w(x))) | ||
| // Therefore the gradient is -(2y - 1)*x | ||
| val brzScaledLabels = new BDM[Double](1, label.numRows, label.values.map(_ * 2 - 1.0)) | ||
|
|
||
| dotProduct *= brzScaledLabels | ||
| brzScaledLabels. | ||
| val gradientMultiplier = data.elementWiseOperateOnRows(_ * _, labelScaled.negInPlace) | ||
| val gradient = DenseMatrix.zeros(weights.numRows, weights.numCols) | ||
| val activeExamples = dotProduct.compare(1.0, _ < _) // Examples where the hinge is active | ||
|
|
||
| gemm(false, false, 1.0, gradientMultiplier, activeExamples, 1.0, gradient) | ||
|
|
||
| val loss = activeExamples.elementWiseOperateInPlace(_ * _, dotProduct.update(1 - _)) | ||
|
|
||
| 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): Vector = { | ||
|
|
||
| val dotProduct = data transposeMultiply weights | ||
| // Our loss function with {0, 1} labels is max(0, 1 - (2y – 1) (f_w(x))) | ||
| // Therefore the gradient is -(2y - 1)*x | ||
| val labelScaled = new DenseMatrix(1, label.numRows, label.map(_ * 2 - 1.0).values) | ||
| dotProduct.elementWiseOperateOnColumnsInPlace(_ * _, labelScaled) | ||
|
|
||
| val gradientMultiplier = data.elementWiseOperateOnRows(_ * _, labelScaled.negInPlace) | ||
|
|
||
| val activeExamples = dotProduct.compare(1.0, _ < _) // Examples where the hinge is active | ||
|
|
||
| gemm(false, false, 1.0, gradientMultiplier, activeExamples, 1.0, cumGradient) | ||
|
|
||
| val loss = activeExamples.elementWiseOperateInPlace(_ * _, dotProduct.update(1 - _)) | ||
|
|
||
| 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 | ||
| } | ||
| } | ||
| } | ||
| /* | ||
| override def compute(data: Matrix, label: DenseMatrix, | ||
| weights: DenseMatrix, cumGradient: DenseMatrix): Vector = { | ||
|
|
||
| val dotProduct = data transposeMultiply weights | ||
| // Our loss function with {0, 1} labels is max(0, 1 - (2y – 1) (f_w(x))) | ||
| // Therefore the gradient is -(2y - 1)*x | ||
| val labelScaled = new DenseMatrix(1, label.numRows, label.map(_ * 2 - 1.0).values) | ||
| dotProduct.elementWiseOperateOnColumnsInPlace(_ * _, labelScaled) | ||
|
|
||
| val gradientMultiplier = data.elementWiseOperateOnRows(_ * _, labelScaled.negInPlace) | ||
|
|
||
| val activeExamples = dotProduct.compare(1.0, _ < _) // Examples where the hinge is active | ||
|
|
||
| gemm(false, false, 1.0, gradientMultiplier, activeExamples, 1.0, cumGradient) | ||
|
|
||
| val loss = activeExamples.elementWiseOperateInPlace(_ * _, dotProduct.update(1 - _)) | ||
|
|
||
| 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 | ||
| } | ||
| } | ||
| */ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,7 +33,7 @@ import org.apache.spark.mllib.rdd.RDDFunctions._ | |
| * @param updater Updater to be used to update weights after every iteration. | ||
| */ | ||
| class GradientDescent private[mllib] (private var gradient: Gradient, private var updater: Updater) | ||
| extends Optimizer with Logging { | ||
| extends Optimizer[Vector] with Logging { | ||
|
|
||
| private var stepSize: Double = 1.0 | ||
| private var numIterations: Int = 100 | ||
|
|
@@ -181,6 +181,7 @@ object GradientDescent extends Logging { | |
| var regVal = updater.compute( | ||
| weights, Vectors.dense(new Array[Double](weights.size)), 0, 1, regParam)._2 | ||
|
|
||
| //println(s"initial:\n$weights\n\n") | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove |
||
| for (i <- 1 to numIterations) { | ||
| val bcWeights = data.context.broadcast(weights) | ||
| // Sample a subset (fraction miniBatchFraction) of the total data | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
newline?