Search in sources :

Example 1 with ModelSignature

use of org.apache.ignite.ml.inference.ModelSignature in project ignite by apache.

the class ModelStorageExample method main.

/**
 * Run example.
 */
public static void main(String... args) throws IOException, ClassNotFoundException {
    try (Ignite ignite = Ignition.start("examples/config/example-ignite-ml.xml")) {
        System.out.println(">>> Ignite grid started.");
        ModelStorage storage = new ModelStorageFactory().getModelStorage(ignite);
        ModelDescriptorStorage descStorage = new ModelDescriptorStorageFactory().getModelDescriptorStorage(ignite);
        System.out.println("Saving model into model storage...");
        byte[] mdl = serialize((IgniteModel<byte[], byte[]>) i -> i);
        storage.mkdirs("/");
        storage.putFile("/my_model", mdl);
        System.out.println("Saving model descriptor into model descriptor storage...");
        ModelDescriptor desc = new ModelDescriptor("MyModel", "My Cool Model", new ModelSignature("", "", ""), new ModelStorageModelReader("/my_model"), new IgniteModelParser<>());
        descStorage.put("my_model", desc);
        System.out.println("List saved models...");
        for (IgniteBiTuple<String, ModelDescriptor> model : descStorage) System.out.println("-> {'" + model.getKey() + "' : " + model.getValue() + "}");
        System.out.println("Load saved model descriptor...");
        desc = descStorage.get("my_model");
        System.out.println("Build inference model...");
        SingleModelBuilder mdlBuilder = new SingleModelBuilder();
        try (Model<byte[], byte[]> infMdl = mdlBuilder.build(desc.getReader(), desc.getParser())) {
            System.out.println("Make inference...");
            for (int i = 0; i < 10; i++) {
                Integer res = deserialize(infMdl.predict(serialize(i)));
                System.out.println(i + " -> " + res);
            }
        }
    } finally {
        System.out.flush();
    }
}
Also used : ModelStorageModelReader(org.apache.ignite.ml.inference.reader.ModelStorageModelReader) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ModelDescriptor(org.apache.ignite.ml.inference.ModelDescriptor) ObjectInputStream(java.io.ObjectInputStream) IOException(java.io.IOException) ModelSignature(org.apache.ignite.ml.inference.ModelSignature) Ignite(org.apache.ignite.Ignite) IgniteModel(org.apache.ignite.ml.IgniteModel) ModelDescriptorStorageFactory(org.apache.ignite.ml.inference.storage.descriptor.ModelDescriptorStorageFactory) Serializable(java.io.Serializable) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) SingleModelBuilder(org.apache.ignite.ml.inference.builder.SingleModelBuilder) Ignition(org.apache.ignite.Ignition) ModelStorage(org.apache.ignite.ml.inference.storage.model.ModelStorage) ByteArrayInputStream(java.io.ByteArrayInputStream) Model(org.apache.ignite.ml.inference.Model) IgniteModelParser(org.apache.ignite.ml.inference.parser.IgniteModelParser) ModelDescriptorStorage(org.apache.ignite.ml.inference.storage.descriptor.ModelDescriptorStorage) ObjectOutputStream(java.io.ObjectOutputStream) ModelStorageFactory(org.apache.ignite.ml.inference.storage.model.ModelStorageFactory) ModelStorage(org.apache.ignite.ml.inference.storage.model.ModelStorage) SingleModelBuilder(org.apache.ignite.ml.inference.builder.SingleModelBuilder) ModelStorageModelReader(org.apache.ignite.ml.inference.reader.ModelStorageModelReader) ModelStorageFactory(org.apache.ignite.ml.inference.storage.model.ModelStorageFactory) ModelDescriptorStorage(org.apache.ignite.ml.inference.storage.descriptor.ModelDescriptorStorage) ModelDescriptor(org.apache.ignite.ml.inference.ModelDescriptor) Ignite(org.apache.ignite.Ignite) ModelDescriptorStorageFactory(org.apache.ignite.ml.inference.storage.descriptor.ModelDescriptorStorageFactory) ModelSignature(org.apache.ignite.ml.inference.ModelSignature)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 ObjectInputStream (java.io.ObjectInputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 Serializable (java.io.Serializable)1 Ignite (org.apache.ignite.Ignite)1 Ignition (org.apache.ignite.Ignition)1 IgniteBiTuple (org.apache.ignite.lang.IgniteBiTuple)1 IgniteModel (org.apache.ignite.ml.IgniteModel)1 Model (org.apache.ignite.ml.inference.Model)1 ModelDescriptor (org.apache.ignite.ml.inference.ModelDescriptor)1 ModelSignature (org.apache.ignite.ml.inference.ModelSignature)1 SingleModelBuilder (org.apache.ignite.ml.inference.builder.SingleModelBuilder)1 IgniteModelParser (org.apache.ignite.ml.inference.parser.IgniteModelParser)1 ModelStorageModelReader (org.apache.ignite.ml.inference.reader.ModelStorageModelReader)1 ModelDescriptorStorage (org.apache.ignite.ml.inference.storage.descriptor.ModelDescriptorStorage)1 ModelDescriptorStorageFactory (org.apache.ignite.ml.inference.storage.descriptor.ModelDescriptorStorageFactory)1 ModelStorage (org.apache.ignite.ml.inference.storage.model.ModelStorage)1 ModelStorageFactory (org.apache.ignite.ml.inference.storage.model.ModelStorageFactory)1