use of org.apache.sysml.api.jmlc.Connection 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();
}
use of org.apache.sysml.api.jmlc.Connection 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();
}
use of org.apache.sysml.api.jmlc.Connection 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();
}
use of org.apache.sysml.api.jmlc.Connection 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();
}
use of org.apache.sysml.api.jmlc.Connection in project incubator-systemml by apache.
the class BuildLiteExecution method jmlcLinReg.
public static void jmlcLinReg() throws Exception {
Connection conn = getConfiguredConnection();
String linRegDS = conn.readScript("scripts/algorithms/LinearRegDS.dml");
PreparedScript linRegDSScript = conn.prepareScript(linRegDS, new String[] { "X", "y" }, new String[] { "beta_out" }, false);
double[][] trainData = new double[500][3];
for (int i = 0; i < 500; i++) {
double one = ThreadLocalRandom.current().nextDouble(0, 100);
double two = ThreadLocalRandom.current().nextDouble(0, 100);
double three = ThreadLocalRandom.current().nextDouble(0, 100);
double[] row = new double[] { one, two, three };
trainData[i] = row;
}
linRegDSScript.setMatrix("X", trainData);
log.debug(displayMatrix(trainData));
double[][] trainLabels = new double[500][1];
for (int i = 0; i < 500; i++) {
double one = ThreadLocalRandom.current().nextDouble(0, 100);
double[] row = new double[] { one };
trainLabels[i] = row;
}
linRegDSScript.setMatrix("y", trainLabels);
log.debug(displayMatrix(trainLabels));
ResultVariables linRegDSResults = linRegDSScript.executeScript();
double[][] dsBetas = linRegDSResults.getMatrix("beta_out");
log.debug("DS BETAS:");
log.debug(displayMatrix(dsBetas));
String linRegCG = conn.readScript("scripts/algorithms/LinearRegCG.dml");
PreparedScript linRegCGScript = conn.prepareScript(linRegCG, new String[] { "X", "y" }, new String[] { "beta_out" }, false);
linRegCGScript.setMatrix("X", trainData);
linRegCGScript.setMatrix("y", trainLabels);
ResultVariables linRegCGResults = linRegCGScript.executeScript();
double[][] cgBetas = linRegCGResults.getMatrix("beta_out");
log.debug("CG BETAS:");
log.debug(displayMatrix(cgBetas));
String glmPredict = conn.readScript("scripts/algorithms/GLM-predict.dml");
PreparedScript glmPredictScript = conn.prepareScript(glmPredict, new String[] { "X", "Y", "B_full" }, new String[] { "means" }, false);
double[][] testData = new double[500][3];
for (int i = 0; i < 500; i++) {
double one = ThreadLocalRandom.current().nextDouble(0, 100);
double two = ThreadLocalRandom.current().nextDouble(0, 100);
double three = ThreadLocalRandom.current().nextDouble(0, 100);
double[] row = new double[] { one, two, three };
testData[i] = row;
}
glmPredictScript.setMatrix("X", testData);
double[][] testLabels = new double[500][1];
for (int i = 0; i < 500; i++) {
double one = ThreadLocalRandom.current().nextDouble(0, 100);
double[] row = new double[] { one };
testLabels[i] = row;
}
glmPredictScript.setMatrix("Y", testLabels);
glmPredictScript.setMatrix("B_full", cgBetas);
ResultVariables glmPredictResults = glmPredictScript.executeScript();
double[][] means = glmPredictResults.getMatrix("means");
log.debug("GLM PREDICT MEANS:");
log.debug(displayMatrix(means));
conn.close();
}
Aggregations