Search in sources :

Example 1 with FileSystemModelReader

use of org.apache.ignite.ml.inference.reader.FileSystemModelReader in project ignite by apache.

the class CatboostClassificationModelParserExample method main.

/**
 * Run example.
 */
public static void main(String... args) throws ExecutionException, InterruptedException, FileNotFoundException {
    try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
        File mdlRsrc = IgniteUtils.resolveIgnitePath(TEST_MODEL_RES);
        if (mdlRsrc == null)
            throw new IllegalArgumentException("File not found [resource_path=" + TEST_MODEL_RES + "]");
        ModelReader reader = new FileSystemModelReader(mdlRsrc.getPath());
        AsyncModelBuilder mdlBuilder = new IgniteDistributedModelBuilder(ignite, 4, 4);
        File testData = IgniteUtils.resolveIgnitePath(TEST_DATA_RES);
        if (testData == null)
            throw new IllegalArgumentException("File not found [resource_path=" + TEST_DATA_RES + "]");
        File testExpRes = IgniteUtils.resolveIgnitePath(TEST_ER_RES);
        if (testExpRes == null)
            throw new IllegalArgumentException("File not found [resource_path=" + TEST_ER_RES + "]");
        try (Model<NamedVector, Future<Double>> mdl = mdlBuilder.build(reader, parser);
            Scanner testDataScanner = new Scanner(testData);
            Scanner testExpResultsScanner = new Scanner(testExpRes)) {
            String header = testDataScanner.nextLine();
            String[] columns = header.split(",");
            while (testDataScanner.hasNextLine()) {
                String testDataStr = testDataScanner.nextLine();
                String testExpResultsStr = testExpResultsScanner.nextLine();
                HashMap<String, Double> testObj = new HashMap<>();
                String[] values = testDataStr.split(",");
                for (int i = 0; i < columns.length; i++) {
                    testObj.put(columns[i], Double.valueOf(values[i]));
                }
                double prediction = mdl.predict(VectorUtils.of(testObj)).get();
                double expPrediction = Double.parseDouble(testExpResultsStr);
                System.out.println("Expected: " + expPrediction + ", prediction: " + prediction);
            }
        }
    } finally {
        System.out.flush();
    }
}
Also used : Scanner(java.util.Scanner) HashMap(java.util.HashMap) FileSystemModelReader(org.apache.ignite.ml.inference.reader.FileSystemModelReader) ModelReader(org.apache.ignite.ml.inference.reader.ModelReader) FileSystemModelReader(org.apache.ignite.ml.inference.reader.FileSystemModelReader) NamedVector(org.apache.ignite.ml.math.primitives.vector.NamedVector) AsyncModelBuilder(org.apache.ignite.ml.inference.builder.AsyncModelBuilder) Future(java.util.concurrent.Future) Ignite(org.apache.ignite.Ignite) IgniteDistributedModelBuilder(org.apache.ignite.ml.inference.builder.IgniteDistributedModelBuilder) File(java.io.File)

Example 2 with FileSystemModelReader

use of org.apache.ignite.ml.inference.reader.FileSystemModelReader in project ignite by apache.

the class H2OMojoParserTest method testParseAndPredict.

/**
 */
@Test
public void testParseAndPredict() {
    URL url = H2OMojoParserTest.class.getClassLoader().getResource(TEST_MODEL_RESOURCE);
    if (url == null)
        throw new IllegalStateException("File not found [resource_name=" + TEST_MODEL_RESOURCE + "]");
    ModelReader reader = new FileSystemModelReader(url.getPath());
    try (H2OMojoModel mdl = mdlBuilder.build(reader, parser)) {
        HashMap<String, Double> input = new HashMap<>();
        input.put("RACE", 1.0);
        input.put("DPROS", 2.0);
        input.put("DCAPS", 1.0);
        input.put("PSA", 1.4);
        input.put("VOL", 0.0);
        input.put("GLEASON", 6.0);
        double prediction = mdl.predict(VectorUtils.of(input));
        assertEquals(64.50328, prediction, 1e-5);
    }
}
Also used : FileSystemModelReader(org.apache.ignite.ml.inference.reader.FileSystemModelReader) ModelReader(org.apache.ignite.ml.inference.reader.ModelReader) HashMap(java.util.HashMap) FileSystemModelReader(org.apache.ignite.ml.inference.reader.FileSystemModelReader) URL(java.net.URL) Test(org.junit.Test)

