Skip to content

Commit

Permalink
[ML][Inference] Adding preprocessors to definition object (elastic#47320
Browse files Browse the repository at this point in the history
)

* [ML][Inference] Adding preprocessors to definition object

* Update TrainedModelConfig.java
  • Loading branch information
benwtrent committed Oct 1, 2019
1 parent c93b39c commit 525a601
Show file tree
Hide file tree
Showing 10 changed files with 523 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import org.elasticsearch.Version;
import org.elasticsearch.client.common.TimeUtil;
import org.elasticsearch.client.ml.inference.trainedmodel.TrainedModel;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.xcontent.ObjectParser;
Expand All @@ -31,7 +30,6 @@
import java.io.IOException;
import java.time.Instant;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;

Expand Down Expand Up @@ -64,9 +62,8 @@ public class TrainedModelConfig implements ToXContentObject {
PARSER.declareLong(TrainedModelConfig.Builder::setModelVersion, MODEL_VERSION);
PARSER.declareString(TrainedModelConfig.Builder::setModelType, MODEL_TYPE);
PARSER.declareObject(TrainedModelConfig.Builder::setMetadata, (p, c) -> p.map(), METADATA);
PARSER.declareNamedObjects(TrainedModelConfig.Builder::setDefinition,
(p, c, n) -> p.namedObject(TrainedModel.class, n, null),
(modelDocBuilder) -> { /* Noop does not matter client side */ },
PARSER.declareObject(TrainedModelConfig.Builder::setDefinition,
(p, c) -> TrainedModelDefinition.fromXContent(p),
DEFINITION);
}

Expand All @@ -82,7 +79,7 @@ public static TrainedModelConfig.Builder fromXContent(XContentParser parser) thr
private final Long modelVersion;
private final String modelType;
private final Map<String, Object> metadata;
private final TrainedModel definition;
private final TrainedModelDefinition definition;

TrainedModelConfig(String modelId,
String createdBy,
Expand All @@ -91,7 +88,7 @@ public static TrainedModelConfig.Builder fromXContent(XContentParser parser) thr
Instant createdTime,
Long modelVersion,
String modelType,
TrainedModel definition,
TrainedModelDefinition definition,
Map<String, Object> metadata) {
this.modelId = modelId;
this.createdBy = createdBy;
Expand Down Expand Up @@ -136,7 +133,7 @@ public Map<String, Object> getMetadata() {
return metadata;
}

public TrainedModel getDefinition() {
public TrainedModelDefinition getDefinition() {
return definition;
}

Expand Down Expand Up @@ -169,11 +166,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
builder.field(MODEL_TYPE.getPreferredName(), modelType);
}
if (definition != null) {
NamedXContentObjectHelper.writeNamedObjects(builder,
params,
false,
DEFINITION.getPreferredName(),
Collections.singletonList(definition));
builder.field(DEFINITION.getPreferredName(), definition);
}
if (metadata != null) {
builder.field(METADATA.getPreferredName(), metadata);
Expand Down Expand Up @@ -227,7 +220,7 @@ public static class Builder {
private Long modelVersion;
private String modelType;
private Map<String, Object> metadata;
private TrainedModel definition;
private TrainedModelDefinition.Builder definition;

public Builder setModelId(String modelId) {
this.modelId = modelId;
Expand Down Expand Up @@ -273,16 +266,11 @@ public Builder setMetadata(Map<String, Object> metadata) {
return this;
}

public Builder setDefinition(TrainedModel definition) {
public Builder setDefinition(TrainedModelDefinition.Builder definition) {
this.definition = definition;
return this;
}

private Builder setDefinition(List<TrainedModel> definition) {
assert definition.size() == 1;
return setDefinition(definition.get(0));
}

public TrainedModelConfig build() {
return new TrainedModelConfig(
modelId,
Expand All @@ -292,7 +280,7 @@ public TrainedModelConfig build() {
createdTime,
modelVersion,
modelType,
definition,
definition == null ? null : definition.build(),
metadata);
}
}
Expand Down
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);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
*/
package org.elasticsearch.client.ml.inference.preprocessing;

import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.client.ml.inference.NamedXContentObject;


/**
* Describes a pre-processor for a defined machine learning model
*/
public interface PreProcessor extends ToXContentObject {
public interface PreProcessor extends NamedXContentObject {

/**
* @return The name of the pre-processor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package org.elasticsearch.client.ml.inference;

import org.elasticsearch.Version;
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;
Expand Down Expand Up @@ -61,7 +60,7 @@ protected TrainedModelConfig createTestInstance() {
Instant.ofEpochMilli(randomNonNegativeLong()),
randomBoolean() ? null : randomNonNegativeLong(),
randomAlphaOfLength(10),
randomFrom(TreeTests.createRandom()),
randomBoolean() ? null : TrainedModelDefinitionTests.createRandomBuilder().build(),
randomBoolean() ? null : Collections.singletonMap(randomAlphaOfLength(10), randomAlphaOfLength(10)));
}

Expand Down
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);
}

}
Loading

0 comments on commit 525a601

Please sign in to comment.