-
Notifications
You must be signed in to change notification settings - Fork 25k
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
[ML] Add hyperparameter model metadata #66349
Merged
benwtrent
merged 21 commits into
elastic:master
from
valeriy42:hyperparameter-model-metadata
Dec 18, 2020
Merged
Changes from 7 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
d575513
init commit
valeriy42 e244d66
continue adjusting tests
valeriy42 f6236fb
fix ChunkedTrainedModelPersisterIT.java
valeriy42 48a3645
formatting
valeriy42 d28b824
rename hyperparameters, add supplied
valeriy42 ff27fab
code review
valeriy42 0c8e00b
Update x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/data…
valeriy42 f73ec7f
Update x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/…
valeriy42 f301ac7
Update x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/…
valeriy42 5515700
optional fields
valeriy42 b4de908
Merge branch 'hyperparameter-model-metadata' of https://github.com/va…
valeriy42 33b8af7
handle optional importance fields
valeriy42 f14592b
formatting
valeriy42 0399e10
Update x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/…
valeriy42 8be327d
Update x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/…
valeriy42 be37937
Update x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/…
valeriy42 f9e6ef2
Update x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/…
valeriy42 62dab59
Update x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/…
valeriy42 3a5182d
Update x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/…
valeriy42 cbf0dc8
fix dangling comma
valeriy42 43dbb5b
Merge branch 'master' into hyperparameter-model-metadata
elasticmachine 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 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 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
118 changes: 118 additions & 0 deletions
118
...java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/metadata/Hyperparameters.java
This file contains 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,118 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.core.ml.inference.trainedmodel.metadata; | ||
|
||
import org.elasticsearch.common.ParseField; | ||
import org.elasticsearch.common.io.stream.StreamInput; | ||
import org.elasticsearch.common.io.stream.StreamOutput; | ||
import org.elasticsearch.common.io.stream.Writeable; | ||
import org.elasticsearch.common.xcontent.ConstructingObjectParser; | ||
import org.elasticsearch.common.xcontent.ToXContentObject; | ||
import org.elasticsearch.common.xcontent.XContentBuilder; | ||
import org.elasticsearch.common.xcontent.XContentParser; | ||
|
||
import java.io.IOException; | ||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
public class Hyperparameters implements ToXContentObject, Writeable { | ||
|
||
private static final String NAME = "hyperparameters"; | ||
public static final ParseField HYPERPARAMETER_NAME = new ParseField("name"); | ||
public static final ParseField VALUE = new ParseField("value"); | ||
public static final ParseField ABSOLUTE_IMPORTANCE = new ParseField("absolute_importance"); | ||
public static final ParseField RELATIVE_IMPORTANCE = new ParseField("relative_importance"); | ||
public static final ParseField SUPPLIED = new ParseField("supplied"); | ||
|
||
|
||
// These parsers follow the pattern that metadata is parsed leniently (to allow for enhancements), whilst config is parsed strictly | ||
public static final ConstructingObjectParser<Hyperparameters, Void> LENIENT_PARSER = createParser(true); | ||
public static final ConstructingObjectParser<Hyperparameters, Void> STRICT_PARSER = createParser(false); | ||
|
||
@SuppressWarnings("unchecked") | ||
private static ConstructingObjectParser<Hyperparameters, Void> createParser(boolean ignoreUnknownFields) { | ||
ConstructingObjectParser<Hyperparameters, Void> parser = new ConstructingObjectParser<>(NAME, | ||
ignoreUnknownFields, | ||
a -> new Hyperparameters((String)a[0], (Double)a[1], (Double)a[2], (Double)a[3], (Boolean)a[4])); | ||
parser.declareString(ConstructingObjectParser.constructorArg(), HYPERPARAMETER_NAME); | ||
parser.declareDouble(ConstructingObjectParser.constructorArg(), VALUE); | ||
parser.declareDouble(ConstructingObjectParser.constructorArg(), ABSOLUTE_IMPORTANCE); | ||
parser.declareDouble(ConstructingObjectParser.constructorArg(), RELATIVE_IMPORTANCE); | ||
parser.declareBoolean(ConstructingObjectParser.constructorArg(), SUPPLIED); | ||
return parser; | ||
} | ||
|
||
public static Hyperparameters fromXContent(XContentParser parser, boolean lenient) throws IOException { | ||
return lenient ? LENIENT_PARSER.parse(parser, null) : STRICT_PARSER.parse(parser, null); | ||
} | ||
|
||
public final String hyperparameterName; | ||
public final Double value; | ||
public final Double absoluteImportance; | ||
public final Double relativeImportance; | ||
public final Boolean supplied; | ||
|
||
public Hyperparameters(StreamInput in) throws IOException { | ||
this.hyperparameterName = in.readString(); | ||
this.value = in.readDouble(); | ||
this.absoluteImportance = in.readDouble(); | ||
this.relativeImportance = in.readDouble(); | ||
this.supplied = in.readBoolean(); | ||
} | ||
|
||
Hyperparameters(String hyperparameterName, Double value, Double absoluteImportance, Double relativeImportance, Boolean supplied) { | ||
this.hyperparameterName = hyperparameterName; | ||
this.value = value; | ||
this.absoluteImportance = absoluteImportance; | ||
this.relativeImportance = relativeImportance; | ||
this.supplied = supplied; | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput out) throws IOException { | ||
out.writeString(hyperparameterName); | ||
out.writeDouble(value); | ||
out.writeDouble(absoluteImportance); | ||
out.writeDouble(relativeImportance); | ||
out.writeBoolean(supplied); | ||
} | ||
|
||
@Override | ||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | ||
return builder.map(asMap()); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
Hyperparameters that = (Hyperparameters) o; | ||
return Objects.equals(that.hyperparameterName, hyperparameterName) | ||
&& Objects.equals(value, that.value) | ||
&& Objects.equals(absoluteImportance, that.absoluteImportance) | ||
&& Objects.equals(relativeImportance, that.relativeImportance) | ||
&& Objects.equals(supplied, that.supplied) | ||
; | ||
} | ||
|
||
public Map<String, Object> asMap() { | ||
Map<String, Object> map = new LinkedHashMap<>(); | ||
map.put(HYPERPARAMETER_NAME.getPreferredName(), hyperparameterName); | ||
map.put(VALUE.getPreferredName(), value); | ||
map.put(ABSOLUTE_IMPORTANCE.getPreferredName(), absoluteImportance); | ||
map.put(RELATIVE_IMPORTANCE.getPreferredName(), relativeImportance); | ||
map.put(SUPPLIED.getPreferredName(), supplied); | ||
|
||
return map; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(hyperparameterName, value, absoluteImportance, relativeImportance, supplied); | ||
} | ||
} |
This file contains 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 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 |
---|---|---|
|
@@ -127,6 +127,27 @@ | |
} | ||
} | ||
} | ||
}, | ||
"hyperparameters": { | ||
"type": "nested", | ||
"dynamic": "false", | ||
"properties": { | ||
"name": { | ||
"type": "keyword" | ||
}, | ||
"value": { | ||
"type": "double" | ||
}, | ||
"absolute_importance": { | ||
"type": "double" | ||
}, | ||
"relative_importance": { | ||
"type": "double" | ||
}, | ||
"supplied": { | ||
"type": "boolean" | ||
}, | ||
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. ^ dangling comma |
||
} | ||
} | ||
} | ||
} | ||
|
This file contains 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
60 changes: 60 additions & 0 deletions
60
...org/elasticsearch/xpack/core/ml/inference/trainedmodel/metadata/HyperparametersTests.java
This file contains 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,60 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
package org.elasticsearch.xpack.core.ml.inference.trainedmodel.metadata; | ||
|
||
import org.elasticsearch.Version; | ||
import org.elasticsearch.common.io.stream.Writeable; | ||
import org.elasticsearch.common.xcontent.XContentParser; | ||
import org.elasticsearch.xpack.core.ml.AbstractBWCSerializationTestCase; | ||
import org.junit.Before; | ||
|
||
import java.io.IOException; | ||
|
||
|
||
public class HyperparametersTests extends AbstractBWCSerializationTestCase<Hyperparameters> { | ||
|
||
private boolean lenient; | ||
|
||
@SuppressWarnings("unchecked") | ||
public static Hyperparameters randomInstance() { | ||
return new Hyperparameters( | ||
valeriy42 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
randomAlphaOfLength(10), | ||
randomDoubleBetween(0.0, 1.0, true), | ||
randomDoubleBetween(0.0, 100.0, true), | ||
randomDoubleBetween(0.0, 1.0, true), | ||
randomBoolean()); | ||
valeriy42 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
@Before | ||
public void chooseStrictOrLenient() { | ||
lenient = randomBoolean(); | ||
} | ||
|
||
@Override | ||
protected Hyperparameters createTestInstance() { | ||
return randomInstance(); | ||
} | ||
|
||
@Override | ||
protected Writeable.Reader<Hyperparameters> instanceReader() { | ||
return Hyperparameters::new; | ||
} | ||
|
||
@Override | ||
protected Hyperparameters doParseInstance(XContentParser parser) throws IOException { | ||
return Hyperparameters.fromXContent(parser, lenient); | ||
} | ||
|
||
@Override | ||
protected boolean supportsUnknownFields() { | ||
return lenient; | ||
} | ||
|
||
@Override | ||
protected Hyperparameters mutateInstanceForVersion(Hyperparameters instance, Version version) { | ||
return instance; | ||
} | ||
} |
This file contains 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
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.
If the
Double
s and theBoolean
are truly not nullable, they should bedouble
andboolean
(unboxed, not nullable).If they are nullable, the parser needs to treat them as optional and the serialization needs to as well