Github user mengxr commented on a diff in the pull request:
https://github.com/apache/spark/pull/40#discussion_r10178907
--- Diff:
mllib/src/test/scala/org/apache/spark/mllib/optimization/GradientDescentSuite.scala
---
@@ -104,4 +104,45 @@ class GradientDescentSuite extends FunSuite with
LocalSparkContext with ShouldMa
val lossDiff = loss.init.zip(loss.tail).map { case (lhs, rhs) => lhs -
rhs }
assert(lossDiff.count(_ > 0).toDouble / lossDiff.size > 0.8)
}
+
+ test("Test the loss and gradient of first iteration with
regularization.") {
+
+ val gradient = new LogisticGradient()
+ val updater = new SquaredL2Updater()
+
+ // Add a extra variable consisting of all 1.0's for the intercept.
+ val testData = GradientDescentSuite.generateGDInput(2.0, -1.5, 10000,
42)
+ val data = testData.map { case LabeledPoint(label, features) =>
+ label -> Array(1.0, features: _*)
+ }
+
+ val dataRDD = sc.parallelize(data, 2).cache()
+
+ // Prepare non-zero weights
+ val initialWeightsWithIntercept = Array(1.0, 0.5)
+
+ val regParam0 = 0
+ val (newWeights0, loss0) = GradientDescent.runMiniBatchSGD(
+ dataRDD, gradient, updater, 1, 1, regParam0, 1.0,
initialWeightsWithIntercept)
+
+ val regParam1 = 1
+ val (newWeights1, loss1) = GradientDescent.runMiniBatchSGD(
+ dataRDD, gradient, updater, 1, 1, regParam1, 1.0,
initialWeightsWithIntercept)
+
+ def compareDouble(x: Double, y: Double, tol: Double = 1E-3): Boolean =
{
+ math.abs(x - y) / (math.abs(y) + 1e-15) < tol
+ }
+
+ assert(compareDouble(
+ loss1(0),
+ loss0(0) + (math.pow(initialWeightsWithIntercept(0), 2) +
+ math.pow(initialWeightsWithIntercept(1), 2)) / 2),
+ """For non-zero weights, the regVal should be \frac{1}{2}\sum_i
w_i^2.""")
+
+ assert(
+ compareDouble(newWeights1(0) , newWeights0(0) -
initialWeightsWithIntercept(0)) &&
+ compareDouble(newWeights1(1) , newWeights0(1) -
initialWeightsWithIntercept(1)),
+ "The different between newWeights with/without regularization " +
+ "should be initialWeightsWithIntercept")
--- End diff --
Missing a period at the end.
---
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.
---