Search in sources :

Example 31 with PreparedScript

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

the class BuildLiteExecution method jmlcUnivariateStatistics.

public static void jmlcUnivariateStatistics() throws Exception {
    Connection conn = getConfiguredConnection();
    String dml = conn.readScript("scripts/algorithms/Univar-Stats.dml");
    Map<String, String> m = new HashMap<>();
    m.put("$CONSOLE_OUTPUT", "TRUE");
    PreparedScript script = conn.prepareScript(dml, m, new String[] { "A", "K" }, new String[] { "baseStats" }, false);
    double[][] data = new double[100][4];
    for (int i = 0; i < 100; i++) {
        int one = ThreadLocalRandom.current().nextInt(0, 101);
        int two = ThreadLocalRandom.current().nextInt(0, 101);
        int three = ThreadLocalRandom.current().nextInt(0, 101);
        int four = ThreadLocalRandom.current().nextInt(1, 3);
        double[] row = new double[] { one, two, three, four };
        data[i] = row;
    }
    log.debug(displayMatrix(data));
    double[][] types = matrix(1, 4, new double[] { 1, 1, 1, 2 });
    script.setMatrix("A", data);
    script.setMatrix("K", types);
    double[][] baseStats = script.executeScript().getMatrix("baseStats");
    log.debug(displayMatrix(baseStats));
    conn.close();
}
Also used : PreparedScript(org.apache.sysml.api.jmlc.PreparedScript) HashMap(java.util.HashMap) Connection(org.apache.sysml.api.jmlc.Connection)

Example 32 with PreparedScript

use of org.apache.sysml.api.jmlc.PreparedScript 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 33 with PreparedScript

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

the class BuildLiteExecution method jmlcReadMatrix.

public static void jmlcReadMatrix() throws Exception {
    Connection conn = getConfiguredConnection();
    PreparedScript script = conn.prepareScript("x=read('" + getRoot() + "x.csv',format='csv');y=x*2;print(toString(y));", new String[] {}, new String[] {}, false);
    script.executeScript();
    String scriptString = "m = read('" + getRoot() + "m.csv',format='csv')\n" + "print(toString(m))\n" + "print('min:' + min(m))\n" + "print('max:' + max(m))\n" + "print('sum:' + sum(m))\n" + "mRowSums = rowSums(m)\n" + "for (i in 1:nrow(mRowSums)) {\n" + "    print('row ' + i + ' sum:' + as.scalar(mRowSums[i,1]))\n" + "}\n" + "mColSums = colSums(m)\n" + "for (i in 1:ncol(mColSums)) {\n" + "    print('col ' + i + ' sum:' + as.scalar(mColSums[1,i]))\n" + "}\n";
    script = conn.prepareScript(scriptString, new String[] {}, new String[] {}, false);
    script.executeScript();
    conn.close();
}
Also used : PreparedScript(org.apache.sysml.api.jmlc.PreparedScript) Connection(org.apache.sysml.api.jmlc.Connection)

Example 34 with PreparedScript

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

the class BuildLiteExecution method jmlcWriteMatrix.

public static void jmlcWriteMatrix() throws Exception {
    Connection conn = getConfiguredConnection();
    PreparedScript script = conn.prepareScript("x=matrix('1 2 3 4',rows=2,cols=2);write(x,'" + getRoot() + "x.csv',format='csv');", new String[] {}, new String[] {}, false);
    script.executeScript();
    String scriptString = "m = matrix('1 2 3 0 0 0 7 8 9 0 0 0', rows=4, cols=3)\n" + "write(m, '" + getRoot() + "m.txt', format='text')\n" + "write(m, '" + getRoot() + "m.mm', format='mm')\n" + "write(m, '" + getRoot() + "m.csv', format='csv')\n" + "write(m, '" + getRoot() + "m.binary', format='binary')\n";
    script = conn.prepareScript(scriptString, new String[] {}, new String[] {}, false);
    script.executeScript();
    conn.close();
}
Also used : PreparedScript(org.apache.sysml.api.jmlc.PreparedScript) Connection(org.apache.sysml.api.jmlc.Connection)

Example 35 with PreparedScript

use of org.apache.sysml.api.jmlc.PreparedScript 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)37 PreparedScript (org.apache.sysml.api.jmlc.PreparedScript)37 ResultVariables (org.apache.sysml.api.jmlc.ResultVariables)14 Test (org.junit.Test)14 IOException (java.io.IOException)12 HashMap (java.util.HashMap)11 ArrayList (java.util.ArrayList)10 Timing (org.apache.sysml.runtime.controlprogram.parfor.stat.Timing)9 DMLException (org.apache.sysml.api.DMLException)2 File (java.io.File)1 ExecutorService (java.util.concurrent.ExecutorService)1 Future (java.util.concurrent.Future)1 SparkConf (org.apache.spark.SparkConf)1 JavaSparkContext (org.apache.spark.api.java.JavaSparkContext)1 DMLScript (org.apache.sysml.api.DMLScript)1 MLContext (org.apache.sysml.api.mlcontext.MLContext)1 Script (org.apache.sysml.api.mlcontext.Script)1 ScalarObject (org.apache.sysml.runtime.instructions.cp.ScalarObject)1 FrameBlock (org.apache.sysml.runtime.matrix.data.FrameBlock)1 MatrixBlock (org.apache.sysml.runtime.matrix.data.MatrixBlock)1