Example 3 with FileSystemModelReader

use of org.apache.ignite.ml.inference.reader.FileSystemModelReader in project ignite by apache.

the class CatboostClassificationModelParserTest method testParseAndPredict.

/**
 * End-to-end test for {@code parse()} and {@code predict()} methods.
 */
@Test
public void testParseAndPredict() {
    URL url = CatboostClassificationModelParserTest.class.getClassLoader().getResource(TEST_MODEL_RESOURCE);
    if (url == null)
        throw new IllegalStateException("File not found [resource_name=" + TEST_MODEL_RESOURCE + "]");
    ModelReader reader = new FileSystemModelReader(url.getPath());
    try (CatboostClassificationModel mdl = mdlBuilder.build(reader, parser)) {
        HashMap<String, Double> input = new HashMap<>();
        input.put("ACTION", 1.0);
        input.put("RESOURCE", 39353.0);
        input.put("MGR_ID", 85475.0);
        input.put("ROLE_ROLLUP_1", 117961.0);
        input.put("ROLE_ROLLUP_2", 118300.0);
        input.put("ROLE_DEPTNAME", 123472.0);
        input.put("ROLE_TITLE", 117905.0);
        input.put("ROLE_FAMILY_DESC", 117906.0);
        input.put("ROLE_FAMILY", 290919.0);
        input.put("ROLE_CODE", 117908.0);
        double prediction = mdl.predict(VectorUtils.of(input));
        assertEquals(0.9928904609329371, prediction, 1e-5);
    }
}
Also used : FileSystemModelReader(org.apache.ignite.ml.inference.reader.FileSystemModelReader) ModelReader(org.apache.ignite.ml.inference.reader.ModelReader) HashMap(java.util.HashMap) CatboostClassificationModel(org.apache.ignite.ml.catboost.CatboostClassificationModel) FileSystemModelReader(org.apache.ignite.ml.inference.reader.FileSystemModelReader) URL(java.net.URL) Test(org.junit.Test)

Example 4 with FileSystemModelReader

use of org.apache.ignite.ml.inference.reader.FileSystemModelReader in project ignite by apache.

the class XGBoostModelParserTest method testParseAndPredict.

/**
 * End-to-end test for {@code parse()} and {@code predict()} methods.
 */
@Test
public void testParseAndPredict() {
    URL url = XGBoostModelParserTest.class.getClassLoader().getResource(TEST_MODEL_RESOURCE);
    if (url == null)
        throw new IllegalStateException("File not found [resource_name=" + TEST_MODEL_RESOURCE + "]");
    ModelReader reader = new FileSystemModelReader(url.getPath());
    try (XGModelComposition mdl = mdlBuilder.build(reader, parser);
        Scanner testDataScanner = new Scanner(XGBoostModelParserTest.class.getClassLoader().getResourceAsStream("datasets/agaricus-test-data.txt"));
        Scanner testExpResultsScanner = new Scanner(XGBoostModelParserTest.class.getClassLoader().getResourceAsStream("datasets/agaricus-test-expected-results.txt"))) {
        while (testDataScanner.hasNextLine()) {
            assertTrue(testExpResultsScanner.hasNextLine());
            String testDataStr = testDataScanner.nextLine();
            String testExpResultsStr = testExpResultsScanner.nextLine();
            HashMap<String, Double> testObj = new HashMap<>();
            for (String keyValueString : testDataStr.split(" ")) {
                String[] keyVal = keyValueString.split(":");
                if (keyVal.length == 2)
                    testObj.put("f" + keyVal[0], Double.parseDouble(keyVal[1]));
            }
            double prediction = mdl.predict(VectorUtils.of(testObj));
            double expPrediction = Double.parseDouble(testExpResultsStr);
            assertEquals(expPrediction, prediction, 1e-6);
        }
    }
}
Also used : Scanner(java.util.Scanner) XGModelComposition(org.apache.ignite.ml.xgboost.XGModelComposition) HashMap(java.util.HashMap) FileSystemModelReader(org.apache.ignite.ml.inference.reader.FileSystemModelReader) URL(java.net.URL) FileSystemModelReader(org.apache.ignite.ml.inference.reader.FileSystemModelReader) ModelReader(org.apache.ignite.ml.inference.reader.ModelReader) Test(org.junit.Test)

