use of org.apache.sysml.api.jmlc.ResultVariables 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();
}
use of org.apache.sysml.api.jmlc.ResultVariables 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.ResultVariables in project systemml by apache.
the class FrameIndexingAppendTest method execDMLScriptviaJMLC.
private static ArrayList<String[][]> execDMLScriptviaJMLC(String testname, String[][] F1, String[][] M, boolean modelReuse) throws IOException {
Timing time = new Timing(true);
ArrayList<String[][]> ret = new ArrayList<String[][]>();
// establish connection to SystemML
Connection conn = new Connection();
try {
// prepare input arguments
HashMap<String, String> args = new HashMap<String, String>();
args.put("$TRANSFORM_SPEC1", "{ \"ids\": true ,\"recode\": [ 1, 2] }");
args.put("$TRANSFORM_SPEC2", "{ \"ids\": true ,\"recode\": [ 1] }");
// read and precompile script
String script = conn.readScript(SCRIPT_DIR + TEST_DIR + testname + ".dml");
PreparedScript pstmt = conn.prepareScript(script, args, new String[] { "F1", "M" }, new String[] { "F2" }, false);
if (modelReuse)
pstmt.setFrame("M", M, true);
// execute script multiple times
for (int i = 0; i < nRuns; i++) {
// bind input parameters
if (!modelReuse)
pstmt.setFrame("M", M);
pstmt.setFrame("F1", F1);
// execute script
ResultVariables rs = pstmt.executeScript();
// get output parameter
String[][] Y = rs.getFrame("F2");
// 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;
}
use of org.apache.sysml.api.jmlc.ResultVariables in project systemml by apache.
the class FrameTransformTest method execDMLScriptviaJMLC.
private static ArrayList<double[][]> execDMLScriptviaJMLC(String testname, String[][] X, String[][] M, boolean modelReuse) throws IOException {
Timing time = new Timing(true);
ArrayList<double[][]> ret = new ArrayList<double[][]>();
// establish connection to SystemML
Connection conn = new Connection();
try {
// prepare input arguments
HashMap<String, String> args = new HashMap<String, String>();
args.put("$TRANSFORM_SPEC", "{ \"ids\": true ,\"recode\": [ 1, 2, 3] }");
// read and precompile script
String script = conn.readScript(SCRIPT_DIR + TEST_DIR + testname + ".dml");
PreparedScript pstmt = conn.prepareScript(script, args, new String[] { "X", "M" }, new String[] { "Y" }, false);
if (modelReuse)
pstmt.setFrame("M", M, true);
// execute script multiple times
for (int i = 0; i < nRuns; i++) {
// bind input parameters
if (!modelReuse)
pstmt.setFrame("M", M);
pstmt.setFrame("X", X);
// execute script
ResultVariables rs = pstmt.executeScript();
// get output parameter
double[][] Y = rs.getMatrix("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;
}
use of org.apache.sysml.api.jmlc.ResultVariables in project 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;
}
Aggregations