Search in sources :

Example 16 with ResultVariables

use of org.apache.sysml.api.jmlc.ResultVariables in project incubator-systemml by apache.

the class MulticlassSVMScoreTest method execDMLScriptviaJMLC.

private static ArrayList<double[][]> execDMLScriptviaJMLC(ArrayList<double[][]> X, boolean flags) throws IOException {
    Timing time = new Timing(true);
    ArrayList<double[][]> ret = new ArrayList<double[][]>();
    // establish connection to SystemML
    Connection conn = !flags ? new Connection() : new Connection(ConfigType.PARALLEL_CP_MATRIX_OPERATIONS, ConfigType.PARALLEL_LOCAL_OR_REMOTE_PARFOR, ConfigType.ALLOW_DYN_RECOMPILATION);
    try {
        // For now, JMLC pipeline only allows dml
        boolean parsePyDML = false;
        // read and precompile script
        String script = conn.readScript(SCRIPT_DIR + TEST_DIR + TEST_NAME + ".dml");
        PreparedScript pstmt = conn.prepareScript(script, new String[] { "X", "W" }, new String[] { "predicted_y" }, parsePyDML);
        // read model
        String modelData = conn.readScript(SCRIPT_DIR + TEST_DIR + MODEL_FILE);
        double[][] W = conn.convertToDoubleMatrix(modelData, rows, cols);
        // execute script multiple times
        for (int i = 0; i < nRuns; i++) {
            // bind input parameters
            pstmt.setMatrix("W", W);
            pstmt.setMatrix("X", X.get(i));
            // execute script
            ResultVariables rs = pstmt.executeScript();
            // get output parameter
            double[][] Y = rs.getMatrix("predicted_y");
            // keep result for comparison
            ret.add(Y);
        }
    } catch (Exception ex) {
        ex.printStackTrace();
        throw new IOException(ex);
    } finally {
        if (conn != null)
            conn.close();
    }
    System.out.println("JMLC scoring w/ " + nRuns + " runs in " + time.stop() + "ms.");
    return ret;
}
Also used : PreparedScript(org.apache.sysml.api.jmlc.PreparedScript) ResultVariables(org.apache.sysml.api.jmlc.ResultVariables) ArrayList(java.util.ArrayList) Connection(org.apache.sysml.api.jmlc.Connection) Timing(org.apache.sysml.runtime.controlprogram.parfor.stat.Timing) IOException(java.io.IOException) IOException(java.io.IOException)

Example 17 with ResultVariables

use of org.apache.sysml.api.jmlc.ResultVariables in project incubator-systemml by apache.

the class ReuseModelVariablesTest method execDMLScriptviaJMLC.

private static ArrayList<double[][]> execDMLScriptviaJMLC(String testname, ArrayList<double[][]> X, boolean modelReuse) throws IOException {
    Timing time = new Timing(true);
    ArrayList<double[][]> ret = new ArrayList<double[][]>();
    // establish connection to SystemML
    Connection conn = new Connection();
    try {
        // For now, JMLC pipeline only allows dml
        boolean parsePyDML = false;
        // read and precompile script
        String script = conn.readScript(SCRIPT_DIR + TEST_DIR + testname + ".dml");
        PreparedScript pstmt = conn.prepareScript(script, new String[] { "X", "W" }, new String[] { "predicted_y" }, parsePyDML);
        // read model
        String modelData = conn.readScript(SCRIPT_DIR + TEST_DIR + MODEL_FILE);
        double[][] W = conn.convertToDoubleMatrix(modelData, rows, cols);
        if (modelReuse)
            pstmt.setMatrix("W", W, true);
        // execute script multiple times
        for (int i = 0; i < nRuns; i++) {
            // bind input parameters
            if (!modelReuse)
                pstmt.setMatrix("W", W);
            pstmt.setMatrix("X", X.get(i));
            // execute script
            ResultVariables rs = pstmt.executeScript();
            // get output parameter
            double[][] Y = rs.getMatrix("predicted_y");
            // keep result for comparison
            ret.add(Y);
        }
    } catch (Exception ex) {
        ex.printStackTrace();
        throw new IOException(ex);
    } finally {
        IOUtilFunctions.closeSilently(conn);
    }
    System.out.println("JMLC scoring w/ " + nRuns + " runs in " + time.stop() + "ms.");
    return ret;
}
Also used : PreparedScript(org.apache.sysml.api.jmlc.PreparedScript) ResultVariables(org.apache.sysml.api.jmlc.ResultVariables) ArrayList(java.util.ArrayList) Connection(org.apache.sysml.api.jmlc.Connection) Timing(org.apache.sysml.runtime.controlprogram.parfor.stat.Timing) IOException(java.io.IOException) IOException(java.io.IOException)

