Skip to content

Commit 7d5fa17

Browse files
committed
Merge pull request apache#337 from yinxusen/mllib-16-bugfix
Mllib 16 bugfix Bug fix: https://spark-project.atlassian.net/browse/MLLIB-16 Hi, I fixed the bug and added a test suite for `GradientDescent`. There are 2 checks in the test case. First, the final loss must be lower than the initial one. Second, the trend of loss sequence should be decreasing, i.e., at least 80% iterations have lower losses than their prior iterations. Thanks!
2 parents 71fc113 + 05e6d5b commit 7d5fa17

File tree

2 files changed

+118
-2
lines changed

2 files changed

+118
-2
lines changed

mllib/src/main/scala/org/apache/spark/mllib/optimization/Gradient.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ class LogisticGradient extends Gradient {
5050

5151
val gradient = data.mul(gradientMultiplier)
5252
val loss =
53-
if (margin > 0) {
54-
math.log(1 + math.exp(0 - margin))
53+
if (label > 0) {
54+
math.log(1 + math.exp(margin))
5555
} else {
5656
math.log(1 + math.exp(margin)) - margin
5757
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.spark.mllib.optimization
19+
20+
import scala.util.Random
21+
import scala.collection.JavaConversions._
22+
23+
import org.scalatest.BeforeAndAfterAll
24+
import org.scalatest.FunSuite
25+
import org.scalatest.matchers.ShouldMatchers
26+
27+
import org.apache.spark.SparkContext
28+
import org.apache.spark.mllib.regression._
29+
30+
object GradientDescentSuite {
31+
32+
def generateLogisticInputAsList(
33+
offset: Double,
34+
scale: Double,
35+
nPoints: Int,
36+
seed: Int): java.util.List[LabeledPoint] = {
37+
seqAsJavaList(generateGDInput(offset, scale, nPoints, seed))
38+
}
39+
40+
// Generate input of the form Y = logistic(offset + scale * X)
41+
def generateGDInput(
42+
offset: Double,
43+
scale: Double,
44+
nPoints: Int,
45+
seed: Int): Seq[LabeledPoint] = {
46+
val rnd = new Random(seed)
47+
val x1 = Array.fill[Double](nPoints)(rnd.nextGaussian())
48+
49+
val unifRand = new scala.util.Random(45)
50+
val rLogis = (0 until nPoints).map { i =>
51+
val u = unifRand.nextDouble()
52+
math.log(u) - math.log(1.0-u)
53+
}
54+
55+
val y: Seq[Int] = (0 until nPoints).map { i =>
56+
val yVal = offset + scale * x1(i) + rLogis(i)
57+
if (yVal > 0) 1 else 0
58+
}
59+
60+
val testData = (0 until nPoints).map(i => LabeledPoint(y(i), Array(x1(i))))
61+
testData
62+
}
63+
}
64+
65+
class GradientDescentSuite extends FunSuite with BeforeAndAfterAll with ShouldMatchers {
66+
@transient private var sc: SparkContext = _
67+
68+
override def beforeAll() {
69+
sc = new SparkContext("local", "test")
70+
}
71+
72+
override def afterAll() {
73+
sc.stop()
74+
System.clearProperty("spark.driver.port")
75+
}
76+
77+
test("Assert the loss is decreasing.") {
78+
val nPoints = 10000
79+
val A = 2.0
80+
val B = -1.5
81+
82+
val initialB = -1.0
83+
val initialWeights = Array(initialB)
84+
85+
val gradient = new LogisticGradient()
86+
val updater = new SimpleUpdater()
87+
val stepSize = 1.0
88+
val numIterations = 10
89+
val regParam = 0
90+
val miniBatchFrac = 1.0
91+
92+
// Add a extra variable consisting of all 1.0's for the intercept.
93+
val testData = GradientDescentSuite.generateGDInput(A, B, nPoints, 42)
94+
val data = testData.map { case LabeledPoint(label, features) =>
95+
label -> Array(1.0, features: _*)
96+
}
97+
98+
val dataRDD = sc.parallelize(data, 2).cache()
99+
val initialWeightsWithIntercept = Array(1.0, initialWeights: _*)
100+
101+
val (_, loss) = GradientDescent.runMiniBatchSGD(
102+
dataRDD,
103+
gradient,
104+
updater,
105+
stepSize,
106+
numIterations,
107+
regParam,
108+
miniBatchFrac,
109+
initialWeightsWithIntercept)
110+
111+
assert(loss.last - loss.head < 0, "loss isn't decreasing.")
112+
113+
val lossDiff = loss.init.zip(loss.tail).map { case (lhs, rhs) => lhs - rhs }
114+
assert(lossDiff.count(_ > 0).toDouble / lossDiff.size > 0.8)
115+
}
116+
}

0 commit comments

Comments
 (0)