forked from elastic/elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ML][Inference] Adding preprocessors to definition object (elastic#47320
- Loading branch information
Showing
10 changed files
with
523 additions
and
67 deletions.
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
137 changes: 137 additions & 0 deletions
137
...igh-level/src/main/java/org/elasticsearch/client/ml/inference/TrainedModelDefinition.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,137 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch 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.elasticsearch.client.ml.inference; | ||
|
||
import org.elasticsearch.client.ml.inference.preprocessing.PreProcessor; | ||
import org.elasticsearch.client.ml.inference.trainedmodel.TrainedModel; | ||
import org.elasticsearch.common.ParseField; | ||
import org.elasticsearch.common.Strings; | ||
import org.elasticsearch.common.xcontent.ObjectParser; | ||
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.Collections; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
public class TrainedModelDefinition implements ToXContentObject { | ||
|
||
public static final String NAME = "trained_model_doc"; | ||
|
||
public static final ParseField TRAINED_MODEL = new ParseField("trained_model"); | ||
public static final ParseField PREPROCESSORS = new ParseField("preprocessors"); | ||
|
||
public static final ObjectParser<Builder, Void> PARSER = new ObjectParser<>(NAME, | ||
true, | ||
TrainedModelDefinition.Builder::new); | ||
static { | ||
PARSER.declareNamedObjects(TrainedModelDefinition.Builder::setTrainedModel, | ||
(p, c, n) -> p.namedObject(TrainedModel.class, n, null), | ||
(modelDocBuilder) -> { /* Noop does not matter client side*/ }, | ||
TRAINED_MODEL); | ||
PARSER.declareNamedObjects(TrainedModelDefinition.Builder::setPreProcessors, | ||
(p, c, n) -> p.namedObject(PreProcessor.class, n, null), | ||
(trainedModelDefBuilder) -> {/* Does not matter client side*/ }, | ||
PREPROCESSORS); | ||
} | ||
|
||
public static TrainedModelDefinition.Builder fromXContent(XContentParser parser) throws IOException { | ||
return PARSER.parse(parser, null); | ||
} | ||
|
||
private final TrainedModel trainedModel; | ||
private final List<PreProcessor> preProcessors; | ||
|
||
TrainedModelDefinition(TrainedModel trainedModel, List<PreProcessor> preProcessors) { | ||
this.trainedModel = trainedModel; | ||
this.preProcessors = preProcessors == null ? Collections.emptyList() : Collections.unmodifiableList(preProcessors); | ||
} | ||
|
||
@Override | ||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | ||
builder.startObject(); | ||
NamedXContentObjectHelper.writeNamedObjects(builder, | ||
params, | ||
false, | ||
TRAINED_MODEL.getPreferredName(), | ||
Collections.singletonList(trainedModel)); | ||
NamedXContentObjectHelper.writeNamedObjects(builder, | ||
params, | ||
true, | ||
PREPROCESSORS.getPreferredName(), | ||
preProcessors); | ||
builder.endObject(); | ||
return builder; | ||
} | ||
|
||
public TrainedModel getTrainedModel() { | ||
return trainedModel; | ||
} | ||
|
||
public List<PreProcessor> getPreProcessors() { | ||
return preProcessors; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return Strings.toString(this); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
TrainedModelDefinition that = (TrainedModelDefinition) o; | ||
return Objects.equals(trainedModel, that.trainedModel) && | ||
Objects.equals(preProcessors, that.preProcessors) ; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(trainedModel, preProcessors); | ||
} | ||
|
||
public static class Builder { | ||
|
||
private List<PreProcessor> preProcessors; | ||
private TrainedModel trainedModel; | ||
|
||
public Builder setPreProcessors(List<PreProcessor> preProcessors) { | ||
this.preProcessors = preProcessors; | ||
return this; | ||
} | ||
|
||
public Builder setTrainedModel(TrainedModel trainedModel) { | ||
this.trainedModel = trainedModel; | ||
return this; | ||
} | ||
|
||
private Builder setTrainedModel(List<TrainedModel> trainedModel) { | ||
assert trainedModel.size() == 1; | ||
return setTrainedModel(trainedModel.get(0)); | ||
} | ||
|
||
public TrainedModelDefinition build() { | ||
return new TrainedModelDefinition(this.trainedModel, this.preProcessors); | ||
} | ||
} | ||
|
||
} |
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
83 changes: 83 additions & 0 deletions
83
...evel/src/test/java/org/elasticsearch/client/ml/inference/TrainedModelDefinitionTests.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,83 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch 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.elasticsearch.client.ml.inference; | ||
|
||
import org.elasticsearch.client.ml.inference.preprocessing.FrequencyEncodingTests; | ||
import org.elasticsearch.client.ml.inference.preprocessing.OneHotEncodingTests; | ||
import org.elasticsearch.client.ml.inference.preprocessing.TargetMeanEncodingTests; | ||
import org.elasticsearch.client.ml.inference.trainedmodel.tree.TreeTests; | ||
import org.elasticsearch.common.settings.Settings; | ||
import org.elasticsearch.common.xcontent.NamedXContentRegistry; | ||
import org.elasticsearch.common.xcontent.XContentParser; | ||
import org.elasticsearch.search.SearchModule; | ||
import org.elasticsearch.test.AbstractXContentTestCase; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.function.Predicate; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
|
||
public class TrainedModelDefinitionTests extends AbstractXContentTestCase<TrainedModelDefinition> { | ||
|
||
@Override | ||
protected TrainedModelDefinition doParseInstance(XContentParser parser) throws IOException { | ||
return TrainedModelDefinition.fromXContent(parser).build(); | ||
} | ||
|
||
@Override | ||
protected boolean supportsUnknownFields() { | ||
return true; | ||
} | ||
|
||
@Override | ||
protected Predicate<String> getRandomFieldsExcludeFilter() { | ||
return field -> !field.isEmpty(); | ||
} | ||
|
||
public static TrainedModelDefinition.Builder createRandomBuilder() { | ||
int numberOfProcessors = randomIntBetween(1, 10); | ||
return new TrainedModelDefinition.Builder() | ||
.setPreProcessors( | ||
randomBoolean() ? null : | ||
Stream.generate(() -> randomFrom(FrequencyEncodingTests.createRandom(), | ||
OneHotEncodingTests.createRandom(), | ||
TargetMeanEncodingTests.createRandom())) | ||
.limit(numberOfProcessors) | ||
.collect(Collectors.toList())) | ||
.setTrainedModel(randomFrom(TreeTests.createRandom())); | ||
} | ||
|
||
@Override | ||
protected TrainedModelDefinition createTestInstance() { | ||
return createRandomBuilder().build(); | ||
} | ||
|
||
@Override | ||
protected NamedXContentRegistry xContentRegistry() { | ||
List<NamedXContentRegistry.Entry> namedXContent = new ArrayList<>(); | ||
namedXContent.addAll(new MlInferenceNamedXContentProvider().getNamedXContentParsers()); | ||
namedXContent.addAll(new SearchModule(Settings.EMPTY, Collections.emptyList()).getNamedXContents()); | ||
return new NamedXContentRegistry(namedXContent); | ||
} | ||
|
||
} |
Oops, something went wrong.