Example 18 with ResultVariables

use of org.apache.sysml.api.jmlc.ResultVariables in project incubator-systemml by apache.

the class BuildLiteExecution method jmlcALS.

public static void jmlcALS() throws Exception {
    Connection conn = getConfiguredConnection();
    String dataGen = conn.readScript("scripts/datagen/genRandData4ALS.dml");
    Map<String, String> m = new HashMap<>();
    m.put("$rows", "1000");
    m.put("$cols", "1000");
    m.put("$rank", "100");
    m.put("$nnz", "10000");
    PreparedScript dataGenScript = conn.prepareScript(dataGen, m, new String[] {}, new String[] { "X", "W", "H" }, false);
    ResultVariables dataGenResults = dataGenScript.executeScript();
    double[][] x = dataGenResults.getMatrix("X");
    log.debug(displayMatrix(x));
    Map<String, String> m2 = new HashMap<>();
    m2.put("$rank", "100");
    String alsCg = conn.readScript("scripts/algorithms/ALS-CG.dml");
    PreparedScript alsCgScript = conn.prepareScript(alsCg, m2, new String[] { "X" }, new String[] { "U", "V" }, false);
    alsCgScript.setMatrix("X", x);
    ResultVariables alsCgResults = alsCgScript.executeScript();
    double[][] u = alsCgResults.getMatrix("U");
    log.debug("u:" + u);
    log.debug(displayMatrix(u));
    double[][] v = alsCgResults.getMatrix("V");
    log.debug("v:" + v);
    log.debug(displayMatrix(v));
    String alsDs = conn.readScript("scripts/algorithms/ALS-DS.dml");
    PreparedScript alsDsScript = conn.prepareScript(alsDs, m2, new String[] { "V" }, new String[] { "L", "Rt" }, false);
    alsDsScript.setMatrix("V", x);
    ResultVariables alsDsResults = alsDsScript.executeScript();
    double[][] l = alsDsResults.getMatrix("L");
    log.debug("l:" + l);
    log.debug(displayMatrix(l));
    double[][] rt = alsDsResults.getMatrix("Rt");
    log.debug("rt:" + rt);
    log.debug(displayMatrix(rt));
    conn.close();
}
Also used : PreparedScript(org.apache.sysml.api.jmlc.PreparedScript) HashMap(java.util.HashMap) ResultVariables(org.apache.sysml.api.jmlc.ResultVariables) Connection(org.apache.sysml.api.jmlc.Connection)

Example 19 with ResultVariables

use of org.apache.sysml.api.jmlc.ResultVariables in project incubator-systemml by apache.

the class BuildLiteExecution method jmlcL2SVM.

