Skip to content
Closed
Show file tree
Hide file tree
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 @@ -126,10 +126,26 @@ private[ml] object ValidatorParams {
extraMetadata: Option[JObject] = None): Unit = {
import org.json4s.JsonDSL._

var numParamsNotJson = 0
val estimatorParamMapsJson = compact(render(
instance.getEstimatorParamMaps.map { case paramMap =>
paramMap.toSeq.map { case ParamPair(p, v) =>
Map("parent" -> p.parent, "name" -> p.name, "value" -> p.jsonEncode(v))
v match {
case writeableObj: DefaultParamsWritable =>
val relativePath = "epm_" + p.name + numParamsNotJson
val paramPath = new Path(path, relativePath).toString
numParamsNotJson += 1
writeableObj.save(paramPath)
Map("parent" -> p.parent, "name" -> p.name,
"value" -> compact(render(JString(relativePath))),
"isJson" -> compact(render(JBool(false))))
case _: MLWritable =>
throw new NotImplementedError("ValidatorParams.saveImpl does not handle parameters " +
"of type: MLWritable that are not DefaultParamsWritable")
case _ =>
Map("parent" -> p.parent, "name" -> p.name, "value" -> p.jsonEncode(v),
"isJson" -> compact(render(JBool(true))))
}
}
}.toSeq
))
Expand Down Expand Up @@ -183,8 +199,17 @@ private[ml] object ValidatorParams {
val paramPairs = pMap.map { case pInfo: Map[String, String] =>
val est = uidToParams(pInfo("parent"))
val param = est.getParam(pInfo("name"))
val value = param.jsonDecode(pInfo("value"))
param -> value
// [Spark-21221] introduced the isJson field
if (!pInfo.contains("isJson") ||
(pInfo.contains("isJson") && pInfo("isJson").toBoolean.booleanValue())) {
val value = param.jsonDecode(pInfo("value"))
param -> value
} else {
val relativePath = param.jsonDecode(pInfo("value")).toString
val value = DefaultParamsReader
.loadParamsInstance[MLWritable](new Path(path, relativePath).toString, sc)
param -> value
}
}
ParamMap(paramPairs: _*)
}.toArray
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ package org.apache.spark.ml.tuning

import org.apache.spark.SparkFunSuite
import org.apache.spark.ml.{Estimator, Model, Pipeline}
import org.apache.spark.ml.classification.{LogisticRegression, LogisticRegressionModel}
import org.apache.spark.ml.classification.{LogisticRegression, LogisticRegressionModel, OneVsRest}
import org.apache.spark.ml.classification.LogisticRegressionSuite.generateLogisticInput
import org.apache.spark.ml.evaluation.{BinaryClassificationEvaluator, Evaluator, RegressionEvaluator}
import org.apache.spark.ml.evaluation.{BinaryClassificationEvaluator, Evaluator, MulticlassClassificationEvaluator, RegressionEvaluator}
import org.apache.spark.ml.feature.HashingTF
import org.apache.spark.ml.linalg.{DenseMatrix, Vectors}
import org.apache.spark.ml.param.{ParamMap, ParamPair}
import org.apache.spark.ml.linalg.Vectors
import org.apache.spark.ml.param.ParamMap
import org.apache.spark.ml.param.shared.HasInputCol
import org.apache.spark.ml.regression.LinearRegression
import org.apache.spark.ml.util.{DefaultReadWriteTest, MLTestingUtils}
Expand Down Expand Up @@ -153,7 +153,76 @@ class CrossValidatorSuite
s" LogisticRegression but found ${other.getClass.getName}")
}

CrossValidatorSuite.compareParamMaps(cv.getEstimatorParamMaps, cv2.getEstimatorParamMaps)
ValidatorParamsSuiteHelpers
.compareParamMaps(cv.getEstimatorParamMaps, cv2.getEstimatorParamMaps)
}

