Skip to content

SPARK-1793 - Heavily duplicated test setup code in SVMSuite #726

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import org.apache.spark.SparkException
import org.apache.spark.mllib.regression._
import org.apache.spark.mllib.util.LocalSparkContext
import org.apache.spark.mllib.linalg.Vectors
import org.apache.spark.rdd.RDD

object SVMSuite {

Expand Down Expand Up @@ -56,20 +57,11 @@ object SVMSuite {
}
y.zip(x).map(p => LabeledPoint(p._1, Vectors.dense(p._2)))
}

}

class SVMSuite extends FunSuite with LocalSparkContext {

def validatePrediction(predictions: Seq[Double], input: Seq[LabeledPoint]) {
val numOffPredictions = predictions.zip(input).count { case (prediction, expected) =>
prediction != expected.label
}
// At least 80% of the predictions should be on.
assert(numOffPredictions < input.length / 5)
}

test("SVM with threshold") {
def generateRDDs(): (RDD[LabeledPoint], RDD[LabeledPoint]) = {
val nPoints = 10000

// NOTE: Intercept should be small for generating equal 0s and 1s
Expand All @@ -78,20 +70,32 @@ class SVMSuite extends FunSuite with LocalSparkContext {
val C = 1.0

val testData = SVMSuite.generateSVMInput(A, Array[Double](B, C), nPoints, 42)

val testRDD = sc.parallelize(testData, 2)
testRDD.cache()

val validationData = SVMSuite.generateSVMInput(A, Array[Double](B, C), nPoints, 17)
val validationRDD = sc.parallelize(validationData, 2)

(testRDD, validationRDD)
}

def validatePrediction(predictions: Seq[Double], input: Seq[LabeledPoint]) {
val numOffPredictions = predictions.zip(input).count { case (prediction, expected) =>
prediction != expected.label
}
// At least 80% of the predictions should be on.
assert(numOffPredictions < input.length / 5)
}

test("SVM with threshold") {
val (testRDD, validationRDD) = generateRDDs()

val svm = new SVMWithSGD().setIntercept(true)
svm.optimizer.setStepSize(1.0).setRegParam(1.0).setNumIterations(100)

val model = svm.run(testRDD)

val validationData = SVMSuite.generateSVMInput(A, Array[Double](B, C), nPoints, 17)
val validationRDD = sc.parallelize(validationData, 2)

// Test prediction on RDD.

var predictions = model.predict(validationRDD.map(_.features)).collect()
assert(predictions.count(_ == 0.0) != predictions.length)

Expand All @@ -106,76 +110,52 @@ class SVMSuite extends FunSuite with LocalSparkContext {
assert(predictions.count(_ == 1.0) == predictions.length)
}

test("SVM using local random SGD") {
val nPoints = 10000

// NOTE: Intercept should be small for generating equal 0s and 1s
val A = 0.01
val B = -1.5
val C = 1.0

val testData = SVMSuite.generateSVMInput(A, Array[Double](B,C), nPoints, 42)

val testRDD = sc.parallelize(testData, 2)
testRDD.cache()
test("SVM using local random SGD") {
val (testRDD, validationRDD) = generateRDDs()

val svm = new SVMWithSGD().setIntercept(true)
svm.optimizer.setStepSize(1.0).setRegParam(1.0).setNumIterations(100)

val model = svm.run(testRDD)

val validationData = SVMSuite.generateSVMInput(A, Array[Double](B,C), nPoints, 17)
val validationRDD = sc.parallelize(validationData, 2)

// Test prediction on RDD.
validatePrediction(model.predict(validationRDD.map(_.features)).collect(), validationData)
validatePrediction(
model.predict(validationRDD.map(_.features)).collect(),
validationRDD.collect())

// Test prediction on Array.
validatePrediction(validationData.map(row => model.predict(row.features)), validationData)
validatePrediction(
validationRDD.collect().map(row => model.predict(row.features)),
validationRDD.collect())
}

test("SVM local random SGD with initial weights") {
val nPoints = 10000

// NOTE: Intercept should be small for generating equal 0s and 1s
val A = 0.01
val B = -1.5
val C = 1.0

val testData = SVMSuite.generateSVMInput(A, Array[Double](B,C), nPoints, 42)
val (testRDD, validationRDD) = generateRDDs()

val initialB = -1.0
val initialC = -1.0
val initialWeights = Vectors.dense(initialB, initialC)

val testRDD = sc.parallelize(testData, 2)
testRDD.cache()

val svm = new SVMWithSGD().setIntercept(true)
svm.optimizer.setStepSize(1.0).setRegParam(1.0).setNumIterations(100)

val model = svm.run(testRDD, initialWeights)

val validationData = SVMSuite.generateSVMInput(A, Array[Double](B,C), nPoints, 17)
val validationRDD = sc.parallelize(validationData,2)

// Test prediction on RDD.
validatePrediction(model.predict(validationRDD.map(_.features)).collect(), validationData)
validatePrediction(
model.predict(validationRDD.map(_.features)).collect(),
validationRDD.collect())

// Test prediction on Array.
validatePrediction(validationData.map(row => model.predict(row.features)), validationData)
validatePrediction(
validationRDD.collect().map(row => model.predict(row.features)),
validationRDD.collect())
}

test("SVM with invalid labels") {
val nPoints = 10000

// NOTE: Intercept should be small for generating equal 0s and 1s
val A = 0.01
val B = -1.5
val C = 1.0

val testData = SVMSuite.generateSVMInput(A, Array[Double](B,C), nPoints, 42)
val testRDD = sc.parallelize(testData, 2)
val (testRDD, _) = generateRDDs()

val testRDDInvalid = testRDD.map { lp =>
if (lp.label == 0.0) {
Expand Down