You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@systemml.apache.org by "Nakul Jindal (JIRA)" <ji...@apache.org> on 2016/04/12 10:32:25 UTC

[jira] [Comment Edited] (SYSTEMML-542) Investigate direct integration of SystemML (DML) with Apache Zeppelin

    [ https://issues.apache.org/jira/browse/SYSTEMML-542?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15182513#comment-15182513 ] 

Nakul Jindal edited comment on SYSTEMML-542 at 4/12/16 8:31 AM:
----------------------------------------------------------------

Here is a second crack at this.
This time, it works for Standalone and Spark mode.
Screenshots
[^spark_cell.png]
[^spark_cell_anotated.png]
[^spark_dml_cell.png]
[^spark_dml_cell_anotated.png]
[^spark_negloglik_cell.png]
[^spark_negloglik_cell_anotated.png]
[Instructions | https://gist.github.com/anonymous/6ab8c569b2360232e252]
Also inlined:

h2. To enable and run DML (standalone & spark) in Apache Zeppelin.

h3. Step 0
Git clone and build [systemml|https://github.com/apache/incubator-systemml]. 
{code}
mvn clean package -P distribution
{code}

h3. Step 1
>From your systemml source directory, install the systemml 0.10.0 jar to your local m2 repository.
{code}
mvn install:install-file -Dfile=target/systemml-0.10.0-incubating-SNAPSHOT-standalone.jar -DgroupId=org.apache.systemml -DartifactId=systemml -Dversion=0.10.0-incubating -Dpackaging=jar
{code}

h3. Step 2
Git clone the *spark_dml* branch from the zeppelin project fork at [nakul02/incubator-systemml|https://github.com/nakul02/incubator-zeppelin/tree/spark_dml]
{code}
git clone -b spark_dml https://github.com/nakul02/incubator-zeppelin.git
{code}

h3. Step 3
Build zeppelin. In the incubating-zeppelin root directory,
{code}
mvn package -DskipTests
{code}

h3. Step 4
Run zeppelin. In the incubating-zeppelin root directory, 
{code}
bin/zeppelin-daemon.sh restart
{code}

h3. Step 5
In a browser, navigate to *localhost:8080*
In a new cell, type

{code}
%dml
X = rand(rows=200, cols=1000, min=0, max=4, pdf="uniform", sparsity=1.0)
Y = rand(rows=1000, cols=100, min=0, max=4, pdf="uniform", sparsity=1.0)
A = rand(rows=100, cols=1000, min=0, max=4, pdf="uniform", sparsity=1.0)
B = rand(rows=1000, cols=200, min=0, max=4, pdf="uniform", sparsity=1.0)
Q [- (X %_% Y) %_% (A %*% B)
q <- castAsScalar(Q[1,1])
print ("Q[1,1] is " + q)
{code}

and press the "play" button on the top right corner of the cell.

Your output should look like 

{code}
Q[1,1] is 1.5272079204925272E9
{code}
h3. Step 6
Download the data set from [SNAP@Stanford|http://snap.stanford.edu/data/amazon0601.html] and unzip/untar it. You should get a file called *amazon0601.txt*.
To try out the Spark DML, in the first cell, paste this Spark code and "run" the cell:
{code}
%spark

val baseDir = "/path/to/file"
val dataPath = baseDir + "/amazon0601.txt"

val X_train = sc.textFile(dataPath)
.filter(!_.startsWith("#"))
.map(_.split("\t") match { case Array(prod1, prod2) =] (prod1.toInt, prod2.toInt, 1.0)})
.toDF("prod_i", "prod_j", "x_ij")
.filter("prod_i [ 500 AND prod_j < 500") // Change to smaller number
.cache()

val max_prod_i = X_train.select(max("prod_i")).first.getInt(0)
val max_prod_j = X_train.select(max("prod_i")).first.getInt(0)
val numProducts = math.max(max_prod_i, max_prod_j) + 1 // 0-based indexing

z.put("X", X_train)
{code}
Change the value of "baseDir" appropriately.

h3. Step 7
In the next cell on the same notebook, type the following DML code and hit run:
{code}
%spark.dml

# data & args
X = read($zX)
X = X+1 # change product IDs to be 1-based, rather than 0-based
V = table(X[,1], X[,2])
size = 100
if(size ] -1) {
V = V[1:size,1:size]
}
max_iteration = 100
rank = 10

# run PNMF
n = nrow(V)
m = ncol(V)

range = 0.01
W = Rand(rows=n, cols=rank, min=0, max=range, pdf="uniform")
H = Rand(rows=rank, cols=m, min=0, max=range, pdf="uniform")

i=0
while(i < max_iteration) {
H = (H _ (t(W) %_% (V/(W%*%H))))/t(colSums(W))
W = (W _ ((V/(W%_%H)) %*% t(H)))/t(rowSums(H))
i = i + 1;
}

# compute negative log-likelihood
negloglik = -1 _ (sum(V_log(W%_%H)) - as.scalar(colSums(W)%_%rowSums(H)))

# write outputs
negloglik = matrix(negloglik, rows=1, cols=1)
write(negloglik, $znegloglikout)
write(W, $zWout)
write(H, $zHout)
{code}
In this code, read and write statements are interpreted differently if they refer to a command line expression.
X = read($zX) reads in a DataFrame object called "X" which was saved in the spark program. "$z" is a special prefix.
write(negloglik, $znegloglikout) saves a DataFrame object called "negloglikout". Again, "$z" is a special prefix. This and two other DataFrame objects are available in subsequent Spark cells.

h3. Step 8
To print the value of "negloglik", paste this code and hit run:
{code}
%spark

val n = z.get( "negloglikout")
n.asInstanceOf[org.apache.spark.sql.DataFrame].show
{code}

Of course, negloglikout could have also been printed from within DML, but this cell was to show the flow of DataFrames into and out of "%spark.dml" cells.


was (Author: nakul02):
Here is a second crack at this.
This time, it works for Standalone and Spark mode.
Screenshots
[^spark_cell.png]
[^spark_cell_anotated.png]
[^spark_dml_cell.png]
[^spark_dml_cell_anotated.png]
[^spark_negloglik_cell.png]
[^spark_negloglik_cell_anotated.png]
[Instructions | https://gist.github.com/anonymous/6ab8c569b2360232e252]
Also inlined:

h2. To enable and run DML (standalone & spark) in Apache Zeppelin.

h3. Step 0
Git clone and build [systemml|https://github.com/apache/incubator-systemml]. 
{code}
mvn clean package -P distribution
{code}

h3. Step 1
>From your systemml source directory, install the systemml 0.10.0 jar to your local m2 repository.
{code}
mvn install:install-file -Dfile=target/systemml-0.10.0-incubating-SNAPSHOT-standalone.jar -DgroupId=org.apache.systemml -DartifactId=systemml -Dversion=0.10.0-incubating -Dpackaging=jar
{code}

h3. Step 2
Git clone the *spark_dml* branch from the zeppelin project fork at [nakul02/incubator-systemml|https://github.com/nakul02/incubator-zeppelin/tree/spark_dml]
{code}
git clone -b spark_dml https://github.com/nakul02/incubator-zeppelin.git
{code}

h3. Step 3
Build zeppelin. In the incubating-zeppelin root directory,
{code}
mvn package -DskipTests
{code}

h3. Step 4
Run zeppelin. In the incubating-zeppelin root directory, 
{code}
bin/zeppelin-daemon.sh restart
{code}

h3. Step 5
In a browser, navigate to *localhost:8080*
In a new cell, type

{code}
%dml
X = rand(rows=200, cols=1000, min=0, max=4, pdf="uniform", sparsity=1.0)
Y = rand(rows=1000, cols=100, min=0, max=4, pdf="uniform", sparsity=1.0)
A = rand(rows=100, cols=1000, min=0, max=4, pdf="uniform", sparsity=1.0)
B = rand(rows=1000, cols=200, min=0, max=4, pdf="uniform", sparsity=1.0)
Q [- (X %_% Y) %_% (A %*% B)
q <- castAsScalar(Q[1,1])
print ("Q[1,1] is " + q)
{code}

and press the "play" button on the top right corner of the cell.

Your output should look like 

{code}
Q[1,1] is 1.5272079204925272E9
{code}
h3. Step 6
Download the data set from [SNAP@Stanford|http://snap.stanford.edu/data/amazon0601.html] and unzip/untar it. You should get a file called *amazon0601.txt*.
To try out the Spark DML, in the first cell, paste this Spark code and "run" the cell:
{code}
%spark

val baseDir = "/path/to/file"
val dataPath = baseDir + "/amazon0601.txt"

val X_train = sc.textFile(dataPath)
.filter(!_.startsWith("#"))
.map(_.split("\t") match { case Array(prod1, prod2) =] (prod1.toInt, prod2.toInt, 1.0)})
.toDF("prod_i", "prod_j", "x_ij")
.filter("prod_i [ 500 AND prod_j < 500") // Change to smaller number
.cache()

val max_prod_i = X_train.select(max("prod_i")).first.getInt(0)
val max_prod_j = X_train.select(max("prod_i")).first.getInt(0)
val numProducts = math.max(max_prod_i, max_prod_j) + 1 // 0-based indexing

z.put("X", X_train)
{code}
Change the value of "baseDir" appropriately.

h3. Step 7
In the next cell on the same notebook, type the following DML code and hit run:
{code}
%spark.dml

h1. data & args
X = read($zX)
X = X+1 # change product IDs to be 1-based, rather than 0-based
V = table(X[,1], X[,2])
size = 100
if(size ] -1) {
V = V[1:size,1:size]
}
max_iteration = 100
rank = 10

h1. run PNMF
n = nrow(V)
m = ncol(V)

range = 0.01
W = Rand(rows=n, cols=rank, min=0, max=range, pdf="uniform")
H = Rand(rows=rank, cols=m, min=0, max=range, pdf="uniform")

i=0
while(i < max_iteration) {
H = (H _ (t(W) %_% (V/(W%*%H))))/t(colSums(W))
W = (W _ ((V/(W%_%H)) %*% t(H)))/t(rowSums(H))
i = i + 1;
}

h1. compute negative log-likelihood
negloglik = -1 _ (sum(V_log(W%_%H)) - as.scalar(colSums(W)%_%rowSums(H)))

h1. write outputs
negloglik = matrix(negloglik, rows=1, cols=1)
write(negloglik, $znegloglikout)
write(W, $zWout)
write(H, $zHout)
{code}
In this code, read and write statements are interpreted differently if they refer to a command line expression.
X = read($zX) reads in a DataFrame object called "X" which was saved in the spark program. "$z" is a special prefix.
write(negloglik, $znegloglikout) saves a DataFrame object called "negloglikout". Again, "$z" is a special prefix. This and two other DataFrame objects are available in subsequent Spark cells.

h3. Step 8
To print the value of "negloglik", paste this code and hit run:
{code}
%spark

val n = z.get( "negloglikout")
n.asInstanceOf[org.apache.spark.sql.DataFrame].show
{code}

Of course, negloglikout could have also been printed from within DML, but this cell was to show the flow of DataFrames into and out of "%spark.dml" cells.

> Investigate direct integration of SystemML (DML) with Apache Zeppelin
> ---------------------------------------------------------------------
>
>                 Key: SYSTEMML-542
>                 URL: https://issues.apache.org/jira/browse/SYSTEMML-542
>             Project: SystemML
>          Issue Type: New Feature
>            Reporter: Nakul Jindal
>            Priority: Minor
>         Attachments: phase2_other_spark_cells.png, phase2_other_spark_cells_annotated.png, phase2_spark_and_dml_cell.png, poc-single-node-dml-on-zeppelin.png, spark_cell.png, spark_cell_anotated.png, spark_dml_cell.png, spark_dml_cell_anotated.png, spark_negloglik_cell.png, spark_negloglik_cell_anotated.png
>
>




--
This message was sent by Atlassian JIRA
(v6.3.4#6332)