Romeo Kienzler
Romeo Kienzler

Reputation: 3537

Apache SystemML DML doesn't allow for multiple return values in function

I'm trying to build a simple hello world neural network in SystemlML's DML but got stuck with returning multiple values from a UDF function. I've been inspired by this code where it successfully runs, but I can't figure out the difference:

EDIT as requested by Berthold (full code):

script = """

#
sigmoid = function(matrix[double] z) return (matrix[double] z) {
    z = 1/(1+exp(-z))
}


sigmoidPrime = function(matrix[double] z) return (matrix[double] z) {
        #Gradient of sigmoid
        z = exp(-z)/(1+exp(-z))
}

X=matrix("3 5 5 1 10 2", rows=3, cols=2) 
inputLayerSize = 2
outputLayerSize = 1
hiddenLayerSize = 3

W1 = rand(rows=inputLayerSize,cols=hiddenLayerSize)
W2 = rand(rows=hiddenLayerSize,cols=outputLayerSize)

feedForward = function (matrix[double] X,
                        matrix[double] W1,
                        matrix[double] W2) return (matrix[double] z3,matrix[double] Y) {
    z2 =  X %*% W1
    a2 =  sigmoid(z2)
    z3 = (a2 %*% W2)
    Y = sigmoid(z3)
}

#feedForward = function (matrix[double] X,
#                        matrix[double] W1,
#                        matrix[double] W2) return (matrix[double] z2,matrix[double] z3,matrix[double] Y) {
#    z2 =  X %*% W1
#    a2 =  sigmoid(z2)
#    z3 = a2 %*% W2
#    Y = sigmoid(z3)
#    z2,z3,Y
#}

#gradient = function(matrix[double] X,
#                        matrix[double] W1,
#                        matrix[double] W2,
#                        matrix[double] Y) return (matrix[double] Y) {
#    #Compute derivative with respect to W and W2 for a given X and y:
#    z2,z3,yHat = feedForward(X,W1,W2)

#    delta3 = -(Y-yHat) * sigmoidPrime(z3)
#    dJdW2 = t(a2) %*% delta3

#    delta2 = (delta3 %*% t(W2))*sigmoidPrime(z2)
#    dJdW1 = t(X) %*% delta2  

#    return dJdW1, dJdW2
#}

Yhat=feedForward(X,W1,W2)
nrx = nrow(X)
ncx = ncol(X)
nrw1 = nrow(W1)
ncw1 = ncol(W1)
"""

If I remove

matrix[double] z3

it works, otherwise I get:

Caused by: org.apache.sysml.parser.LanguageException: ERROR: null -- line 22, column 0 -- Assignment statement cannot return multiple values

Any ideas?

Upvotes: 0

Views: 42

Answers (2)

Berthold Reinwald
Berthold Reinwald

Reputation: 86

Your invocation of "feedForward" is incorrect as you return to outputs. Change to something like that:

[Yhat1, Yhat2]=feedForward(X,W1,W2)

Upvotes: 0

Berthold Reinwald
Berthold Reinwald

Reputation: 86

SystemML does support multiple return values in functions. See http://apache.github.io/systemml/dml-language-reference.html#user-defined-function-udf

Below Python example returns 2 matrixes.

DMLstr = """
M1M2 = function( matrix[double] M) 
           return (matrix[double] M1, 
                   matrix[double] M2) {
    M1 = M + 1
    M2 = M + 2
}

X=matrix("3 5 5 1 10 2", rows=3, cols=2) 
[M1, M2] = M1M2 (X)
"""

[M1, M2] = ml.execute(dml(DMLstr).output('M1', 'M2')).get('M1','M2')

print M1.toNumPy()
print M2.toNumPy()

Your snippet does not show the invocation of "feedforward". Can you please post?

Upvotes: 1

Related Questions