Example 5 with FileSystemModelReader

use of org.apache.ignite.ml.inference.reader.FileSystemModelReader in project ignite by apache.

the class XGBoostModelParserExample method main.

/**
 * Run example.
 */
public static void main(String... args) throws ExecutionException, InterruptedException, FileNotFoundException {
    try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
        File mdlRsrc = IgniteUtils.resolveIgnitePath(TEST_MODEL_RES);
        if (mdlRsrc == null)
            throw new IllegalArgumentException("File not found [resource_path=" + TEST_MODEL_RES + "]");
        ModelReader reader = new FileSystemModelReader(mdlRsrc.getPath());
        AsyncModelBuilder mdlBuilder = new IgniteDistributedModelBuilder(ignite, 4, 4);
        File testData = IgniteUtils.resolveIgnitePath(TEST_DATA_RES);
        if (testData == null)
            throw new IllegalArgumentException("File not found [resource_path=" + TEST_DATA_RES + "]");
        File testExpRes = IgniteUtils.resolveIgnitePath(TEST_ER_RES);
        if (testExpRes == null)
            throw new IllegalArgumentException("File not found [resource_path=" + TEST_ER_RES + "]");
        try (Model<NamedVector, Future<Double>> mdl = mdlBuilder.build(reader, parser);
            Scanner testDataScanner = new Scanner(testData);
            Scanner testExpResultsScanner = new Scanner(testExpRes)) {
            while (testDataScanner.hasNextLine()) {
                String testDataStr = testDataScanner.nextLine();
                String testExpResultsStr = testExpResultsScanner.nextLine();
                HashMap<String, Double> testObj = new HashMap<>();
                for (String keyValueString : testDataStr.split(" ")) {
                    String[] keyVal = keyValueString.split(":");
                    if (keyVal.length == 2)
                        testObj.put("f" + keyVal[0], Double.parseDouble(keyVal[1]));
                }
                double prediction = mdl.predict(VectorUtils.of(testObj)).get();
                double expPrediction = Double.parseDouble(testExpResultsStr);
                System.out.println("Expected: " + expPrediction + ", prediction: " + prediction);
            }
        }
    } finally {
        System.out.flush();
    }
}
Also used : Scanner(java.util.Scanner) HashMap(java.util.HashMap) FileSystemModelReader(org.apache.ignite.ml.inference.reader.FileSystemModelReader) ModelReader(org.apache.ignite.ml.inference.reader.ModelReader) FileSystemModelReader(org.apache.ignite.ml.inference.reader.FileSystemModelReader) NamedVector(org.apache.ignite.ml.math.primitives.vector.NamedVector) AsyncModelBuilder(org.apache.ignite.ml.inference.builder.AsyncModelBuilder) Future(java.util.concurrent.Future) Ignite(org.apache.ignite.Ignite) IgniteDistributedModelBuilder(org.apache.ignite.ml.inference.builder.IgniteDistributedModelBuilder) File(java.io.File)

Aggregations

HashMap (java.util.HashMap)8 FileSystemModelReader (org.apache.ignite.ml.inference.reader.FileSystemModelReader)8 ModelReader (org.apache.ignite.ml.inference.reader.ModelReader)8 Scanner (java.util.Scanner)5 File (java.io.File)4 URL (java.net.URL)4 Future (java.util.concurrent.Future)4 Ignite (org.apache.ignite.Ignite)4 AsyncModelBuilder (org.apache.ignite.ml.inference.builder.AsyncModelBuilder)4 IgniteDistributedModelBuilder (org.apache.ignite.ml.inference.builder.IgniteDistributedModelBuilder)4 NamedVector (org.apache.ignite.ml.math.primitives.vector.NamedVector)4 Test (org.junit.Test)4 CatboostClassificationModel (org.apache.ignite.ml.catboost.CatboostClassificationModel)1 CatboostRegressionModel (org.apache.ignite.ml.catboost.CatboostRegressionModel)1 XGModelComposition (org.apache.ignite.ml.xgboost.XGModelComposition)1