use of org.apache.sysml.api.mlcontext.BinaryBlockMatrix in project incubator-systemml by apache.
the class MLContextOutputBlocksizeTest method runMLContextOutputBlocksizeTest.
private void runMLContextOutputBlocksizeTest(String format) {
try {
double[][] A = getRandomMatrix(rows, cols, -10, 10, sparsity, 76543);
MatrixBlock mbA = DataConverter.convertToMatrixBlock(A);
int blksz = ConfigurationManager.getBlocksize();
MatrixCharacteristics mc = new MatrixCharacteristics(rows, cols, blksz, blksz, mbA.getNonZeros());
//create input dataset
JavaPairRDD<MatrixIndexes, MatrixBlock> in = SparkExecutionContext.toMatrixJavaPairRDD(sc, mbA, blksz, blksz);
BinaryBlockMatrix bbmatrix = new BinaryBlockMatrix(in, mc);
ml.setExplain(true);
ml.setExplainLevel(ExplainLevel.HOPS);
//execute script
String s = "if( sum(X) > 0 )" + " X = X/2;" + "R = X;" + "write(R, \"/tmp\", format=\"" + format + "\");";
Script script = dml(s).in("X", bbmatrix).out("R");
MLResults results = ml.execute(script);
//compare output matrix characteristics
MatrixCharacteristics mcOut = results.getMatrix("R").getMatrixMetadata().asMatrixCharacteristics();
Assert.assertEquals(blksz, mcOut.getRowsPerBlock());
Assert.assertEquals(blksz, mcOut.getColsPerBlock());
} catch (Exception ex) {
ex.printStackTrace();
throw new RuntimeException(ex);
}
}
use of org.apache.sysml.api.mlcontext.BinaryBlockMatrix in project incubator-systemml by apache.
the class MLContextTest method testOutputBinaryBlockMatrixPYDML.
@Test
public void testOutputBinaryBlockMatrixPYDML() {
System.out.println("MLContextTest - output BinaryBlockMatrix PYDML");
String s = "M = full('1 2 3 4', rows=2, cols=2);";
BinaryBlockMatrix binaryBlockMatrix = ml.execute(pydml(s).out("M")).getBinaryBlockMatrix("M");
JavaRDD<String> javaRDDStringIJV = MLContextConversionUtil.binaryBlockMatrixToJavaRDDStringIJV(binaryBlockMatrix);
List<String> lines = javaRDDStringIJV.collect();
Assert.assertEquals("1 1 1.0", lines.get(0));
Assert.assertEquals("1 2 2.0", lines.get(1));
Assert.assertEquals("2 1 3.0", lines.get(2));
Assert.assertEquals("2 2 4.0", lines.get(3));
}
use of org.apache.sysml.api.mlcontext.BinaryBlockMatrix in project incubator-systemml by apache.
the class MLContextTest method testInputBinaryBlockMatrixPYDML.
@Test
public void testInputBinaryBlockMatrixPYDML() {
System.out.println("MLContextTest - input BinaryBlockMatrix PYDML");
List<String> list = new ArrayList<String>();
list.add("10,20,30");
list.add("40,50,60");
list.add("70,80,90");
JavaRDD<String> javaRddString = sc.parallelize(list);
JavaRDD<Row> javaRddRow = javaRddString.map(new CommaSeparatedValueStringToRow());
List<StructField> fields = new ArrayList<StructField>();
fields.add(DataTypes.createStructField("C1", DataTypes.StringType, true));
fields.add(DataTypes.createStructField("C2", DataTypes.StringType, true));
fields.add(DataTypes.createStructField("C3", DataTypes.StringType, true));
StructType schema = DataTypes.createStructType(fields);
Dataset<Row> dataFrame = spark.createDataFrame(javaRddRow, schema);
BinaryBlockMatrix binaryBlockMatrix = new BinaryBlockMatrix(dataFrame);
Script script = pydml("avg = avg(M)").in("M", binaryBlockMatrix).out("avg");
double avg = ml.execute(script).getDouble("avg");
Assert.assertEquals(50.0, avg, 0.0);
}
use of org.apache.sysml.api.mlcontext.BinaryBlockMatrix in project incubator-systemml by apache.
the class MLContextTest method testInputBinaryBlockMatrixDML.
@Test
public void testInputBinaryBlockMatrixDML() {
System.out.println("MLContextTest - input BinaryBlockMatrix DML");
List<String> list = new ArrayList<String>();
list.add("10,20,30");
list.add("40,50,60");
list.add("70,80,90");
JavaRDD<String> javaRddString = sc.parallelize(list);
JavaRDD<Row> javaRddRow = javaRddString.map(new CommaSeparatedValueStringToRow());
List<StructField> fields = new ArrayList<StructField>();
fields.add(DataTypes.createStructField("C1", DataTypes.StringType, true));
fields.add(DataTypes.createStructField("C2", DataTypes.StringType, true));
fields.add(DataTypes.createStructField("C3", DataTypes.StringType, true));
StructType schema = DataTypes.createStructType(fields);
Dataset<Row> dataFrame = spark.createDataFrame(javaRddRow, schema);
BinaryBlockMatrix binaryBlockMatrix = new BinaryBlockMatrix(dataFrame);
Script script = dml("avg = avg(M);").in("M", binaryBlockMatrix).out("avg");
double avg = ml.execute(script).getDouble("avg");
Assert.assertEquals(50.0, avg, 0.0);
}
use of org.apache.sysml.api.mlcontext.BinaryBlockMatrix in project incubator-systemml by apache.
the class MLContextTest method testOutputBinaryBlockMatrixDML.
@Test
public void testOutputBinaryBlockMatrixDML() {
System.out.println("MLContextTest - output BinaryBlockMatrix DML");
String s = "M = matrix('1 2 3 4', rows=2, cols=2);";
BinaryBlockMatrix binaryBlockMatrix = ml.execute(dml(s).out("M")).getBinaryBlockMatrix("M");
JavaRDD<String> javaRDDStringIJV = MLContextConversionUtil.binaryBlockMatrixToJavaRDDStringIJV(binaryBlockMatrix);
List<String> lines = javaRDDStringIJV.collect();
Assert.assertEquals("1 1 1.0", lines.get(0));
Assert.assertEquals("1 2 2.0", lines.get(1));
Assert.assertEquals("2 1 3.0", lines.get(2));
Assert.assertEquals("2 2 4.0", lines.get(3));
}
Aggregations