use of org.apache.sysml.api.jmlc.Connection in project incubator-systemml by apache.
the class BuildLiteExecution method jmlcScoringExample.
public static void jmlcScoringExample() throws Exception {
String scriptString = "X = read(\"./tmp/X\", rows=-1, cols=-1);\n" + "W = read(\"./tmp/W\", rows=-1, cols=-1);\n" + "\n" + "numRows = nrow(X);\n" + "numCols = ncol(X);\n" + "b = W[numCols+1,]\n" + "scores = X %*% W[1:numCols,] + b;\n" + "predicted_y = rowIndexMax(scores);\n" + "\n" + "print('pred:' + toString(predicted_y))\n" + "\n" + "write(predicted_y, \"./tmp\", format=\"text\");\n";
File file = new File(getRoot() + "scoring-example.dml");
System.out.println(file.toString());
FileUtils.writeStringToFile(file, scriptString);
Connection conn = getConfiguredConnection();
String dml = conn.readScript(getRoot() + "scoring-example.dml");
PreparedScript script = conn.prepareScript(dml, new String[] { "W", "X" }, new String[] { "predicted_y" }, false);
double[][] mtx = matrix(4, 3, new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 });
double[][] result = null;
script.setMatrix("W", mtx);
script.setMatrix("X", randomMatrix(3, 3, -1, 1, 0.7));
result = script.executeScript().getMatrix("predicted_y");
log.debug(displayMatrix(result));
script.setMatrix("W", mtx);
script.setMatrix("X", randomMatrix(3, 3, -1, 1, 0.7));
result = script.executeScript().getMatrix("predicted_y");
log.debug(displayMatrix(result));
script.setMatrix("W", mtx);
script.setMatrix("X", randomMatrix(3, 3, -1, 1, 0.7));
result = script.executeScript().getMatrix("predicted_y");
log.debug(displayMatrix(result));
conn.close();
}
use of org.apache.sysml.api.jmlc.Connection 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();
}
use of org.apache.sysml.api.jmlc.Connection in project incubator-systemml by apache.
the class BuildLiteExecution method jmlcHelloWorld.
public static void jmlcHelloWorld() throws Exception {
Connection conn = getConfiguredConnection();
PreparedScript script = conn.prepareScript("print('hello world');", new String[] {}, new String[] {}, false);
script.executeScript();
conn.close();
}
use of org.apache.sysml.api.jmlc.Connection in project incubator-systemml by apache.
the class BuildLiteExecution method jmlcBasics.
public static void jmlcBasics() throws Exception {
String dml = "A = matrix(\"1 2 3 4 5 6\", rows=3, cols=2)\n" + "print(toString(A))\n" + "B = A + 4\n" + "B = t(B)\n" + "print(toString(B))\n" + "C = A %*% B\n" + "print(toString(C))\n" + "D = matrix(5, rows=nrow(C), cols=ncol(C))\n" + "D = (C - D) / 2\n" + "print(toString(D))\n" + "\n" + "A = matrix(\"1 2 3 4 5 6 7 8 9\", rows=3, cols=3)\n" + "print(toString(A))\n" + "B = A[3,3]\n" + "print(toString(B))\n" + "C = A[2,]\n" + "print(toString(C))\n" + "D = A[,3]\n" + "print(toString(D))\n" + "E = A[2:3,1:2]\n" + "print(toString(E))\n" + "\n" + "i = 1\n" + "while (i <= 3) {\n" + " if (i == 1) {\n" + " print(\'hello\')\n" + " } else if (i == 2) {\n" + " print(\'world\')\n" + " } else {\n" + " print(\'!!!\')\n" + " }\n" + " i = i + 1\n" + "}\n" + "\n" + "A = matrix(\"1 2 3 4 5 6\", rows=3, cols=2)\n" + "\n" + "for (i in 1:nrow(A)) {\n" + " print(\"for A[\" + i + \",1]:\" + as.scalar(A[i,1]))\n" + "}\n" + "\n" + "parfor(i in 1:nrow(A)) {\n" + " print(\"parfor A[\" + i + \",1]:\" + as.scalar(A[i,1]))\n" + "}\n" + "\n" + "doSomething = function(matrix[double] mat) return (matrix[double] ret) {\n" + " additionalCol = matrix(1, rows=nrow(mat), cols=1) # 1x3 matrix with 1 values\n" + " ret = cbind(mat, additionalCol) # concatenate column to matrix\n" + " ret = cbind(ret, seq(0, 2, 1)) # concatenate column (0,1,2) to matrix\n" + " ret = cbind(ret, rowMaxs(ret)) # concatenate column of max row values to matrix\n" + " ret = cbind(ret, rowSums(ret)) # concatenate column of row sums to matrix\n" + "}\n" + "\n" + "A = rand(rows=3, cols=2, min=0, max=2) # random 3x2 matrix with values 0 to 2\n" + "B = doSomething(A)\n" + "write(A, \"" + getRoot() + "A.csv\", format=\"csv\")\n" + "write(B, \"" + getRoot() + "B.csv\", format=\"csv\")\n";
Connection conn = getConfiguredConnection();
PreparedScript script = conn.prepareScript(dml, new String[] {}, new String[] {}, false);
script.executeScript();
conn.close();
}
use of org.apache.sysml.api.jmlc.Connection 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();
}
Aggregations