-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-24058][ML][PySpark] Default Params in ML should be saved separately: Python API #21153
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
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f32350f
Default Params in ML should be saved separately in Python API.
viirya 526fa4a
Remove redundant call to getParam.
viirya 86fa433
Merge remote-tracking branch 'upstream/master' into SPARK-24058
viirya b47beab
Sync with updated majorAndMinorVersions API.
viirya ce84137
Merge remote-tracking branch 'upstream/master' into SPARK-24058
viirya dc59375
Fix typo.
viirya 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,6 +30,7 @@ | |
| from pyspark import SparkContext, since | ||
| from pyspark.ml.common import inherit_doc | ||
| from pyspark.sql import SparkSession | ||
| from pyspark.util import VersionUtils | ||
|
|
||
|
|
||
| def _jvm(): | ||
|
|
@@ -396,6 +397,7 @@ def saveMetadata(instance, path, sc, extraMetadata=None, paramMap=None): | |
| - sparkVersion | ||
| - uid | ||
| - paramMap | ||
| - defaultParamMap (since 2.4.0) | ||
| - (optionally, extra metadata) | ||
| :param extraMetadata: Extra metadata to be saved at same level as uid, paramMap, etc. | ||
| :param paramMap: If given, this is saved in the "paramMap" field. | ||
|
|
@@ -417,15 +419,24 @@ def _get_metadata_to_save(instance, sc, extraMetadata=None, paramMap=None): | |
| """ | ||
| uid = instance.uid | ||
| cls = instance.__module__ + '.' + instance.__class__.__name__ | ||
| params = instance.extractParamMap() | ||
|
|
||
| # User-supplied param values | ||
| params = instance._paramMap | ||
| jsonParams = {} | ||
| if paramMap is not None: | ||
| jsonParams = paramMap | ||
| else: | ||
| for p in params: | ||
| jsonParams[p.name] = params[p] | ||
|
|
||
| # Default param values | ||
| jsonDefaultParams = {} | ||
| for p in instance._defaultParamMap: | ||
| jsonDefaultParams[p.name] = instance._defaultParamMap[p] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. similar, use
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto. |
||
|
|
||
| basicMetadata = {"class": cls, "timestamp": long(round(time.time() * 1000)), | ||
| "sparkVersion": sc.version, "uid": uid, "paramMap": jsonParams} | ||
| "sparkVersion": sc.version, "uid": uid, "paramMap": jsonParams, | ||
| "defaultParamMap": jsonDefaultParams} | ||
| if extraMetadata is not None: | ||
| basicMetadata.update(extraMetadata) | ||
| return json.dumps(basicMetadata, separators=[',', ':']) | ||
|
|
@@ -523,11 +534,26 @@ def getAndSetParams(instance, metadata): | |
| """ | ||
| Extract Params from metadata, and set them in the instance. | ||
| """ | ||
| # Set user-supplied param values | ||
| for paramName in metadata['paramMap']: | ||
| param = instance.getParam(paramName) | ||
| paramValue = metadata['paramMap'][paramName] | ||
| instance.set(param, paramValue) | ||
|
|
||
| # Set default param values | ||
| majorAndMinorVersions = VersionUtils.majorMinorVersion(metadata['sparkVersion']) | ||
| major = majorAndMinorVersions[0] | ||
| minor = majorAndMinorVersions[1] | ||
|
|
||
| # For metadata file prior to Spark 2.4, there is no default section. | ||
| if major > 2 or (major == 2 and minor >= 4): | ||
| assert 'defaultParamMap' in metadata, "Error loading metadata: Expected " + \ | ||
| "`defaultParamMap` section not found" | ||
|
|
||
| for paramName in metadata['defaultParamMap']: | ||
| paramValue = metadata['defaultParamMap'][paramName] | ||
| instance._setDefault(**{paramName: paramValue}) | ||
|
|
||
| @staticmethod | ||
| def loadParamsInstance(path, sc): | ||
| """ | ||
|
|
||
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.
I think use
_paramMap.copy()will be simpler.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.
_paramMap's keys areParamnot string.