Search in sources :

Example 1 with IgniteModelParser

use of org.apache.ignite.ml.inference.parser.IgniteModelParser in project ignite by apache.

the class IgniteModelDistributedInferenceExample method main.

/**
 * Run example.
 */
public static void main(String... args) throws IOException, ExecutionException, InterruptedException {
    System.out.println();
    System.out.println(">>> Linear regression model over cache based dataset usage example started.");
    // Start ignite grid.
    try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
        System.out.println(">>> Ignite grid started.");
        IgniteCache<Integer, Vector> dataCache = null;
        try {
            dataCache = new SandboxMLCache(ignite).fillCacheWith(MLSandboxDatasets.MORTALITY_DATA);
            System.out.println(">>> Create new linear regression trainer object.");
            LinearRegressionLSQRTrainer trainer = new LinearRegressionLSQRTrainer();
            System.out.println(">>> Perform the training to get the model.");
            LinearRegressionModel mdl = trainer.fit(ignite, dataCache, new DummyVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.FIRST));
            System.out.println(">>> Linear regression model: " + mdl);
            System.out.println(">>> Preparing model reader and model parser.");
            ModelReader reader = new InMemoryModelReader(mdl);
            ModelParser<Vector, Double, ?> parser = new IgniteModelParser<>();
            try (Model<Vector, Future<Double>> infMdl = new IgniteDistributedModelBuilder(ignite, 4, 4).build(reader, parser)) {
                System.out.println(">>> Inference model is ready.");
                System.out.println(">>> ---------------------------------");
                System.out.println(">>> | Prediction\t| Ground Truth\t|");
                System.out.println(">>> ---------------------------------");
                try (QueryCursor<Cache.Entry<Integer, Vector>> observations = dataCache.query(new ScanQuery<>())) {
                    for (Cache.Entry<Integer, Vector> observation : observations) {
                        Vector val = observation.getValue();
                        Vector inputs = val.copyOfRange(1, val.size());
                        double groundTruth = val.get(0);
                        double prediction = infMdl.predict(inputs).get();
                        System.out.printf(">>> | %.4f\t\t| %.4f\t\t|\n", prediction, groundTruth);
                    }
                }
            }
            System.out.println(">>> ---------------------------------");
            System.out.println(">>> Linear regression model over cache based dataset usage example completed.");
        } finally {
            if (dataCache != null)
                dataCache.destroy();
        }
    } finally {
        System.out.flush();
    }
}
Also used : SandboxMLCache(org.apache.ignite.examples.ml.util.SandboxMLCache) LinearRegressionModel(org.apache.ignite.ml.regressions.linear.LinearRegressionModel) IgniteModelParser(org.apache.ignite.ml.inference.parser.IgniteModelParser) DummyVectorizer(org.apache.ignite.ml.dataset.feature.extractor.impl.DummyVectorizer) InMemoryModelReader(org.apache.ignite.ml.inference.reader.InMemoryModelReader) LinearRegressionLSQRTrainer(org.apache.ignite.ml.regressions.linear.LinearRegressionLSQRTrainer) InMemoryModelReader(org.apache.ignite.ml.inference.reader.InMemoryModelReader) ModelReader(org.apache.ignite.ml.inference.reader.ModelReader) Future(java.util.concurrent.Future) Ignite(org.apache.ignite.Ignite) IgniteDistributedModelBuilder(org.apache.ignite.ml.inference.builder.IgniteDistributedModelBuilder) Vector(org.apache.ignite.ml.math.primitives.vector.Vector) IgniteCache(org.apache.ignite.IgniteCache) SandboxMLCache(org.apache.ignite.examples.ml.util.SandboxMLCache) Cache(javax.cache.Cache)

Example 2 with IgniteModelParser

use of org.apache.ignite.ml.inference.parser.IgniteModelParser in project ignite by apache.

the class IgniteModelStorageUtil method saveModelDescriptor.

/**
 * Saves model descriptor into descriptor storage if a model with given name is not saved yet, otherwise throws
 * exception. To save model with the same name remove old model first.
 *
 * @param ignite Ignite instance.
 * @param name Model name.
 * @param mdlId Model identifier used to find model in model storage (only with {@link ModelStorageModelReader}).
 * @throws IllegalArgumentException If model with given name was already saved.
 */
private static void saveModelDescriptor(Ignite ignite, String name, UUID mdlId) {
    ModelDescriptorStorage descStorage = new ModelDescriptorStorageFactory().getModelDescriptorStorage(ignite);
    boolean saved = descStorage.putIfAbsent(name, new ModelDescriptor(mdlId.toString(), null, new ModelSignature(null, null, null), new ModelStorageModelReader(IGNITE_MDL_FOLDER + "/" + mdlId), new IgniteModelParser<>()));
    if (!saved)
        throw new IllegalArgumentException("Model descriptor with given name already exists [name=" + name + "]");
}
Also used : IgniteModelParser(org.apache.ignite.ml.inference.parser.IgniteModelParser) ModelStorageModelReader(org.apache.ignite.ml.inference.reader.ModelStorageModelReader) ModelDescriptorStorage(org.apache.ignite.ml.inference.storage.descriptor.ModelDescriptorStorage) ModelDescriptorStorageFactory(org.apache.ignite.ml.inference.storage.descriptor.ModelDescriptorStorageFactory)

Example 3 with IgniteModelParser

use of org.apache.ignite.ml.inference.parser.IgniteModelParser 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

IgniteModelParser (org.apache.ignite.ml.inference.parser.IgniteModelParser)3 Ignite (org.apache.ignite.Ignite)2 ModelStorageModelReader (org.apache.ignite.ml.inference.reader.ModelStorageModelReader)2 ModelDescriptorStorage (org.apache.ignite.ml.inference.storage.descriptor.ModelDescriptorStorage)2 ModelDescriptorStorageFactory (org.apache.ignite.ml.inference.storage.descriptor.ModelDescriptorStorageFactory)2 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 Future (java.util.concurrent.Future)1 Cache (javax.cache.Cache)1 IgniteCache (org.apache.ignite.IgniteCache)1 Ignition (org.apache.ignite.Ignition)1 SandboxMLCache (org.apache.ignite.examples.ml.util.SandboxMLCache)1 IgniteBiTuple (org.apache.ignite.lang.IgniteBiTuple)1 IgniteModel (org.apache.ignite.ml.IgniteModel)1 DummyVectorizer (org.apache.ignite.ml.dataset.feature.extractor.impl.DummyVectorizer)1 Model (org.apache.ignite.ml.inference.Model)1