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

    https://github.com/apache/spark/pull/353#discussion_r11464736
  
    --- Diff: 
mllib/src/test/scala/org/apache/spark/mllib/optimization/LBFGSSuite.scala ---
    @@ -0,0 +1,217 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one or more
    + * contributor license agreements.  See the NOTICE file distributed with
    + * this work for additional information regarding copyright ownership.
    + * The ASF licenses this file to You under the Apache License, Version 2.0
    + * (the "License"); you may not use this file except in compliance with
    + * the License.  You may obtain a copy of the License at
    + *
    + *    http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.spark.mllib.optimization
    +
    +import org.scalatest.BeforeAndAfterAll
    +import org.scalatest.FunSuite
    +import org.scalatest.matchers.ShouldMatchers
    +
    +import org.apache.spark.SparkContext
    +import org.apache.spark.mllib.regression.LabeledPoint
    +import org.apache.spark.rdd.RDD
    +import org.apache.spark.mllib.linalg.{Vectors, Vector}
    +
    +class LBFGSSuite extends FunSuite with BeforeAndAfterAll with 
ShouldMatchers {
    +  @transient private var sc: SparkContext = _
    +  var dataRDD:RDD[(Double, Vector)] = _
    +
    +  val nPoints = 10000
    +  val A = 2.0
    +  val B = -1.5
    +
    +  val initialB = -1.0
    +  val initialWeights = Array(initialB)
    +
    +  val gradient = new LogisticGradient()
    +  val numCorrections = 10
    +  val lineSearchTolerance = 0.9
    +  var convTolerance = 1e-12
    +  var maxNumIterations = 10
    +  val miniBatchFrac = 1.0
    +
    +  val simpleUpdater = new SimpleUpdater()
    +  val squaredL2Updater = new SquaredL2Updater()
    +
    +  // Add a extra variable consisting of all 1.0's for the intercept.
    +  val testData = GradientDescentSuite.generateGDInput(A, B, nPoints, 42)
    +  val data = testData.map { case LabeledPoint(label, features) =>
    +    label -> Vectors.dense(1.0, features.toArray: _*)
    +  }
    +
    +  override def beforeAll() {
    +    sc = new SparkContext("local", "test")
    +    dataRDD = sc.parallelize(data, 2).cache()
    +  }
    +
    +  override def afterAll() {
    +    sc.stop()
    +    System.clearProperty("spark.driver.port")
    +  }
    +
    +  def compareDouble(x: Double, y: Double, tol: Double = 1E-3): Boolean = {
    +    math.abs(x - y) / (math.abs(y) + 1e-15) < tol
    +  }
    +
    +  test("Assert LBFGS loss is decreasing and matches the result of Gradient 
Descent.") {
    +    val updater = new SimpleUpdater()
    +    val regParam = 0
    +
    +    val initialWeightsWithIntercept = Vectors.dense(1.0, initialWeights: 
_*)
    +
    +    val (_, loss) = LBFGS.runMiniBatchLBFGS(
    +      dataRDD,
    +      gradient,
    +      updater,
    +      numCorrections,
    +      lineSearchTolerance,
    +      convTolerance,
    +      maxNumIterations,
    +      regParam,
    +      miniBatchFrac,
    +      initialWeightsWithIntercept)
    +
    +    assert(loss.last - loss.head < 0, "loss isn't decreasing.")
    +
    +    val lossDiff = loss.init.zip(loss.tail).map {
    +      case (lhs, rhs) => lhs - rhs
    +    }
    +    assert(lossDiff.count(_ > 0).toDouble / lossDiff.size > 0.8)
    +
    +    val stepSize = 1.0
    +    // Well, GD converges slower, so it requires more iterations!
    +    val numGDIterations = 50
    +    val (_, lossGD) = GradientDescent.runMiniBatchSGD(
    +      dataRDD,
    +      gradient,
    +      updater,
    +      stepSize,
    +      numGDIterations,
    +      regParam,
    +      miniBatchFrac,
    +      initialWeightsWithIntercept)
    +
    +    assert(Math.abs((lossGD.last - loss.last) / loss.last) < 0.05,
    +      "LBFGS should match GD result within 5% error.")
    +  }
    +
    +  test("Assert that LBFGS and Gradient Descent with L2 regularization get 
the same result.") {
    +    val regParam = 0.2
    +
    +    // Prepare another non-zero weights to compare the loss in the first 
iteration.
    +    val initialWeightsWithIntercept = Vectors.dense(0.3, 0.12)
    +
    +    val (weightLBFGS, lossLBFGS) = LBFGS.runMiniBatchLBFGS(
    +      dataRDD,
    +      gradient,
    +      squaredL2Updater,
    +      numCorrections,
    +      lineSearchTolerance,
    +      convTolerance,
    +      maxNumIterations,
    +      regParam,
    +      miniBatchFrac,
    +      initialWeightsWithIntercept)
    +
    +    // With regularization, GD converges faster now!
    +    // So we only need 20 iterations to get the same result.
    +    val numGDIterations = 20
    +    val stepSize = 1.0
    +    val (weightGD, lossGD) = GradientDescent.runMiniBatchSGD(
    +      dataRDD,
    +      gradient,
    +      squaredL2Updater,
    +      stepSize,
    +      numGDIterations,
    +      regParam,
    +      miniBatchFrac,
    +      initialWeightsWithIntercept)
    +
    +    assert(compareDouble(lossGD(0), lossLBFGS(0)),
    +      "The first losses of LBFGS and GD should be the same.")
    +
    +    assert(compareDouble(lossGD.last, lossLBFGS.last, 0.05),
    +      "The last losses of LBFGS and GD should be within 5% difference.")
    +
    +    assert(
    +      compareDouble(weightLBFGS(0), weightGD(0), 0.05) &&
    +        compareDouble(weightLBFGS(1), weightGD(1), 0.05),
    +      "The weight differences between LBFGS and GD should be within 5% 
difference.")
    +  }
    +
    +  test("Test if the convergence criteria works as we expect.") {
    +    val regParam = 0.0
    +
    +    /**
    +     * For the first run, we set the convTolerance to 0.0, so that the 
algorithm will
    +     * run up to the maxNumIterations which is 8 here.
    +     */
    +    val initialWeightsWithIntercept = Vectors.dense(0.0, 0.0)
    +    maxNumIterations = 8
    +    convTolerance = 0
    +
    +    val (_, lossLBFGS1) = LBFGS.runMiniBatchLBFGS(
    +      dataRDD,
    +      gradient,
    +      squaredL2Updater,
    +      numCorrections,
    +      lineSearchTolerance,
    +      convTolerance,
    +      maxNumIterations,
    +      regParam,
    +      miniBatchFrac,
    +      initialWeightsWithIntercept)
    +
    +    // Note that the first loss is computed with initial weights,
    +    // so the total numbers of loss will be numbers of iterations + 1
    +    assert(lossLBFGS1.length == 9)
    +
    +    convTolerance = 0.1
    +    val (_, lossLBFGS2) = LBFGS.runMiniBatchLBFGS(
    +      dataRDD,
    +      gradient,
    +      squaredL2Updater,
    +      numCorrections,
    +      lineSearchTolerance,
    +      convTolerance,
    +      maxNumIterations,
    +      regParam,
    +      miniBatchFrac,
    +      initialWeightsWithIntercept)
    +
    +    assert(lossLBFGS2.length == 4)
    +    assert((lossLBFGS2(2) - lossLBFGS2(3)) / lossLBFGS2(2) < convTolerance)
    +
    +    // With smaller convTolerance, it takes more steps.
    +    convTolerance = 0.01
    +    val (_, lossLBFGS3) = LBFGS.runMiniBatchLBFGS(
    +      dataRDD,
    +      gradient,
    +      squaredL2Updater,
    +      numCorrections,
    +      lineSearchTolerance,
    +      convTolerance,
    +      maxNumIterations,
    +      regParam,
    +      miniBatchFrac,
    +      initialWeightsWithIntercept)
    +
    +    assert(lossLBFGS3.length == 6)
    +    assert((lossLBFGS3(4) - lossLBFGS3(5)) / lossLBFGS3(4) < convTolerance)
    +  }
    +}
    +
    --- End diff --
    
    Without this extra empty line, the jenkins will complain.


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

Reply via email to