Github user dbtsai commented on a diff in the pull request: https://github.com/apache/spark/pull/353#discussion_r11464121 --- 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.") --- End diff -- I add the comment in the code as // GD converges a way slower than L-BFGS. To achieve 1% difference, // it requires 90 iterations in GD. No matter how hard we increase // the number of iterations in GD here, the lossGD will be always // larger than lossLBFGS.
--- 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. ---