test("read/write: CrossValidator with nested estimator") {
val ova = new OneVsRest().setClassifier(new LogisticRegression)
val evaluator = new MulticlassClassificationEvaluator()
.setMetricName("accuracy")
val classifier1 = new LogisticRegression().setRegParam(2.0)
val classifier2 = new LogisticRegression().setRegParam(3.0)
// params that are not JSON serializable must inherit from Params
val paramMaps = new ParamGridBuilder()
.addGrid(ova.classifier, Array(classifier1, classifier2))
.build()
val cv = new CrossValidator()
.setEstimator(ova)
.setEvaluator(evaluator)
.setNumFolds(20)
.setEstimatorParamMaps(paramMaps)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please compare the original + the loaded estimatorParamMaps

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same for the TrainValidationSplitSuite


val cv2 = testDefaultReadWrite(cv, testParams = false)

assert(cv.uid === cv2.uid)
assert(cv.getNumFolds === cv2.getNumFolds)
assert(cv.getSeed === cv2.getSeed)

assert(cv2.getEvaluator.isInstanceOf[MulticlassClassificationEvaluator])
val evaluator2 = cv2.getEvaluator.asInstanceOf[MulticlassClassificationEvaluator]
assert(evaluator.uid === evaluator2.uid)
assert(evaluator.getMetricName === evaluator2.getMetricName)

cv2.getEstimator match {
case ova2: OneVsRest =>
assert(ova.uid === ova2.uid)
val classifier = ova2.getClassifier
classifier match {
case lr: LogisticRegression =>
assert(ova.getClassifier.asInstanceOf[LogisticRegression].getMaxIter
=== lr.getMaxIter)
case _ =>
throw new AssertionError(s"Loaded CrossValidator expected estimator of type" +
s" LogisticREgression but found ${classifier.getClass.getName}")
}

case other =>
throw new AssertionError(s"Loaded CrossValidator expected estimator of type" +
s" OneVsRest but found ${other.getClass.getName}")
}

ValidatorParamsSuiteHelpers
.compareParamMaps(cv.getEstimatorParamMaps, cv2.getEstimatorParamMaps)
}

test("read/write: Persistence of nested estimator works if parent directory changes") {
val ova = new OneVsRest().setClassifier(new LogisticRegression)
val evaluator = new MulticlassClassificationEvaluator()
.setMetricName("accuracy")
val classifier1 = new LogisticRegression().setRegParam(2.0)
val classifier2 = new LogisticRegression().setRegParam(3.0)
// params that are not JSON serializable must inherit from Params
val paramMaps = new ParamGridBuilder()
.addGrid(ova.classifier, Array(classifier1, classifier2))
.build()
val cv = new CrossValidator()
.setEstimator(ova)
.setEvaluator(evaluator)
.setNumFolds(20)
.setEstimatorParamMaps(paramMaps)

ValidatorParamsSuiteHelpers.testFileMove(cv)
}

test("read/write: CrossValidator with complex estimator") {
Expand Down Expand Up @@ -193,7 +262,8 @@ class CrossValidatorSuite
assert(cv2.getEvaluator.isInstanceOf[BinaryClassificationEvaluator])
assert(cv.getEvaluator.uid === cv2.getEvaluator.uid)

CrossValidatorSuite.compareParamMaps(cv.getEstimatorParamMaps, cv2.getEstimatorParamMaps)
ValidatorParamsSuiteHelpers
.compareParamMaps(cv.getEstimatorParamMaps, cv2.getEstimatorParamMaps)

cv2.getEstimator match {
case pipeline2: Pipeline =>
Expand All @@ -212,7 +282,8 @@ class CrossValidatorSuite
assert(lrcv.uid === lrcv2.uid)
assert(lrcv2.getEvaluator.isInstanceOf[BinaryClassificationEvaluator])
assert(lrEvaluator.uid === lrcv2.getEvaluator.uid)
CrossValidatorSuite.compareParamMaps(lrParamMaps, lrcv2.getEstimatorParamMaps)
ValidatorParamsSuiteHelpers
.compareParamMaps(lrParamMaps, lrcv2.getEstimatorParamMaps)
case other =>
throw new AssertionError("Loaded Pipeline expected stages (HashingTF, CrossValidator)" +
" but found: " + other.map(_.getClass.getName).mkString(", "))
Expand Down Expand Up @@ -278,7 +349,8 @@ class CrossValidatorSuite
s" LogisticRegression but found ${other.getClass.getName}")
}

CrossValidatorSuite.compareParamMaps(cv.getEstimatorParamMaps, cv2.getEstimatorParamMaps)
ValidatorParamsSuiteHelpers
.compareParamMaps(cv.getEstimatorParamMaps, cv2.getEstimatorParamMaps)

