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

    https://github.com/apache/spark/pull/353#discussion_r11468948
  
    --- Diff: 
mllib/src/test/scala/org/apache/spark/mllib/optimization/LBFGSSuite.scala ---
    @@ -0,0 +1,209 @@
    +/*
    + * 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.mllib.regression.LabeledPoint
    +import org.apache.spark.mllib.linalg.Vectors
    +import org.apache.spark.mllib.util.LocalSparkContext
    +
    +class LBFGSSuite extends FunSuite with BeforeAndAfterAll with 
LocalSparkContext with ShouldMatchers {
    +
    +  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
    +  var convergenceTol = 1e-12
    +  var maxNumIterations = 10
    +  val miniBatchFrac = 1.0
    +
    +  val simpleUpdater = new SimpleUpdater()
    +  val squaredL2Updater = new SquaredL2Updater()
    +
    +  // Add an 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: _*)
    +  }
    +
    +  lazy val dataRDD = sc.parallelize(data, 2).cache()
    +
    +  def compareDouble(x: Double, y: Double, tol: Double = 1E-3): Boolean = {
    +    math.abs(x - y) / (math.abs(y) + 1e-15) < tol
    +  }
    +
    +  test("LBFGS loss should be decreasing and match 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,
    +      convergenceTol,
    +      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
    +    }
    +    // This 0.8 bound is copying from GradientDescentSuite, and L-BFGS 
should
    +    // at least have the same performance. It's based on observation, no 
theoretically guaranteed.
    +    assert(lossDiff.count(_ > 0).toDouble / lossDiff.size > 0.8)
    --- End diff --
    
    @dbtsai This test still looks strange, because both gradient descent and 
L-BFGS are monotonic if the line search is correctly implemented. In our 
gradient descent implementation, step sizes are pre-defined, so it may not be 
monotonic if the initial step size is too large. However, L-BFGS uses line 
search, so maybe you should test the sequence of loss is strictly decreasing 
instead of 80 percent of them.


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