-
Notifications
You must be signed in to change notification settings - Fork 29k
[Spark-21221][ML] CrossValidator and TrainValidationSplit Persist Nested Estimators such as OneVsRest #18428
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
ajaysaini725
wants to merge
12
commits into
apache:master
from
ajaysaini725:MetaAlgorithmPersistNestedEstimators
Closed
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
8390437
Added functionality for CrossValidator and TrainValidationSplit to pe…
ajaysaini725 76aece5
Responded to first round of code review on Scala nested estimator per…
ajaysaini725 a0dbf6c
Cleaned up python meta algorithm persistence code. CrossValidation an…
ajaysaini725 253f39e
Small style fix.
ajaysaini725 89be87e
Attempt at removing two of the duplicated persistence functions in On…
ajaysaini725 44342b2
Made changes based on pull request comments.
ajaysaini725 823593d
Added ValidatorParamsSuiteHelpers file.
ajaysaini725 f169aa5
Fixed backwards compatibility issue
ajaysaini725 7601df7
Fixed small style change
ajaysaini725 231ec55
Changed saving of nested estimators to store the path of the estimato…
ajaysaini725 a6bd197
Fixed indentation
ajaysaini725 6a7162d
Better indentation fix
ajaysaini725 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
mllib/src/test/scala/org/apache/spark/ml/tuning/ValidatorParamsSuiteHelpers.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same for the TrainValidationSplitSuite