aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDongjoon Hyun <dongjoon@apache.org>2016-03-31 09:39:15 -0700
committerXiangrui Meng <meng@databricks.com>2016-03-31 09:39:15 -0700
commit208fff3ac87f200fd4e6f0407d70bf81cf8c556f (patch)
treee67c4e7e77cdab6bac2311b34b0edd36aed66378
parenta9b93e07391faede77dde4c0b3c21c9b3f97f8eb (diff)
downloadspark-208fff3ac87f200fd4e6f0407d70bf81cf8c556f.tar.gz
spark-208fff3ac87f200fd4e6f0407d70bf81cf8c556f.tar.bz2
spark-208fff3ac87f200fd4e6f0407d70bf81cf8c556f.zip
[SPARK-14164][MLLIB] Improve input layer validation of MultilayerPerceptronClassifier
## What changes were proposed in this pull request? This issue improves an input layer validation and adds related testcases to MultilayerPerceptronClassifier. ```scala - // TODO: how to check ALSO that all elements are greater than 0? - ParamValidators.arrayLengthGt(1) + (t: Array[Int]) => t.forall(ParamValidators.gt(0)) && t.length > 1 ``` ## How was this patch tested? Pass the Jenkins tests including the new testcases. Author: Dongjoon Hyun <dongjoon@apache.org> Closes #11964 from dongjoon-hyun/SPARK-14164.
-rw-r--r--mllib/src/main/scala/org/apache/spark/ml/classification/MultilayerPerceptronClassifier.scala3
-rw-r--r--mllib/src/test/scala/org/apache/spark/ml/classification/MultilayerPerceptronClassifierSuite.scala17
2 files changed, 18 insertions, 2 deletions
diff --git a/mllib/src/main/scala/org/apache/spark/ml/classification/MultilayerPerceptronClassifier.scala b/mllib/src/main/scala/org/apache/spark/ml/classification/MultilayerPerceptronClassifier.scala
index f6de5f2df4..7ce3ec68da 100644
--- a/mllib/src/main/scala/org/apache/spark/ml/classification/MultilayerPerceptronClassifier.scala
+++ b/mllib/src/main/scala/org/apache/spark/ml/classification/MultilayerPerceptronClassifier.scala
@@ -43,8 +43,7 @@ private[ml] trait MultilayerPerceptronParams extends PredictorParams
"Sizes of layers from input layer to output layer" +
" E.g., Array(780, 100, 10) means 780 inputs, " +
"one hidden layer with 100 neurons and output layer of 10 neurons.",
- // TODO: how to check ALSO that all elements are greater than 0?
- ParamValidators.arrayLengthGt(1)
+ (t: Array[Int]) => t.forall(ParamValidators.gt(0)) && t.length > 1
)
/** @group getParam */
diff --git a/mllib/src/test/scala/org/apache/spark/ml/classification/MultilayerPerceptronClassifierSuite.scala b/mllib/src/test/scala/org/apache/spark/ml/classification/MultilayerPerceptronClassifierSuite.scala
index 5df8e6a847..53c7a559e3 100644
--- a/mllib/src/test/scala/org/apache/spark/ml/classification/MultilayerPerceptronClassifierSuite.scala
+++ b/mllib/src/test/scala/org/apache/spark/ml/classification/MultilayerPerceptronClassifierSuite.scala
@@ -43,6 +43,23 @@ class MultilayerPerceptronClassifierSuite
).toDF("features", "label")
}
+ test("Input Validation") {
+ val mlpc = new MultilayerPerceptronClassifier()
+ intercept[IllegalArgumentException] {
+ mlpc.setLayers(Array[Int]())
+ }
+ intercept[IllegalArgumentException] {
+ mlpc.setLayers(Array[Int](1))
+ }
+ intercept[IllegalArgumentException] {
+ mlpc.setLayers(Array[Int](0, 1))
+ }
+ intercept[IllegalArgumentException] {
+ mlpc.setLayers(Array[Int](1, 0))
+ }
+ mlpc.setLayers(Array[Int](1, 1))
+ }
+
test("XOR function learning as binary classification problem with two outputs.") {
val layers = Array[Int](2, 5, 2)
val trainer = new MultilayerPerceptronClassifier()