use of org.apache.sysml.api.jmlc.Connection in project 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 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 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();
}
use of org.apache.sysml.api.jmlc.Connection in project 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();
}
use of org.apache.sysml.api.jmlc.Connection in project 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();
}
Aggregations