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