http://git-wip-us.apache.org/repos/asf/ignite/blob/2dc0d9f7/modules/ml/src/main/java/org/apache/ignite/ml/inference/parser/ModelParser.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/inference/parser/ModelParser.java b/modules/ml/src/main/java/org/apache/ignite/ml/inference/parser/ModelParser.java new file mode 100644 index 0000000..f03e34a --- /dev/null +++ b/modules/ml/src/main/java/org/apache/ignite/ml/inference/parser/ModelParser.java @@ -0,0 +1,38 @@ +/* + * 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.ignite.ml.inference.parser; + +import java.io.Serializable; +import org.apache.ignite.ml.inference.Model; + +/** + * Model parser that accepts a serialized model represented by byte array, parses it and returns {@link Model}. + * + * @param <I> Type of model input. + * @param <O> Type of model output. + */ +@FunctionalInterface +public interface ModelParser<I, O, M extends Model<I, O>> extends Serializable { + /** + * Accepts serialized model represented by byte array, parses it and returns {@link Model}. + * + * @param mdl Serialized model represented by byte array. + * @return Inference model. + */ + public M parse(byte[] mdl); +}
http://git-wip-us.apache.org/repos/asf/ignite/blob/2dc0d9f7/modules/ml/src/main/java/org/apache/ignite/ml/inference/parser/TensorFlowBaseInfModelParser.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/inference/parser/TensorFlowBaseInfModelParser.java b/modules/ml/src/main/java/org/apache/ignite/ml/inference/parser/TensorFlowBaseInfModelParser.java deleted file mode 100644 index d79b5de..0000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/inference/parser/TensorFlowBaseInfModelParser.java +++ /dev/null @@ -1,216 +0,0 @@ -/* - * 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.ignite.ml.inference.parser; - -import java.io.Serializable; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import org.apache.ignite.ml.inference.InfModel; -import org.tensorflow.Session; -import org.tensorflow.Tensor; - -/** - * Base class for TensorFlow model parsers. Contains the logic that is common for models saved as "SavedModel" and as a - * simple graph. - * - * @param <I> Type of model input. - * @param <O> Type of model output. - */ -public abstract class TensorFlowBaseInfModelParser<I, O> implements InfModelParser<I, O, InfModel<I, O>> { - /** */ - private static final long serialVersionUID = 5574259553625871456L; - - /** Map of input graph nodes (placeholders) and transformers that allow to transform input into tensor. */ - private final Map<String, InputTransformer<I>> inputs = new HashMap<>(); - - /** List of output graph nodes. */ - private List<String> outputNames; - - /** Transformer that allows to transform tensors into output. */ - private OutputTransformer<O> outputTransformer; - - /** {@inheritDoc} */ - @Override public InfModel<I, O> parse(byte[] mdl) { - return new TensorFlowInfModel(parseModel(mdl)); - } - - /** - * Parses model specified in serialized form as byte array. - * - * @param mdl Inference model in serialized form as byte array. - * @return TensorFlow session that encapsulates the TensorFlow graph parsed from serialized model. - */ - public abstract Session parseModel(byte[] mdl); - - /** - * Setter that allows to specify additional input graph node and correspondent transformer that allows to transform - * input into tensor. - * - * @param name Name of the input graph node. - * @param transformer Transformer that allows to transform input into tensor. - * @return This instance. - */ - public TensorFlowBaseInfModelParser<I, O> withInput(String name, InputTransformer<I> transformer) { - if (inputs.containsKey(name)) - throw new IllegalArgumentException("Inputs already contains specified name [name=" + name + "]"); - - inputs.put(name, transformer); - - return this; - } - - /** - * Setter that allows to specify output graph nodes and correspondent transformer that allow to transform tensors - * into output. - * - * @param names List of output graph node names. - * @param transformer Transformer that allow to transform tensors into output. - * @return This instance. - */ - public TensorFlowBaseInfModelParser<I, O> withOutput(List<String> names, OutputTransformer<O> transformer) { - if (outputNames != null || outputTransformer != null) - throw new IllegalArgumentException("Outputs already specified"); - - outputNames = names; - outputTransformer = transformer; - - return this; - } - - /** - * Input transformer that accepts input and transforms it into tensor. - * - * @param <I> Type of model input. - */ - @FunctionalInterface - public interface InputTransformer<I> extends Serializable { - /** - * Transforms input into tensor. - * - * @param input Input data. - * @return Tensor (transformed input data). - */ - public Tensor<?> transform(I input); - } - - /** - * Output transformer that accepts tensors and transforms them into output. - * - * @param <O> Type of model output. - */ - @FunctionalInterface - public interface OutputTransformer<O> extends Serializable { - /** - * Transforms tensors into output. - * - * @param output Tensors. - * @return Output (transformed tensors). - */ - public O transform(Map<String, Tensor<?>> output); - } - - /** - * TensorFlow inference model based on pre-loaded graph and created session. - */ - private class TensorFlowInfModel implements InfModel<I, O> { - /** TensorFlow session. */ - private final Session ses; - - /** - * Constructs a new instance of TensorFlow inference model. - * - * @param ses TensorFlow session. - */ - TensorFlowInfModel(Session ses) { - this.ses = ses; - } - - /** {@inheritDoc} */ - @Override public O apply(I input) { - Session.Runner runner = ses.runner(); - - runner = feedAll(runner, input); - runner = fetchAll(runner); - - List<Tensor<?>> prediction = runner.run(); - Map<String, Tensor<?>> collectedPredictionTensors = indexTensors(prediction); - - return outputTransformer.transform(collectedPredictionTensors); - } - - /** - * Feeds input into graphs input nodes using input transformers (see {@link #inputs}). - * - * @param runner TensorFlow session runner. - * @param input Input. - * @return TensorFlow session runner. - */ - private Session.Runner feedAll(Session.Runner runner, I input) { - for (Map.Entry<String, InputTransformer<I>> e : inputs.entrySet()) { - String opName = e.getKey(); - InputTransformer<I> transformer = e.getValue(); - - runner = runner.feed(opName, transformer.transform(input)); - } - - return runner; - } - - /** - * Specifies graph output nodes to be fetched using {@link #outputNames}. - * - * @param runner TensorFlow session runner. - * @return TensorFlow session runner. - */ - private Session.Runner fetchAll(Session.Runner runner) { - for (String e : outputNames) - runner.fetch(e); - - return runner; - } - - /** - * Indexes tensors fetched from graph using {@link #outputNames}. - * - * @param tensors List of fetched tensors. - * @return Map of tensor name as a key and tensor as a value. - */ - private Map<String, Tensor<?>> indexTensors(List<Tensor<?>> tensors) { - Map<String, Tensor<?>> collectedTensors = new HashMap<>(); - - Iterator<String> outputNamesIter = outputNames.iterator(); - Iterator<Tensor<?>> tensorsIter = tensors.iterator(); - - while (outputNamesIter.hasNext() && tensorsIter.hasNext()) - collectedTensors.put(outputNamesIter.next(), tensorsIter.next()); - - // We expect that output names and output tensors have the same size. - if (outputNamesIter.hasNext() || tensorsIter.hasNext()) - throw new IllegalStateException("Outputs are incorrect"); - - return collectedTensors; - } - - /** {@inheritDoc} */ - @Override public void close() { - ses.close(); - } - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/2dc0d9f7/modules/ml/src/main/java/org/apache/ignite/ml/inference/parser/TensorFlowBaseModelParser.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/inference/parser/TensorFlowBaseModelParser.java b/modules/ml/src/main/java/org/apache/ignite/ml/inference/parser/TensorFlowBaseModelParser.java new file mode 100644 index 0000000..b40486d --- /dev/null +++ b/modules/ml/src/main/java/org/apache/ignite/ml/inference/parser/TensorFlowBaseModelParser.java @@ -0,0 +1,216 @@ +/* + * 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.ignite.ml.inference.parser; + +import java.io.Serializable; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import org.apache.ignite.ml.inference.Model; +import org.tensorflow.Session; +import org.tensorflow.Tensor; + +/** + * Base class for TensorFlow model parsers. Contains the logic that is common for models saved as "SavedModel" and as a + * simple graph. + * + * @param <I> Type of model input. + * @param <O> Type of model output. + */ +public abstract class TensorFlowBaseModelParser<I, O> implements ModelParser<I, O, Model<I, O>> { + /** */ + private static final long serialVersionUID = 5574259553625871456L; + + /** Map of input graph nodes (placeholders) and transformers that allow to transform input into tensor. */ + private final Map<String, InputTransformer<I>> inputs = new HashMap<>(); + + /** List of output graph nodes. */ + private List<String> outputNames; + + /** Transformer that allows to transform tensors into output. */ + private OutputTransformer<O> outputTransformer; + + /** {@inheritDoc} */ + @Override public Model<I, O> parse(byte[] mdl) { + return new TensorFlowInfModel(parseModel(mdl)); + } + + /** + * Parses model specified in serialized form as byte array. + * + * @param mdl Inference model in serialized form as byte array. + * @return TensorFlow session that encapsulates the TensorFlow graph parsed from serialized model. + */ + public abstract Session parseModel(byte[] mdl); + + /** + * Setter that allows to specify additional input graph node and correspondent transformer that allows to transform + * input into tensor. + * + * @param name Name of the input graph node. + * @param transformer Transformer that allows to transform input into tensor. + * @return This instance. + */ + public TensorFlowBaseModelParser<I, O> withInput(String name, InputTransformer<I> transformer) { + if (inputs.containsKey(name)) + throw new IllegalArgumentException("Inputs already contains specified name [name=" + name + "]"); + + inputs.put(name, transformer); + + return this; + } + + /** + * Setter that allows to specify output graph nodes and correspondent transformer that allow to transform tensors + * into output. + * + * @param names List of output graph node names. + * @param transformer Transformer that allow to transform tensors into output. + * @return This instance. + */ + public TensorFlowBaseModelParser<I, O> withOutput(List<String> names, OutputTransformer<O> transformer) { + if (outputNames != null || outputTransformer != null) + throw new IllegalArgumentException("Outputs already specified"); + + outputNames = names; + outputTransformer = transformer; + + return this; + } + + /** + * Input transformer that accepts input and transforms it into tensor. + * + * @param <I> Type of model input. + */ + @FunctionalInterface + public interface InputTransformer<I> extends Serializable { + /** + * Transforms input into tensor. + * + * @param input Input data. + * @return Tensor (transformed input data). + */ + public Tensor<?> transform(I input); + } + + /** + * Output transformer that accepts tensors and transforms them into output. + * + * @param <O> Type of model output. + */ + @FunctionalInterface + public interface OutputTransformer<O> extends Serializable { + /** + * Transforms tensors into output. + * + * @param output Tensors. + * @return Output (transformed tensors). + */ + public O transform(Map<String, Tensor<?>> output); + } + + /** + * TensorFlow inference model based on pre-loaded graph and created session. + */ + private class TensorFlowInfModel implements Model<I, O> { + /** TensorFlow session. */ + private final Session ses; + + /** + * Constructs a new instance of TensorFlow inference model. + * + * @param ses TensorFlow session. + */ + TensorFlowInfModel(Session ses) { + this.ses = ses; + } + + /** {@inheritDoc} */ + @Override public O predict(I input) { + Session.Runner runner = ses.runner(); + + runner = feedAll(runner, input); + runner = fetchAll(runner); + + List<Tensor<?>> prediction = runner.run(); + Map<String, Tensor<?>> collectedPredictionTensors = indexTensors(prediction); + + return outputTransformer.transform(collectedPredictionTensors); + } + + /** + * Feeds input into graphs input nodes using input transformers (see {@link #inputs}). + * + * @param runner TensorFlow session runner. + * @param input Input. + * @return TensorFlow session runner. + */ + private Session.Runner feedAll(Session.Runner runner, I input) { + for (Map.Entry<String, InputTransformer<I>> e : inputs.entrySet()) { + String opName = e.getKey(); + InputTransformer<I> transformer = e.getValue(); + + runner = runner.feed(opName, transformer.transform(input)); + } + + return runner; + } + + /** + * Specifies graph output nodes to be fetched using {@link #outputNames}. + * + * @param runner TensorFlow session runner. + * @return TensorFlow session runner. + */ + private Session.Runner fetchAll(Session.Runner runner) { + for (String e : outputNames) + runner.fetch(e); + + return runner; + } + + /** + * Indexes tensors fetched from graph using {@link #outputNames}. + * + * @param tensors List of fetched tensors. + * @return Map of tensor name as a key and tensor as a value. + */ + private Map<String, Tensor<?>> indexTensors(List<Tensor<?>> tensors) { + Map<String, Tensor<?>> collectedTensors = new HashMap<>(); + + Iterator<String> outputNamesIter = outputNames.iterator(); + Iterator<Tensor<?>> tensorsIter = tensors.iterator(); + + while (outputNamesIter.hasNext() && tensorsIter.hasNext()) + collectedTensors.put(outputNamesIter.next(), tensorsIter.next()); + + // We expect that output names and output tensors have the same size. + if (outputNamesIter.hasNext() || tensorsIter.hasNext()) + throw new IllegalStateException("Outputs are incorrect"); + + return collectedTensors; + } + + /** {@inheritDoc} */ + @Override public void close() { + ses.close(); + } + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/2dc0d9f7/modules/ml/src/main/java/org/apache/ignite/ml/inference/parser/TensorFlowGraphInfModelParser.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/inference/parser/TensorFlowGraphInfModelParser.java b/modules/ml/src/main/java/org/apache/ignite/ml/inference/parser/TensorFlowGraphInfModelParser.java deleted file mode 100644 index 7c547ae..0000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/inference/parser/TensorFlowGraphInfModelParser.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * 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.ignite.ml.inference.parser; - -import org.tensorflow.Graph; -import org.tensorflow.Session; - -/** - * Implementation of TensorFlow model parser that accepts serialized graph definition. - * - * @param <I> Type of model input. - * @param <O> Type of model output. - */ -public class TensorFlowGraphInfModelParser<I, O> extends TensorFlowBaseInfModelParser<I, O> { - /** */ - private static final long serialVersionUID = -1872566748640565856L; - - /** {@inheritDoc} */ - @Override public Session parseModel(byte[] mdl) { - Graph graph = new Graph(); - graph.importGraphDef(mdl); - - return new Session(graph); - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/2dc0d9f7/modules/ml/src/main/java/org/apache/ignite/ml/inference/parser/TensorFlowGraphModelParser.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/inference/parser/TensorFlowGraphModelParser.java b/modules/ml/src/main/java/org/apache/ignite/ml/inference/parser/TensorFlowGraphModelParser.java new file mode 100644 index 0000000..dc1d99e --- /dev/null +++ b/modules/ml/src/main/java/org/apache/ignite/ml/inference/parser/TensorFlowGraphModelParser.java @@ -0,0 +1,40 @@ +/* + * 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.ignite.ml.inference.parser; + +import org.tensorflow.Graph; +import org.tensorflow.Session; + +/** + * Implementation of TensorFlow model parser that accepts serialized graph definition. + * + * @param <I> Type of model input. + * @param <O> Type of model output. + */ +public class TensorFlowGraphModelParser<I, O> extends TensorFlowBaseModelParser<I, O> { + /** */ + private static final long serialVersionUID = -1872566748640565856L; + + /** {@inheritDoc} */ + @Override public Session parseModel(byte[] mdl) { + Graph graph = new Graph(); + graph.importGraphDef(mdl); + + return new Session(graph); + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/2dc0d9f7/modules/ml/src/main/java/org/apache/ignite/ml/inference/parser/TensorFlowSavedModelInfModelParser.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/inference/parser/TensorFlowSavedModelInfModelParser.java b/modules/ml/src/main/java/org/apache/ignite/ml/inference/parser/TensorFlowSavedModelInfModelParser.java deleted file mode 100644 index 2ee9f11..0000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/inference/parser/TensorFlowSavedModelInfModelParser.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * 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.ignite.ml.inference.parser; - -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import org.apache.ignite.ml.inference.util.DirectorySerializer; -import org.tensorflow.SavedModelBundle; -import org.tensorflow.Session; - -/** - * Implementation of TensorFlow model parser that accepts serialized directory with "SavedModel" as an input. The - * directory is assumed to be serialized by {@link DirectorySerializer}. - * - * @param <I> Type of model input. - * @param <O> Type of model output. - */ -public class TensorFlowSavedModelInfModelParser<I, O> extends TensorFlowBaseInfModelParser<I, O> { - /** */ - private static final long serialVersionUID = 5638083440240281879L; - - /** Prefix to be used to create temporary directory for TensorFlow model files. */ - private static final String TMP_DIR_PREFIX = "tensorflow_saved_model_"; - - /** Model tags. */ - private final String[] tags; - - /** - * Constructs a new instance of TensorFlow model parser. - * - * @param tags Model tags. - */ - public TensorFlowSavedModelInfModelParser(String... tags) { - this.tags = tags; - } - - /** {@inheritDoc} */ - @Override public Session parseModel(byte[] mdl) { - Path dir = null; - try { - dir = Files.createTempDirectory(TMP_DIR_PREFIX); - DirectorySerializer.deserialize(dir.toAbsolutePath(), mdl); - SavedModelBundle bundle = SavedModelBundle.load(dir.toString(), tags); - return bundle.session(); - } - catch (IOException | ClassNotFoundException e) { - throw new RuntimeException(e); - } - finally { - if (dir != null) - DirectorySerializer.deleteDirectory(dir); - } - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/2dc0d9f7/modules/ml/src/main/java/org/apache/ignite/ml/inference/parser/TensorFlowSavedModelModelParser.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/inference/parser/TensorFlowSavedModelModelParser.java b/modules/ml/src/main/java/org/apache/ignite/ml/inference/parser/TensorFlowSavedModelModelParser.java new file mode 100644 index 0000000..2c0b007 --- /dev/null +++ b/modules/ml/src/main/java/org/apache/ignite/ml/inference/parser/TensorFlowSavedModelModelParser.java @@ -0,0 +1,70 @@ +/* + * 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.ignite.ml.inference.parser; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import org.apache.ignite.ml.inference.util.DirectorySerializer; +import org.tensorflow.SavedModelBundle; +import org.tensorflow.Session; + +/** + * Implementation of TensorFlow model parser that accepts serialized directory with "SavedModel" as an input. The + * directory is assumed to be serialized by {@link DirectorySerializer}. + * + * @param <I> Type of model input. + * @param <O> Type of model output. + */ +public class TensorFlowSavedModelModelParser<I, O> extends TensorFlowBaseModelParser<I, O> { + /** */ + private static final long serialVersionUID = 5638083440240281879L; + + /** Prefix to be used to create temporary directory for TensorFlow model files. */ + private static final String TMP_DIR_PREFIX = "tensorflow_saved_model_"; + + /** Model tags. */ + private final String[] tags; + + /** + * Constructs a new instance of TensorFlow model parser. + * + * @param tags Model tags. + */ + public TensorFlowSavedModelModelParser(String... tags) { + this.tags = tags; + } + + /** {@inheritDoc} */ + @Override public Session parseModel(byte[] mdl) { + Path dir = null; + try { + dir = Files.createTempDirectory(TMP_DIR_PREFIX); + DirectorySerializer.deserialize(dir.toAbsolutePath(), mdl); + SavedModelBundle bundle = SavedModelBundle.load(dir.toString(), tags); + return bundle.session(); + } + catch (IOException | ClassNotFoundException e) { + throw new RuntimeException(e); + } + finally { + if (dir != null) + DirectorySerializer.deleteDirectory(dir); + } + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/2dc0d9f7/modules/ml/src/main/java/org/apache/ignite/ml/inference/reader/FileSystemInfModelReader.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/inference/reader/FileSystemInfModelReader.java b/modules/ml/src/main/java/org/apache/ignite/ml/inference/reader/FileSystemInfModelReader.java deleted file mode 100644 index 0f04270..0000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/inference/reader/FileSystemInfModelReader.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * 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.ignite.ml.inference.reader; - -import java.io.File; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Paths; -import org.apache.ignite.ml.inference.util.DirectorySerializer; - -/** - * Model reader that reads directory or file and serializes it using {@link DirectorySerializer}. - */ -public class FileSystemInfModelReader implements InfModelReader { - /** */ - private static final long serialVersionUID = 7370932792669930039L; - - /** Path to the directory. */ - private final String path; - - /** - * Constructs a new instance of directory model reader. - * - * @param path Path to the directory. - */ - public FileSystemInfModelReader(String path) { - this.path = path; - } - - /** {@inheritDoc} */ - @Override public byte[] read() { - try { - File file = Paths.get(path).toFile(); - if (!file.exists()) - throw new IllegalArgumentException("File or directory does not exist [path=" + path + "]"); - - if (file.isDirectory()) - return DirectorySerializer.serialize(Paths.get(path)); - else - return Files.readAllBytes(Paths.get(path)); - } - catch (IOException e) { - throw new RuntimeException(e); - } - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/2dc0d9f7/modules/ml/src/main/java/org/apache/ignite/ml/inference/reader/FileSystemModelReader.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/inference/reader/FileSystemModelReader.java b/modules/ml/src/main/java/org/apache/ignite/ml/inference/reader/FileSystemModelReader.java new file mode 100644 index 0000000..eb2cba0 --- /dev/null +++ b/modules/ml/src/main/java/org/apache/ignite/ml/inference/reader/FileSystemModelReader.java @@ -0,0 +1,61 @@ +/* + * 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.ignite.ml.inference.reader; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import org.apache.ignite.ml.inference.util.DirectorySerializer; + +/** + * Model reader that reads directory or file and serializes it using {@link DirectorySerializer}. + */ +public class FileSystemModelReader implements ModelReader { + /** */ + private static final long serialVersionUID = 7370932792669930039L; + + /** Path to the directory. */ + private final String path; + + /** + * Constructs a new instance of directory model reader. + * + * @param path Path to the directory. + */ + public FileSystemModelReader(String path) { + this.path = path; + } + + /** {@inheritDoc} */ + @Override public byte[] read() { + try { + File file = Paths.get(path).toFile(); + if (!file.exists()) + throw new IllegalArgumentException("File or directory does not exist [path=" + path + "]"); + + if (file.isDirectory()) + return DirectorySerializer.serialize(Paths.get(path)); + else + return Files.readAllBytes(Paths.get(path)); + } + catch (IOException e) { + throw new RuntimeException(e); + } + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/2dc0d9f7/modules/ml/src/main/java/org/apache/ignite/ml/inference/reader/InMemoryInfModelReader.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/inference/reader/InMemoryInfModelReader.java b/modules/ml/src/main/java/org/apache/ignite/ml/inference/reader/InMemoryInfModelReader.java deleted file mode 100644 index aac5ca8..0000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/inference/reader/InMemoryInfModelReader.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * 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.ignite.ml.inference.reader; - -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.ObjectOutputStream; -import java.io.Serializable; - -/** - * Model reader that reads predefined array of bytes. - */ -public class InMemoryInfModelReader implements InfModelReader { - /** */ - private static final long serialVersionUID = -5518861989758691500L; - - /** Data. */ - private final byte[] data; - - /** - * Constructs a new instance of in-memory model reader that returns specified byte array. - * - * @param data Data. - */ - public InMemoryInfModelReader(byte[] data) { - this.data = data; - } - - /** - * Constructs a new instance of in-memory model reader that returns serialized specified object. - * - * @param obj Data object. - * @param <T> Type of data object. - */ - public <T extends Serializable> InMemoryInfModelReader(T obj) { - try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); - ObjectOutputStream oos = new ObjectOutputStream(baos)) { - oos.writeObject(obj); - oos.flush(); - - this.data = baos.toByteArray(); - } - catch (IOException e) { - throw new RuntimeException(e); - } - } - - /** {@inheritDoc} */ - @Override public byte[] read() { - return data; - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ignite/blob/2dc0d9f7/modules/ml/src/main/java/org/apache/ignite/ml/inference/reader/InMemoryModelReader.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/inference/reader/InMemoryModelReader.java b/modules/ml/src/main/java/org/apache/ignite/ml/inference/reader/InMemoryModelReader.java new file mode 100644 index 0000000..d5ebe8e --- /dev/null +++ b/modules/ml/src/main/java/org/apache/ignite/ml/inference/reader/InMemoryModelReader.java @@ -0,0 +1,67 @@ +/* + * 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.ignite.ml.inference.reader; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.ObjectOutputStream; +import java.io.Serializable; + +/** + * Model reader that reads predefined array of bytes. + */ +public class InMemoryModelReader implements ModelReader { + /** */ + private static final long serialVersionUID = -5518861989758691500L; + + /** Data. */ + private final byte[] data; + + /** + * Constructs a new instance of in-memory model reader that returns specified byte array. + * + * @param data Data. + */ + public InMemoryModelReader(byte[] data) { + this.data = data; + } + + /** + * Constructs a new instance of in-memory model reader that returns serialized specified object. + * + * @param obj Data object. + * @param <T> Type of data object. + */ + public <T extends Serializable> InMemoryModelReader(T obj) { + try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ObjectOutputStream oos = new ObjectOutputStream(baos)) { + oos.writeObject(obj); + oos.flush(); + + this.data = baos.toByteArray(); + } + catch (IOException e) { + throw new RuntimeException(e); + } + } + + /** {@inheritDoc} */ + @Override public byte[] read() { + return data; + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ignite/blob/2dc0d9f7/modules/ml/src/main/java/org/apache/ignite/ml/inference/reader/InfModelReader.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/inference/reader/InfModelReader.java b/modules/ml/src/main/java/org/apache/ignite/ml/inference/reader/InfModelReader.java deleted file mode 100644 index 779a1ee..0000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/inference/reader/InfModelReader.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * 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.ignite.ml.inference.reader; - -import java.io.Serializable; - -/** - * Model reader that reads model from external or internal storage and returns it in serialized form as byte array. - */ -@FunctionalInterface -public interface InfModelReader extends Serializable { - /** - * Rads model and returns it in serialized form as byte array. - * - * @return Inference model in serialized form as byte array. - */ - public byte[] read(); -} http://git-wip-us.apache.org/repos/asf/ignite/blob/2dc0d9f7/modules/ml/src/main/java/org/apache/ignite/ml/inference/reader/ModelReader.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/inference/reader/ModelReader.java b/modules/ml/src/main/java/org/apache/ignite/ml/inference/reader/ModelReader.java new file mode 100644 index 0000000..e26d614 --- /dev/null +++ b/modules/ml/src/main/java/org/apache/ignite/ml/inference/reader/ModelReader.java @@ -0,0 +1,33 @@ +/* + * 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.ignite.ml.inference.reader; + +import java.io.Serializable; + +/** + * Model reader that reads model from external or internal storage and returns it in serialized form as byte array. + */ +@FunctionalInterface +public interface ModelReader extends Serializable { + /** + * Rads model and returns it in serialized form as byte array. + * + * @return Inference model in serialized form as byte array. + */ + public byte[] read(); +} http://git-wip-us.apache.org/repos/asf/ignite/blob/2dc0d9f7/modules/ml/src/main/java/org/apache/ignite/ml/inference/reader/ModelStorageInfModelReader.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/inference/reader/ModelStorageInfModelReader.java b/modules/ml/src/main/java/org/apache/ignite/ml/inference/reader/ModelStorageInfModelReader.java deleted file mode 100644 index 2a4d4b4..0000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/inference/reader/ModelStorageInfModelReader.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * 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.ignite.ml.inference.reader; - -import org.apache.ignite.Ignition; -import org.apache.ignite.ml.inference.storage.model.ModelStorage; -import org.apache.ignite.ml.inference.storage.model.ModelStorageFactory; -import org.apache.ignite.ml.inference.util.DirectorySerializer; -import org.apache.ignite.ml.math.functions.IgniteSupplier; - -/** - * Model reader that reads directory or file from model storage and serializes it using {@link DirectorySerializer}. - */ -public class ModelStorageInfModelReader implements InfModelReader { - /** */ - private static final long serialVersionUID = -5878564742783562872L; - - /** Path to the directory or file. */ - private final String path; - - /** Model storage supplier. */ - private final IgniteSupplier<ModelStorage> mdlStorageSupplier; - - /** - * Constructs a new instance of model storage inference model builder. - * - * @param path Path to the directory or file. - */ - public ModelStorageInfModelReader(String path, IgniteSupplier<ModelStorage> mdlStorageSupplier) { - this.path = path; - this.mdlStorageSupplier = mdlStorageSupplier; - } - - /** - * Constructs a new instance of model storage inference model builder. - * - * @param path Path to the directory or file. - */ - public ModelStorageInfModelReader(String path) { - this(path, () -> new ModelStorageFactory().getModelStorage(Ignition.ignite())); - } - - /** {@inheritDoc} */ - @Override public byte[] read() { - ModelStorage storage = mdlStorageSupplier.get(); - - return storage.getFile(path); - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/2dc0d9f7/modules/ml/src/main/java/org/apache/ignite/ml/inference/reader/ModelStorageModelReader.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/inference/reader/ModelStorageModelReader.java b/modules/ml/src/main/java/org/apache/ignite/ml/inference/reader/ModelStorageModelReader.java new file mode 100644 index 0000000..9316ede --- /dev/null +++ b/modules/ml/src/main/java/org/apache/ignite/ml/inference/reader/ModelStorageModelReader.java @@ -0,0 +1,64 @@ +/* + * 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.ignite.ml.inference.reader; + +import org.apache.ignite.Ignition; +import org.apache.ignite.ml.inference.storage.model.ModelStorage; +import org.apache.ignite.ml.inference.storage.model.ModelStorageFactory; +import org.apache.ignite.ml.inference.util.DirectorySerializer; +import org.apache.ignite.ml.math.functions.IgniteSupplier; + +/** + * Model reader that reads directory or file from model storage and serializes it using {@link DirectorySerializer}. + */ +public class ModelStorageModelReader implements ModelReader { + /** */ + private static final long serialVersionUID = -5878564742783562872L; + + /** Path to the directory or file. */ + private final String path; + + /** Model storage supplier. */ + private final IgniteSupplier<ModelStorage> mdlStorageSupplier; + + /** + * Constructs a new instance of model storage inference model builder. + * + * @param path Path to the directory or file. + */ + public ModelStorageModelReader(String path, IgniteSupplier<ModelStorage> mdlStorageSupplier) { + this.path = path; + this.mdlStorageSupplier = mdlStorageSupplier; + } + + /** + * Constructs a new instance of model storage inference model builder. + * + * @param path Path to the directory or file. + */ + public ModelStorageModelReader(String path) { + this(path, () -> new ModelStorageFactory().getModelStorage(Ignition.ignite())); + } + + /** {@inheritDoc} */ + @Override public byte[] read() { + ModelStorage storage = mdlStorageSupplier.get(); + + return storage.getFile(path); + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/2dc0d9f7/modules/ml/src/main/java/org/apache/ignite/ml/knn/NNClassificationModel.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/knn/NNClassificationModel.java b/modules/ml/src/main/java/org/apache/ignite/ml/knn/NNClassificationModel.java index d435f91..ba0d885 100644 --- a/modules/ml/src/main/java/org/apache/ignite/ml/knn/NNClassificationModel.java +++ b/modules/ml/src/main/java/org/apache/ignite/ml/knn/NNClassificationModel.java @@ -26,7 +26,7 @@ import java.util.Set; import java.util.TreeMap; import org.apache.ignite.ml.Exportable; import org.apache.ignite.ml.Exporter; -import org.apache.ignite.ml.Model; +import org.apache.ignite.ml.IgniteModel; import org.apache.ignite.ml.knn.classification.KNNModelFormat; import org.apache.ignite.ml.knn.classification.NNStrategy; import org.apache.ignite.ml.math.distances.DistanceMeasure; @@ -41,7 +41,7 @@ import org.jetbrains.annotations.NotNull; * Common methods and fields for all kNN and aNN models * to predict label based on neighbours' labels. */ -public abstract class NNClassificationModel implements Model<Vector, Double>, Exportable<KNNModelFormat> { +public abstract class NNClassificationModel implements IgniteModel<Vector, Double>, Exportable<KNNModelFormat> { /** Amount of nearest neighbors. */ protected int k = 5; http://git-wip-us.apache.org/repos/asf/ignite/blob/2dc0d9f7/modules/ml/src/main/java/org/apache/ignite/ml/knn/ann/ANNClassificationModel.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/knn/ann/ANNClassificationModel.java b/modules/ml/src/main/java/org/apache/ignite/ml/knn/ann/ANNClassificationModel.java index 6ef3990..4e42ab8 100644 --- a/modules/ml/src/main/java/org/apache/ignite/ml/knn/ann/ANNClassificationModel.java +++ b/modules/ml/src/main/java/org/apache/ignite/ml/knn/ann/ANNClassificationModel.java @@ -69,7 +69,7 @@ public class ANNClassificationModel extends NNClassificationModel { } /** {@inheritDoc} */ - @Override public Double apply(Vector v) { + @Override public Double predict(Vector v) { List<LabeledVector> neighbors = findKNearestNeighbors(v); return classify(neighbors, v, stgy); } http://git-wip-us.apache.org/repos/asf/ignite/blob/2dc0d9f7/modules/ml/src/main/java/org/apache/ignite/ml/knn/classification/KNNClassificationModel.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/knn/classification/KNNClassificationModel.java b/modules/ml/src/main/java/org/apache/ignite/ml/knn/classification/KNNClassificationModel.java index 3de73bd..f313e8a 100644 --- a/modules/ml/src/main/java/org/apache/ignite/ml/knn/classification/KNNClassificationModel.java +++ b/modules/ml/src/main/java/org/apache/ignite/ml/knn/classification/KNNClassificationModel.java @@ -58,7 +58,7 @@ public class KNNClassificationModel extends NNClassificationModel implements Exp } /** {@inheritDoc} */ - @Override public Double apply(Vector v) { + @Override public Double predict(Vector v) { if (!datasets.isEmpty()) { List<LabeledVector> neighbors = findKNearestNeighbors(v); http://git-wip-us.apache.org/repos/asf/ignite/blob/2dc0d9f7/modules/ml/src/main/java/org/apache/ignite/ml/knn/regression/KNNRegressionModel.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/knn/regression/KNNRegressionModel.java b/modules/ml/src/main/java/org/apache/ignite/ml/knn/regression/KNNRegressionModel.java index f854177..84d86d6 100644 --- a/modules/ml/src/main/java/org/apache/ignite/ml/knn/regression/KNNRegressionModel.java +++ b/modules/ml/src/main/java/org/apache/ignite/ml/knn/regression/KNNRegressionModel.java @@ -50,7 +50,7 @@ public class KNNRegressionModel extends KNNClassificationModel { } /** {@inheritDoc} */ - @Override public Double apply(Vector v) { + @Override public Double predict(Vector v) { List<LabeledVector> neighbors = findKNearestNeighbors(v); return predictYBasedOn(neighbors, v); http://git-wip-us.apache.org/repos/asf/ignite/blob/2dc0d9f7/modules/ml/src/main/java/org/apache/ignite/ml/multiclass/MultiClassModel.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/multiclass/MultiClassModel.java b/modules/ml/src/main/java/org/apache/ignite/ml/multiclass/MultiClassModel.java index 8520aa9..fa73bcb 100644 --- a/modules/ml/src/main/java/org/apache/ignite/ml/multiclass/MultiClassModel.java +++ b/modules/ml/src/main/java/org/apache/ignite/ml/multiclass/MultiClassModel.java @@ -25,11 +25,11 @@ import java.util.Optional; import java.util.TreeMap; import org.apache.ignite.ml.Exportable; import org.apache.ignite.ml.Exporter; -import org.apache.ignite.ml.Model; +import org.apache.ignite.ml.IgniteModel; import org.apache.ignite.ml.math.primitives.vector.Vector; /** Base class for multi-classification model for set of classifiers. */ -public class MultiClassModel<M extends Model<Vector, Double>> implements Model<Vector, Double>, Exportable<MultiClassModel>, Serializable { +public class MultiClassModel<M extends IgniteModel<Vector, Double>> implements IgniteModel<Vector, Double>, Exportable<MultiClassModel>, Serializable { /** */ private static final long serialVersionUID = -114986533359917L; @@ -60,10 +60,10 @@ public class MultiClassModel<M extends Model<Vector, Double>> implements Model<V } /** {@inheritDoc} */ - @Override public Double apply(Vector input) { + @Override public Double predict(Vector input) { TreeMap<Double, Double> maxMargins = new TreeMap<>(); - models.forEach((k, v) -> maxMargins.put(v.apply(input), k)); + models.forEach((k, v) -> maxMargins.put(v.predict(input), k)); // returns value the most closest to 1 return maxMargins.lastEntry().getValue(); http://git-wip-us.apache.org/repos/asf/ignite/blob/2dc0d9f7/modules/ml/src/main/java/org/apache/ignite/ml/multiclass/OneVsRestTrainer.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/multiclass/OneVsRestTrainer.java b/modules/ml/src/main/java/org/apache/ignite/ml/multiclass/OneVsRestTrainer.java index 0ddad53..4eca27f 100644 --- a/modules/ml/src/main/java/org/apache/ignite/ml/multiclass/OneVsRestTrainer.java +++ b/modules/ml/src/main/java/org/apache/ignite/ml/multiclass/OneVsRestTrainer.java @@ -25,7 +25,7 @@ import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; import java.util.stream.Stream; -import org.apache.ignite.ml.Model; +import org.apache.ignite.ml.IgniteModel; import org.apache.ignite.ml.dataset.Dataset; import org.apache.ignite.ml.dataset.DatasetBuilder; import org.apache.ignite.ml.dataset.PartitionDataBuilder; @@ -42,7 +42,7 @@ import org.apache.ignite.ml.trainers.SingleLabelDatasetTrainer; * NOTE: The current implementation suffers from unbalanced training over the dataset due to unweighted approach * during the process of reassign labels from all range of labels to 0,1. */ -public class OneVsRestTrainer<M extends Model<Vector, Double>> +public class OneVsRestTrainer<M extends IgniteModel<Vector, Double>> extends SingleLabelDatasetTrainer<MultiClassModel<M>> { /** The common binary classifier with all hyper-parameters to spread them for all separate trainings . */ private SingleLabelDatasetTrainer<M> classifier; http://git-wip-us.apache.org/repos/asf/ignite/blob/2dc0d9f7/modules/ml/src/main/java/org/apache/ignite/ml/naivebayes/discrete/DiscreteNaiveBayesModel.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/naivebayes/discrete/DiscreteNaiveBayesModel.java b/modules/ml/src/main/java/org/apache/ignite/ml/naivebayes/discrete/DiscreteNaiveBayesModel.java index 7ab2957..3935232 100644 --- a/modules/ml/src/main/java/org/apache/ignite/ml/naivebayes/discrete/DiscreteNaiveBayesModel.java +++ b/modules/ml/src/main/java/org/apache/ignite/ml/naivebayes/discrete/DiscreteNaiveBayesModel.java @@ -20,7 +20,7 @@ package org.apache.ignite.ml.naivebayes.discrete; import java.io.Serializable; import org.apache.ignite.ml.Exportable; import org.apache.ignite.ml.Exporter; -import org.apache.ignite.ml.Model; +import org.apache.ignite.ml.IgniteModel; import org.apache.ignite.ml.math.primitives.vector.Vector; /** @@ -28,7 +28,7 @@ import org.apache.ignite.ml.math.primitives.vector.Vector; * {@code p(C_k,y) =x_1*p_k1^x *...*x_i*p_ki^x_i}. Where {@code x_i} is a discrete feature, {@code p_ki} is a prior * probability probability of class {@code p(x|C_k)}. Returns the number of the most possible class. */ -public class DiscreteNaiveBayesModel implements Model<Vector, Double>, Exportable<DiscreteNaiveBayesModel>, Serializable { +public class DiscreteNaiveBayesModel implements IgniteModel<Vector, Double>, Exportable<DiscreteNaiveBayesModel>, Serializable { /** */ private static final long serialVersionUID = -127386523291350345L; /** @@ -74,7 +74,7 @@ public class DiscreteNaiveBayesModel implements Model<Vector, Double>, Exportabl * @param vector features vector. * @return a label with max probability. */ - @Override public Double apply(Vector vector) { + @Override public Double predict(Vector vector) { double maxProbapilityPower = -Double.MAX_VALUE; int maxLabelIndex = -1; http://git-wip-us.apache.org/repos/asf/ignite/blob/2dc0d9f7/modules/ml/src/main/java/org/apache/ignite/ml/naivebayes/gaussian/GaussianNaiveBayesModel.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/naivebayes/gaussian/GaussianNaiveBayesModel.java b/modules/ml/src/main/java/org/apache/ignite/ml/naivebayes/gaussian/GaussianNaiveBayesModel.java index 66c97b3..92617d7 100644 --- a/modules/ml/src/main/java/org/apache/ignite/ml/naivebayes/gaussian/GaussianNaiveBayesModel.java +++ b/modules/ml/src/main/java/org/apache/ignite/ml/naivebayes/gaussian/GaussianNaiveBayesModel.java @@ -20,14 +20,14 @@ package org.apache.ignite.ml.naivebayes.gaussian; import java.io.Serializable; import org.apache.ignite.ml.Exportable; import org.apache.ignite.ml.Exporter; -import org.apache.ignite.ml.Model; +import org.apache.ignite.ml.IgniteModel; import org.apache.ignite.ml.math.primitives.vector.Vector; /** * Simple naive Bayes model which predicts result value {@code y} belongs to a class {@code C_k, k in [0..K]} as {@code * p(C_k,y) = p(C_k)*p(y_1,C_k) *...*p(y_n,C_k) / p(y)}. Return the number of the most possible class. */ -public class GaussianNaiveBayesModel implements Model<Vector, Double>, Exportable<GaussianNaiveBayesModel>, Serializable { +public class GaussianNaiveBayesModel implements IgniteModel<Vector, Double>, Exportable<GaussianNaiveBayesModel>, Serializable { /** */ private static final long serialVersionUID = -127386523291350345L; /** Means of features for all classes. kth row contains means for labels[k] class. */ @@ -63,7 +63,7 @@ public class GaussianNaiveBayesModel implements Model<Vector, Double>, Exportabl } /** Returns a number of class to which the input belongs. */ - @Override public Double apply(Vector vector) { + @Override public Double predict(Vector vector) { int k = classProbabilities.length; double maxProbability = .0; http://git-wip-us.apache.org/repos/asf/ignite/blob/2dc0d9f7/modules/ml/src/main/java/org/apache/ignite/ml/nn/MultilayerPerceptron.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/nn/MultilayerPerceptron.java b/modules/ml/src/main/java/org/apache/ignite/ml/nn/MultilayerPerceptron.java index b469603..df807b0 100644 --- a/modules/ml/src/main/java/org/apache/ignite/ml/nn/MultilayerPerceptron.java +++ b/modules/ml/src/main/java/org/apache/ignite/ml/nn/MultilayerPerceptron.java @@ -23,7 +23,7 @@ import java.util.LinkedList; import java.util.List; import java.util.Random; import org.apache.ignite.lang.IgniteBiTuple; -import org.apache.ignite.ml.Model; +import org.apache.ignite.ml.IgniteModel; import org.apache.ignite.ml.math.Tracer; import org.apache.ignite.ml.math.functions.IgniteDifferentiableDoubleToDoubleFunction; import org.apache.ignite.ml.math.functions.IgniteDifferentiableVectorToDoubleFunction; @@ -43,7 +43,7 @@ import static org.apache.ignite.ml.math.util.MatrixUtil.elementWiseTimes; /** * Class encapsulating logic of multilayer perceptron. */ -public class MultilayerPerceptron implements Model<Matrix, Matrix>, SmoothParametrized<MultilayerPerceptron>, +public class MultilayerPerceptron implements IgniteModel<Matrix, Matrix>, SmoothParametrized<MultilayerPerceptron>, Serializable { /** * This MLP architecture. @@ -176,7 +176,7 @@ public class MultilayerPerceptron implements Model<Matrix, Matrix>, SmoothParame * @param val Matrix containing objects. * @return Matrix with predicted vectors. */ - @Override public Matrix apply(Matrix val) { + @Override public Matrix predict(Matrix val) { MLPState state = new MLPState(null); forwardPass(val.transpose(), state, false); return state.activatorsOutput.get(state.activatorsOutput.size() - 1).transpose(); http://git-wip-us.apache.org/repos/asf/ignite/blob/2dc0d9f7/modules/ml/src/main/java/org/apache/ignite/ml/optimization/SmoothParametrized.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/optimization/SmoothParametrized.java b/modules/ml/src/main/java/org/apache/ignite/ml/optimization/SmoothParametrized.java index 5eb8722..c1cf037 100644 --- a/modules/ml/src/main/java/org/apache/ignite/ml/optimization/SmoothParametrized.java +++ b/modules/ml/src/main/java/org/apache/ignite/ml/optimization/SmoothParametrized.java @@ -17,7 +17,7 @@ package org.apache.ignite.ml.optimization; -import org.apache.ignite.ml.Model; +import org.apache.ignite.ml.IgniteModel; import org.apache.ignite.ml.math.functions.IgniteDifferentiableVectorToDoubleFunction; import org.apache.ignite.ml.math.functions.IgniteFunction; import org.apache.ignite.ml.math.primitives.matrix.Matrix; @@ -26,7 +26,7 @@ import org.apache.ignite.ml.math.primitives.vector.Vector; /** * Interface for models which are smooth functions of their parameters. */ -public interface SmoothParametrized<M extends Parametrized<M>> extends Parametrized<M>, Model<Matrix, Matrix> { +public interface SmoothParametrized<M extends Parametrized<M>> extends Parametrized<M>, IgniteModel<Matrix, Matrix> { /** * Compose function in the following way: feed output of this model as input to second argument to loss function. * After that we have a function g of three arguments: input, ground truth, parameters. http://git-wip-us.apache.org/repos/asf/ignite/blob/2dc0d9f7/modules/ml/src/main/java/org/apache/ignite/ml/pipeline/Pipeline.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/pipeline/Pipeline.java b/modules/ml/src/main/java/org/apache/ignite/ml/pipeline/Pipeline.java index 1aeac6b..e6e4227 100644 --- a/modules/ml/src/main/java/org/apache/ignite/ml/pipeline/Pipeline.java +++ b/modules/ml/src/main/java/org/apache/ignite/ml/pipeline/Pipeline.java @@ -22,7 +22,7 @@ import java.util.List; import java.util.Map; import org.apache.ignite.Ignite; import org.apache.ignite.IgniteCache; -import org.apache.ignite.ml.Model; +import org.apache.ignite.ml.IgniteModel; import org.apache.ignite.ml.dataset.DatasetBuilder; import org.apache.ignite.ml.dataset.impl.cache.CacheBasedDatasetBuilder; import org.apache.ignite.ml.dataset.impl.local.LocalDatasetBuilder; @@ -151,7 +151,7 @@ public class Pipeline<K, V, R> { ); }); - Model<Vector, Double> internalMdl = finalStage + IgniteModel<Vector, Double> internalMdl = finalStage .fit( datasetBuilder, finalFeatureExtractor, http://git-wip-us.apache.org/repos/asf/ignite/blob/2dc0d9f7/modules/ml/src/main/java/org/apache/ignite/ml/pipeline/PipelineMdl.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/pipeline/PipelineMdl.java b/modules/ml/src/main/java/org/apache/ignite/ml/pipeline/PipelineMdl.java index edd70eb..ccd1fb1 100644 --- a/modules/ml/src/main/java/org/apache/ignite/ml/pipeline/PipelineMdl.java +++ b/modules/ml/src/main/java/org/apache/ignite/ml/pipeline/PipelineMdl.java @@ -17,7 +17,7 @@ package org.apache.ignite.ml.pipeline; -import org.apache.ignite.ml.Model; +import org.apache.ignite.ml.IgniteModel; import org.apache.ignite.ml.math.functions.IgniteBiFunction; import org.apache.ignite.ml.math.primitives.vector.Vector; @@ -27,9 +27,9 @@ import org.apache.ignite.ml.math.primitives.vector.Vector; * @param <K> Type of a key in {@code upstream} data. * @param <V> Type of a value in {@code upstream} data. */ -public class PipelineMdl<K, V> implements Model<Vector, Double> { +public class PipelineMdl<K, V> implements IgniteModel<Vector, Double> { /** Internal model produced by {@link Pipeline}. */ - private Model<Vector, Double> internalMdl; + private IgniteModel<Vector, Double> internalMdl; /** Feature extractor. */ private IgniteBiFunction<K, V, Vector> featureExtractor; @@ -38,8 +38,8 @@ public class PipelineMdl<K, V> implements Model<Vector, Double> { private IgniteBiFunction<K, V, Double> lbExtractor; /** */ - @Override public Double apply(Vector vector) { - return internalMdl.apply(vector); + @Override public Double predict(Vector vector) { + return internalMdl.predict(vector); } /** */ @@ -53,12 +53,12 @@ public class PipelineMdl<K, V> implements Model<Vector, Double> { } /** */ - public Model<Vector, Double> getInternalMdl() { + public IgniteModel<Vector, Double> getInternalMdl() { return internalMdl; } /** */ - public PipelineMdl<K, V> withInternalMdl(Model<Vector, Double> internalMdl) { + public PipelineMdl<K, V> withInternalMdl(IgniteModel<Vector, Double> internalMdl) { this.internalMdl = internalMdl; return this; } http://git-wip-us.apache.org/repos/asf/ignite/blob/2dc0d9f7/modules/ml/src/main/java/org/apache/ignite/ml/regressions/linear/LinearRegressionModel.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/regressions/linear/LinearRegressionModel.java b/modules/ml/src/main/java/org/apache/ignite/ml/regressions/linear/LinearRegressionModel.java index 4d9d28e..b298524 100644 --- a/modules/ml/src/main/java/org/apache/ignite/ml/regressions/linear/LinearRegressionModel.java +++ b/modules/ml/src/main/java/org/apache/ignite/ml/regressions/linear/LinearRegressionModel.java @@ -21,14 +21,14 @@ import java.io.Serializable; import java.util.Objects; import org.apache.ignite.ml.Exportable; import org.apache.ignite.ml.Exporter; -import org.apache.ignite.ml.Model; +import org.apache.ignite.ml.IgniteModel; import org.apache.ignite.ml.math.primitives.vector.Vector; /** * Simple linear regression model which predicts result value Y as a linear combination of input variables: * Y = weights * X + intercept. */ -public class LinearRegressionModel implements Model<Vector, Double>, Exportable<LinearRegressionModel>, Serializable { +public class LinearRegressionModel implements IgniteModel<Vector, Double>, Exportable<LinearRegressionModel>, Serializable { /** */ private static final long serialVersionUID = -105984600091550226L; @@ -55,7 +55,7 @@ public class LinearRegressionModel implements Model<Vector, Double>, Exportable< } /** {@inheritDoc} */ - @Override public Double apply(Vector input) { + @Override public Double predict(Vector input) { return input.dot(weights) + intercept; } http://git-wip-us.apache.org/repos/asf/ignite/blob/2dc0d9f7/modules/ml/src/main/java/org/apache/ignite/ml/regressions/logistic/LogisticRegressionModel.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/regressions/logistic/LogisticRegressionModel.java b/modules/ml/src/main/java/org/apache/ignite/ml/regressions/logistic/LogisticRegressionModel.java index 5cc44f8..f041fbf 100644 --- a/modules/ml/src/main/java/org/apache/ignite/ml/regressions/logistic/LogisticRegressionModel.java +++ b/modules/ml/src/main/java/org/apache/ignite/ml/regressions/logistic/LogisticRegressionModel.java @@ -21,13 +21,13 @@ import java.io.Serializable; import java.util.Objects; import org.apache.ignite.ml.Exportable; import org.apache.ignite.ml.Exporter; -import org.apache.ignite.ml.Model; +import org.apache.ignite.ml.IgniteModel; import org.apache.ignite.ml.math.primitives.vector.Vector; /** * Logistic regression (logit model) is a generalized linear model used for binomial regression. */ -public class LogisticRegressionModel implements Model<Vector, Double>, Exportable<LogisticRegressionModel>, Serializable { +public class LogisticRegressionModel implements IgniteModel<Vector, Double>, Exportable<LogisticRegressionModel>, Serializable { /** */ private static final long serialVersionUID = -133984600091550776L; @@ -130,7 +130,7 @@ public class LogisticRegressionModel implements Model<Vector, Double>, Exportabl } /** {@inheritDoc} */ - @Override public Double apply(Vector input) { + @Override public Double predict(Vector input) { final double res = sigmoid(input.dot(weights) + intercept); http://git-wip-us.apache.org/repos/asf/ignite/blob/2dc0d9f7/modules/ml/src/main/java/org/apache/ignite/ml/selection/cv/CrossValidation.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/selection/cv/CrossValidation.java b/modules/ml/src/main/java/org/apache/ignite/ml/selection/cv/CrossValidation.java index ef4f30f..07dd57e 100644 --- a/modules/ml/src/main/java/org/apache/ignite/ml/selection/cv/CrossValidation.java +++ b/modules/ml/src/main/java/org/apache/ignite/ml/selection/cv/CrossValidation.java @@ -28,7 +28,7 @@ import java.util.function.Function; import org.apache.ignite.Ignite; import org.apache.ignite.IgniteCache; import org.apache.ignite.lang.IgniteBiPredicate; -import org.apache.ignite.ml.Model; +import org.apache.ignite.ml.IgniteModel; import org.apache.ignite.ml.dataset.DatasetBuilder; import org.apache.ignite.ml.dataset.impl.cache.CacheBasedDatasetBuilder; import org.apache.ignite.ml.dataset.impl.local.LocalDatasetBuilder; @@ -59,7 +59,7 @@ import org.apache.ignite.ml.trainers.DatasetTrainer; * @param <K> Type of a key in {@code upstream} data. * @param <V> Type of a value in {@code upstream} data. */ -public class CrossValidation<M extends Model<Vector, L>, L, K, V> { +public class CrossValidation<M extends IgniteModel<Vector, L>, L, K, V> { /** * Computes cross-validated metrics. * http://git-wip-us.apache.org/repos/asf/ignite/blob/2dc0d9f7/modules/ml/src/main/java/org/apache/ignite/ml/selection/scoring/cursor/CacheBasedLabelPairCursor.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/selection/scoring/cursor/CacheBasedLabelPairCursor.java b/modules/ml/src/main/java/org/apache/ignite/ml/selection/scoring/cursor/CacheBasedLabelPairCursor.java index 06be96b..9dccd59 100644 --- a/modules/ml/src/main/java/org/apache/ignite/ml/selection/scoring/cursor/CacheBasedLabelPairCursor.java +++ b/modules/ml/src/main/java/org/apache/ignite/ml/selection/scoring/cursor/CacheBasedLabelPairCursor.java @@ -23,7 +23,7 @@ import org.apache.ignite.IgniteCache; import org.apache.ignite.cache.query.QueryCursor; import org.apache.ignite.cache.query.ScanQuery; import org.apache.ignite.lang.IgniteBiPredicate; -import org.apache.ignite.ml.Model; +import org.apache.ignite.ml.IgniteModel; import org.apache.ignite.ml.math.functions.IgniteBiFunction; import org.apache.ignite.ml.math.primitives.vector.Vector; import org.apache.ignite.ml.selection.scoring.LabelPair; @@ -47,7 +47,7 @@ public class CacheBasedLabelPairCursor<L, K, V> implements LabelPairCursor<L> { private final IgniteBiFunction<K, V, L> lbExtractor; /** Model for inference. */ - private final Model<Vector, L> mdl; + private final IgniteModel<Vector, L> mdl; /** * Constructs a new instance of cache based truth with prediction cursor. @@ -60,7 +60,7 @@ public class CacheBasedLabelPairCursor<L, K, V> implements LabelPairCursor<L> { */ public CacheBasedLabelPairCursor(IgniteCache<K, V> upstreamCache, IgniteBiPredicate<K, V> filter, IgniteBiFunction<K, V, Vector> featureExtractor, IgniteBiFunction<K, V, L> lbExtractor, - Model<Vector, L> mdl) { + IgniteModel<Vector, L> mdl) { this.cursor = query(upstreamCache, filter); this.featureExtractor = featureExtractor; this.lbExtractor = lbExtractor; @@ -77,7 +77,7 @@ public class CacheBasedLabelPairCursor<L, K, V> implements LabelPairCursor<L> { */ public CacheBasedLabelPairCursor(IgniteCache<K, V> upstreamCache, IgniteBiFunction<K, V, Vector> featureExtractor, IgniteBiFunction<K, V, L> lbExtractor, - Model<Vector, L> mdl) { + IgniteModel<Vector, L> mdl) { this.cursor = query(upstreamCache); this.featureExtractor = featureExtractor; this.lbExtractor = lbExtractor; @@ -150,7 +150,7 @@ public class CacheBasedLabelPairCursor<L, K, V> implements LabelPairCursor<L> { Vector features = featureExtractor.apply(entry.getKey(), entry.getValue()); L lb = lbExtractor.apply(entry.getKey(), entry.getValue()); - return new LabelPair<>(lb, mdl.apply(features)); + return new LabelPair<>(lb, mdl.predict(features)); } } } http://git-wip-us.apache.org/repos/asf/ignite/blob/2dc0d9f7/modules/ml/src/main/java/org/apache/ignite/ml/selection/scoring/cursor/LocalLabelPairCursor.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/selection/scoring/cursor/LocalLabelPairCursor.java b/modules/ml/src/main/java/org/apache/ignite/ml/selection/scoring/cursor/LocalLabelPairCursor.java index d8c2240..9679ae5 100644 --- a/modules/ml/src/main/java/org/apache/ignite/ml/selection/scoring/cursor/LocalLabelPairCursor.java +++ b/modules/ml/src/main/java/org/apache/ignite/ml/selection/scoring/cursor/LocalLabelPairCursor.java @@ -21,7 +21,7 @@ import java.util.Iterator; import java.util.Map; import java.util.NoSuchElementException; import org.apache.ignite.lang.IgniteBiPredicate; -import org.apache.ignite.ml.Model; +import org.apache.ignite.ml.IgniteModel; import org.apache.ignite.ml.math.functions.IgniteBiFunction; import org.apache.ignite.ml.math.primitives.vector.Vector; import org.apache.ignite.ml.selection.scoring.LabelPair; @@ -48,7 +48,7 @@ public class LocalLabelPairCursor<L, K, V, T> implements LabelPairCursor<L> { private final IgniteBiFunction<K, V, L> lbExtractor; /** Model for inference. */ - private final Model<Vector, L> mdl; + private final IgniteModel<Vector, L> mdl; /** * Constructs a new instance of local truth with prediction cursor. @@ -61,7 +61,7 @@ public class LocalLabelPairCursor<L, K, V, T> implements LabelPairCursor<L> { */ public LocalLabelPairCursor(Map<K, V> upstreamMap, IgniteBiPredicate<K, V> filter, IgniteBiFunction<K, V, Vector> featureExtractor, IgniteBiFunction<K, V, L> lbExtractor, - Model<Vector, L> mdl) { + IgniteModel<Vector, L> mdl) { this.upstreamMap = upstreamMap; this.filter = filter; this.featureExtractor = featureExtractor; @@ -125,7 +125,7 @@ public class LocalLabelPairCursor<L, K, V, T> implements LabelPairCursor<L> { nextEntry = null; - return new LabelPair<>(lb, mdl.apply(features)); + return new LabelPair<>(lb, mdl.predict(features)); } /** http://git-wip-us.apache.org/repos/asf/ignite/blob/2dc0d9f7/modules/ml/src/main/java/org/apache/ignite/ml/selection/scoring/evaluator/BinaryClassificationEvaluator.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/selection/scoring/evaluator/BinaryClassificationEvaluator.java b/modules/ml/src/main/java/org/apache/ignite/ml/selection/scoring/evaluator/BinaryClassificationEvaluator.java index 5cbe10f..7b9698f 100644 --- a/modules/ml/src/main/java/org/apache/ignite/ml/selection/scoring/evaluator/BinaryClassificationEvaluator.java +++ b/modules/ml/src/main/java/org/apache/ignite/ml/selection/scoring/evaluator/BinaryClassificationEvaluator.java @@ -20,7 +20,7 @@ package org.apache.ignite.ml.selection.scoring.evaluator; import java.util.Map; import org.apache.ignite.IgniteCache; import org.apache.ignite.lang.IgniteBiPredicate; -import org.apache.ignite.ml.Model; +import org.apache.ignite.ml.IgniteModel; import org.apache.ignite.ml.math.functions.IgniteBiFunction; import org.apache.ignite.ml.math.primitives.vector.Vector; import org.apache.ignite.ml.selection.scoring.cursor.CacheBasedLabelPairCursor; @@ -47,7 +47,7 @@ public class BinaryClassificationEvaluator { * @return Computed metric. */ public static <L, K, V> double evaluate(IgniteCache<K, V> dataCache, - Model<Vector, L> mdl, + IgniteModel<Vector, L> mdl, IgniteBiFunction<K, V, Vector> featureExtractor, IgniteBiFunction<K, V, L> lbExtractor, Metric<L> metric) { @@ -67,7 +67,7 @@ public class BinaryClassificationEvaluator { * @return Computed metric. */ public static <L, K, V> double evaluate(Map<K, V> dataCache, - Model<Vector, L> mdl, + IgniteModel<Vector, L> mdl, IgniteBiFunction<K, V, Vector> featureExtractor, IgniteBiFunction<K, V, L> lbExtractor, Metric<L> metric) { @@ -89,7 +89,7 @@ public class BinaryClassificationEvaluator { * @return Computed metric. */ public static <L, K, V> double evaluate(IgniteCache<K, V> dataCache, IgniteBiPredicate<K, V> filter, - Model<Vector, L> mdl, + IgniteModel<Vector, L> mdl, IgniteBiFunction<K, V, Vector> featureExtractor, IgniteBiFunction<K, V, L> lbExtractor, Metric<L> metric) { @@ -111,7 +111,7 @@ public class BinaryClassificationEvaluator { * @return Computed metric. */ public static <L, K, V> double evaluate(Map<K, V> dataCache, IgniteBiPredicate<K, V> filter, - Model<Vector, L> mdl, + IgniteModel<Vector, L> mdl, IgniteBiFunction<K, V, Vector> featureExtractor, IgniteBiFunction<K, V, L> lbExtractor, Metric<L> metric) { @@ -130,7 +130,7 @@ public class BinaryClassificationEvaluator { * @return Computed metric. */ public static <K, V> BinaryClassificationMetricValues evaluate(IgniteCache<K, V> dataCache, - Model<Vector, Double> mdl, + IgniteModel<Vector, Double> mdl, IgniteBiFunction<K, V, Vector> featureExtractor, IgniteBiFunction<K, V, Double> lbExtractor) { return calcMetricValues(dataCache, null, mdl, featureExtractor, lbExtractor); @@ -148,7 +148,7 @@ public class BinaryClassificationEvaluator { * @return Computed metric. */ public static <K, V> BinaryClassificationMetricValues evaluate(Map<K, V> dataCache, - Model<Vector, Double> mdl, + IgniteModel<Vector, Double> mdl, IgniteBiFunction<K, V, Vector> featureExtractor, IgniteBiFunction<K, V, Double> lbExtractor) { return calcMetricValues(dataCache, null, mdl, featureExtractor, lbExtractor); @@ -168,7 +168,7 @@ public class BinaryClassificationEvaluator { */ public static <K, V> BinaryClassificationMetricValues evaluate(IgniteCache<K, V> dataCache, IgniteBiPredicate<K, V> filter, - Model<Vector, Double> mdl, + IgniteModel<Vector, Double> mdl, IgniteBiFunction<K, V, Vector> featureExtractor, IgniteBiFunction<K, V, Double> lbExtractor) { return calcMetricValues(dataCache, filter, mdl, featureExtractor, lbExtractor); @@ -187,7 +187,7 @@ public class BinaryClassificationEvaluator { * @return Computed metric. */ public static <K, V> BinaryClassificationMetricValues evaluate(Map<K, V> dataCache, IgniteBiPredicate<K, V> filter, - Model<Vector, Double> mdl, + IgniteModel<Vector, Double> mdl, IgniteBiFunction<K, V, Vector> featureExtractor, IgniteBiFunction<K, V, Double> lbExtractor) { return calcMetricValues(dataCache, filter, mdl, featureExtractor, lbExtractor); @@ -207,7 +207,7 @@ public class BinaryClassificationEvaluator { */ private static <K, V> BinaryClassificationMetricValues calcMetricValues(IgniteCache<K, V> dataCache, IgniteBiPredicate<K, V> filter, - Model<Vector, Double> mdl, + IgniteModel<Vector, Double> mdl, IgniteBiFunction<K, V, Vector> featureExtractor, IgniteBiFunction<K, V, Double> lbExtractor) { BinaryClassificationMetricValues metricValues; @@ -243,7 +243,7 @@ public class BinaryClassificationEvaluator { */ private static <K, V> BinaryClassificationMetricValues calcMetricValues(Map<K, V> dataCache, IgniteBiPredicate<K, V> filter, - Model<Vector, Double> mdl, + IgniteModel<Vector, Double> mdl, IgniteBiFunction<K, V, Vector> featureExtractor, IgniteBiFunction<K, V, Double> lbExtractor) { BinaryClassificationMetricValues metricValues; @@ -280,7 +280,7 @@ public class BinaryClassificationEvaluator { * @return Computed metric. */ private static <L, K, V> double calculateMetric(IgniteCache<K, V> dataCache, IgniteBiPredicate<K, V> filter, - Model<Vector, L> mdl, IgniteBiFunction<K, V, Vector> featureExtractor, + IgniteModel<Vector, L> mdl, IgniteBiFunction<K, V, Vector> featureExtractor, IgniteBiFunction<K, V, L> lbExtractor, Metric<L> metric) { double metricRes; @@ -315,7 +315,7 @@ public class BinaryClassificationEvaluator { * @return Computed metric. */ private static <L, K, V> double calculateMetric(Map<K, V> dataCache, IgniteBiPredicate<K, V> filter, - Model<Vector, L> mdl, IgniteBiFunction<K, V, Vector> featureExtractor, + IgniteModel<Vector, L> mdl, IgniteBiFunction<K, V, Vector> featureExtractor, IgniteBiFunction<K, V, L> lbExtractor, Metric<L> metric) { double metricRes; http://git-wip-us.apache.org/repos/asf/ignite/blob/2dc0d9f7/modules/ml/src/main/java/org/apache/ignite/ml/svm/SVMLinearClassificationModel.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/svm/SVMLinearClassificationModel.java b/modules/ml/src/main/java/org/apache/ignite/ml/svm/SVMLinearClassificationModel.java index 579fdb2..4ee822b 100644 --- a/modules/ml/src/main/java/org/apache/ignite/ml/svm/SVMLinearClassificationModel.java +++ b/modules/ml/src/main/java/org/apache/ignite/ml/svm/SVMLinearClassificationModel.java @@ -21,13 +21,13 @@ import java.io.Serializable; import java.util.Objects; import org.apache.ignite.ml.Exportable; import org.apache.ignite.ml.Exporter; -import org.apache.ignite.ml.Model; +import org.apache.ignite.ml.IgniteModel; import org.apache.ignite.ml.math.primitives.vector.Vector; /** * Base class for SVM linear classification model. */ -public class SVMLinearClassificationModel implements Model<Vector, Double>, Exportable<SVMLinearClassificationModel>, Serializable { +public class SVMLinearClassificationModel implements IgniteModel<Vector, Double>, Exportable<SVMLinearClassificationModel>, Serializable { /** */ private static final long serialVersionUID = -996984622291440226L; @@ -94,7 +94,7 @@ public class SVMLinearClassificationModel implements Model<Vector, Double>, Expo } /** {@inheritDoc} */ - @Override public Double apply(Vector input) { + @Override public Double predict(Vector input) { final double res = input.dot(weights) + intercept; if (isKeepingRawLabels) return res;