public static void jmlcL2SVM() throws Exception {
    Connection conn = getConfiguredConnection();
    String dml = conn.readScript("scripts/algorithms/l2-svm.dml");
    PreparedScript l2svm = conn.prepareScript(dml, new String[] { "X", "Y", "fmt", "Log" }, new String[] { "w", "debug_str" }, false);
    double[][] trainData = new double[150][3];
    for (int i = 0; i < 150; i++) {
        int one = ThreadLocalRandom.current().nextInt(0, 101);
        int two = ThreadLocalRandom.current().nextInt(0, 101);
        int three = ThreadLocalRandom.current().nextInt(0, 101);
        double[] row = new double[] { one, two, three };
        trainData[i] = row;
    }
    l2svm.setMatrix("X", trainData);
    log.debug(displayMatrix(trainData));
    double[][] trainLabels = new double[150][1];
    for (int i = 0; i < 150; i++) {
        int one = ThreadLocalRandom.current().nextInt(1, 3);
        double[] row = new double[] { one };
        trainLabels[i] = row;
    }
    l2svm.setMatrix("Y", trainLabels);
    log.debug(displayMatrix(trainLabels));
    l2svm.setScalar("fmt", "csv");
    l2svm.setScalar("Log", "temp/l2-svm-log.csv");
    ResultVariables l2svmResults = l2svm.executeScript();
    double[][] model = l2svmResults.getMatrix("w");
    log.debug("MODEL:");
    log.debug(displayMatrix(model));
    String debugString = l2svmResults.getString("debug_str");
    log.debug("DEBUG STRING:");
    log.debug(debugString);
    String s = conn.readScript("scripts/algorithms/l2-svm-predict.dml");
    Map<String, String> m = new HashMap<>();
    m.put("$Y", "temp/1.csv");
    m.put("$confusion", "temp/2.csv");
    m.put("$scores", "temp/3.csv");
    PreparedScript l2svmPredict = conn.prepareScript(s, m, new String[] { "X", "Y", "w", "fmt" }, new String[] { "scores", "confusion_mat" }, false);
    double[][] testData = new double[150][3];
    for (int i = 0; i < 150; i++) {
        int one = ThreadLocalRandom.current().nextInt(0, 101);
        int two = ThreadLocalRandom.current().nextInt(0, 101);
        int three = ThreadLocalRandom.current().nextInt(0, 101);
        double[] row = new double[] { one, two, three };
        testData[i] = row;
    }
    l2svmPredict.setMatrix("X", testData);
    double[][] testLabels = new double[150][1];
    for (int i = 0; i < 150; i++) {
        int one = ThreadLocalRandom.current().nextInt(1, 3);
        double[] row = new double[] { one };
        testLabels[i] = row;
    }
    l2svmPredict.setMatrix("Y", testLabels);
    l2svmPredict.setMatrix("w", model);
    l2svmPredict.setScalar("fmt", "csv");
    ResultVariables l2svmPredictResults = l2svmPredict.executeScript();
    double[][] scores = l2svmPredictResults.getMatrix("scores");
    log.debug("SCORES:");
    log.debug(displayMatrix(scores));
    double[][] confusionMatrix = l2svmPredictResults.getMatrix("confusion_mat");
    log.debug("CONFUSION MATRIX:");
    log.debug(displayMatrix(confusionMatrix));
    conn.close();
}
Also used : PreparedScript(org.apache.sysml.api.jmlc.PreparedScript) ResultVariables(org.apache.sysml.api.jmlc.ResultVariables) HashMap(java.util.HashMap) Connection(org.apache.sysml.api.jmlc.Connection)

Example 20 with ResultVariables

use of org.apache.sysml.api.jmlc.ResultVariables in project incubator-systemml by apache.

the class BuildLiteExecution method jmlcKmeans.

public static void jmlcKmeans() throws Exception {
    Connection conn = getConfiguredConnection();
    Map<String, String> m = new HashMap<>();
    m.put("$k", "5");
    m.put("$isY", "TRUE");
    m.put("$verb", "TRUE");
    String kMeans = conn.readScript("scripts/algorithms/Kmeans.dml");
    PreparedScript kMeansScript = conn.prepareScript(kMeans, m, new String[] { "X" }, new String[] { "C", "Y" }, false);
    double[][] x = randomMatrix(50, 50, -1, 1, 0.1);
    kMeansScript.setMatrix("X", x);
    log.debug("X:");
    log.debug(displayMatrix(x));
    ResultVariables kMeansResults = kMeansScript.executeScript();
    double[][] c = kMeansResults.getMatrix("C");
    log.debug("C:");
    log.debug(displayMatrix(c));
    double[][] y = kMeansResults.getMatrix("Y");
    log.debug("Y:");
    log.debug(displayMatrix(y));
    conn.close();
}
Also used : PreparedScript(org.apache.sysml.api.jmlc.PreparedScript) HashMap(java.util.HashMap) ResultVariables(org.apache.sysml.api.jmlc.ResultVariables) Connection(org.apache.sysml.api.jmlc.Connection)

Aggregations

Connection (org.apache.sysml.api.jmlc.Connection)27 PreparedScript (org.apache.sysml.api.jmlc.PreparedScript)27 ResultVariables (org.apache.sysml.api.jmlc.ResultVariables)27 HashMap (java.util.HashMap)20 IOException (java.io.IOException)19 ArrayList (java.util.ArrayList)17 Timing (org.apache.sysml.runtime.controlprogram.parfor.stat.Timing)17 FrameBlock (org.apache.sysml.runtime.matrix.data.FrameBlock)2 TestConfiguration (org.apache.sysml.test.integration.TestConfiguration)2 DMLException (org.apache.sysml.api.DMLException)1