cv2.bestModel match {
case lrModel2: LogisticRegressionModel =>
Expand All @@ -296,21 +368,6 @@ class CrossValidatorSuite

object CrossValidatorSuite extends SparkFunSuite {

/**
* Assert sequences of estimatorParamMaps are identical.
* Params must be simple types comparable with `===`.
*/
def compareParamMaps(pMaps: Array[ParamMap], pMaps2: Array[ParamMap]): Unit = {
assert(pMaps.length === pMaps2.length)
pMaps.zip(pMaps2).foreach { case (pMap, pMap2) =>
assert(pMap.size === pMap2.size)
pMap.toSeq.foreach { case ParamPair(p, v) =>
assert(pMap2.contains(p))
assert(pMap2(p) === v)
}
}
}

abstract class MyModel extends Model[MyModel]

class MyEstimator(override val uid: String) extends Estimator[MyModel] with HasInputCol {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ package org.apache.spark.ml.tuning

import org.apache.spark.SparkFunSuite
import org.apache.spark.ml.{Estimator, Model}
import org.apache.spark.ml.classification.{LogisticRegression, LogisticRegressionModel}
import org.apache.spark.ml.classification.{LogisticRegression, LogisticRegressionModel, OneVsRest}
import org.apache.spark.ml.classification.LogisticRegressionSuite.generateLogisticInput
import org.apache.spark.ml.evaluation.{BinaryClassificationEvaluator, Evaluator, RegressionEvaluator}
import org.apache.spark.ml.linalg.Vectors
import org.apache.spark.ml.param.ParamMap
import org.apache.spark.ml.param.{ParamMap}
import org.apache.spark.ml.param.shared.HasInputCol
import org.apache.spark.ml.regression.LinearRegression
import org.apache.spark.ml.util.{DefaultReadWriteTest, MLTestingUtils}
Expand Down Expand Up @@ -95,7 +95,7 @@ class TrainValidationSplitSuite
}

test("transformSchema should check estimatorParamMaps") {
import TrainValidationSplitSuite._
import TrainValidationSplitSuite.{MyEstimator, MyEvaluator}

val est = new MyEstimator("est")
val eval = new MyEvaluator
Expand Down Expand Up @@ -134,6 +134,82 @@ class TrainValidationSplitSuite

assert(tvs.getTrainRatio === tvs2.getTrainRatio)
assert(tvs.getSeed === tvs2.getSeed)

ValidatorParamsSuiteHelpers
.compareParamMaps(tvs.getEstimatorParamMaps, tvs2.getEstimatorParamMaps)

tvs2.getEstimator match {
case lr2: LogisticRegression =>
assert(lr.uid === lr2.uid)
assert(lr.getMaxIter === lr2.getMaxIter)
case other =>
throw new AssertionError(s"Loaded TrainValidationSplit expected estimator of type" +
s" LogisticRegression but found ${other.getClass.getName}")
}
}

test("read/write: TrainValidationSplit with nested estimator") {
val ova = new OneVsRest()
.setClassifier(new LogisticRegression)
val evaluator = new BinaryClassificationEvaluator()
.setMetricName("areaUnderPR") // not default metric
val classifier1 = new LogisticRegression().setRegParam(2.0)
val classifier2 = new LogisticRegression().setRegParam(3.0)
val paramMaps = new ParamGridBuilder()
.addGrid(ova.classifier, Array(classifier1, classifier2))
.build()
val tvs = new TrainValidationSplit()
.setEstimator(ova)
.setEvaluator(evaluator)
.setTrainRatio(0.5)
.setEstimatorParamMaps(paramMaps)
.setSeed(42L)

val tvs2 = testDefaultReadWrite(tvs, testParams = false)

assert(tvs.getTrainRatio === tvs2.getTrainRatio)
assert(tvs.getSeed === tvs2.getSeed)

tvs2.getEstimator match {
case ova2: OneVsRest =>
assert(ova.uid === ova2.uid)
val classifier = ova2.getClassifier
classifier match {
case lr: LogisticRegression =>
assert(ova.getClassifier.asInstanceOf[LogisticRegression].getMaxIter
=== lr.getMaxIter)
case _ =>
throw new AssertionError(s"Loaded TrainValidationSplit expected estimator of type" +
s" LogisticREgression but found ${classifier.getClass.getName}")
}

case other =>
throw new AssertionError(s"Loaded TrainValidationSplit expected estimator of type" +
s" OneVsRest but found ${other.getClass.getName}")
}

ValidatorParamsSuiteHelpers
.compareParamMaps(tvs.getEstimatorParamMaps, tvs2.getEstimatorParamMaps)
}

test("read/write: Persistence of nested estimator works if parent directory changes") {
val ova = new OneVsRest()
.setClassifier(new LogisticRegression)
val evaluator = new BinaryClassificationEvaluator()
.setMetricName("areaUnderPR") // not default metric
val classifier1 = new LogisticRegression().setRegParam(2.0)
val classifier2 = new LogisticRegression().setRegParam(3.0)
val paramMaps = new ParamGridBuilder()
.addGrid(ova.classifier, Array(classifier1, classifier2))
.build()
val tvs = new TrainValidationSplit()
.setEstimator(ova)
.setEvaluator(evaluator)
.setTrainRatio(0.5)
.setEstimatorParamMaps(paramMaps)
.setSeed(42L)

ValidatorParamsSuiteHelpers.testFileMove(tvs)
}

test("read/write: TrainValidationSplitModel") {
Expand All @@ -160,7 +236,7 @@ class TrainValidationSplitSuite
}
}

object TrainValidationSplitSuite {
object TrainValidationSplitSuite extends SparkFunSuite{

abstract class MyModel extends Model[MyModel]

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* 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.ml.tuning

import java.io.File
import java.nio.file.{Files, StandardCopyOption}

import org.apache.spark.SparkFunSuite
import org.apache.spark.ml.param.{ParamMap, ParamPair, Params}
import org.apache.spark.ml.util.{DefaultReadWriteTest, Identifiable, MLReader, MLWritable}

object ValidatorParamsSuiteHelpers extends SparkFunSuite with DefaultReadWriteTest {
/**
* Assert sequences of estimatorParamMaps are identical.
* If the values for a parameter are not directly comparable with ===
* and are instead Params types themselves then their corresponding paramMaps
* are compared against each other.
*/
def compareParamMaps(pMaps: Array[ParamMap], pMaps2: Array[ParamMap]): Unit = {
assert(pMaps.length === pMaps2.length)
pMaps.zip(pMaps2).foreach { case (pMap, pMap2) =>
assert(pMap.size === pMap2.size)
pMap.toSeq.foreach { case ParamPair(p, v) =>
assert(pMap2.contains(p))
val otherParam = pMap2(p)
v match {
case estimator: Params =>
otherParam match {
case estimator2: Params =>
val estimatorParamMap = Array(estimator.extractParamMap())
val estimatorParamMap2 = Array(estimator2.extractParamMap())
compareParamMaps(estimatorParamMap, estimatorParamMap2)
case other =>
throw new AssertionError(s"Expected parameter of type Params but" +
s" found ${otherParam.getClass.getName}")
}
case _ =>
assert(otherParam === v)
}
}
}
}

/**
* When nested estimators (ex. OneVsRest) are saved within meta-algorithms such as
* CrossValidator and TrainValidationSplit, relative paths should be used to store
* the path of the estimator so that if the parent directory changes, loading the
* model still works.
*/
def testFileMove[T <: Params with MLWritable](instance: T): Unit = {
val uid = instance.uid
val subdirName = Identifiable.randomUID("test")

val subdir = new File(tempDir, subdirName)
val subDirWithUid = new File(subdir, uid)

instance.save(subDirWithUid.getPath)

val newSubdirName = Identifiable.randomUID("test_moved")
val newSubdir = new File(tempDir, newSubdirName)
val newSubdirWithUid = new File(newSubdir, uid)

Files.createDirectory(newSubdir.toPath)
Files.createDirectory(newSubdirWithUid.toPath)
Files.move(subDirWithUid.toPath, newSubdirWithUid.toPath, StandardCopyOption.ATOMIC_MOVE)

val loader = instance.getClass.getMethod("read").invoke(null).asInstanceOf[MLReader[T]]
val newInstance = loader.load(newSubdirWithUid.getPath)
assert(uid == newInstance.uid)
}
}
Loading