0

我正在尝试在 SystemlML 的 DML 中构建一个简单的 hello world 神经网络,但遇到了从 UDF 函数返回多个值的问题。我受到成功运行的这段代码的启发,但我无法弄清楚其中的区别:

按照 Berthold 的要求进行编辑(完整代码):

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)
"""

如果我删除

matrix[double] z3

它有效,否则我得到:

原因:org.apache.sysml.parser.LanguageException: ERROR: null -- 第 22 行,第 0 列 -- 赋值语句不能返回多个值

有任何想法吗?

4

2 回答 2

1

SystemML 确实支持函数中的多个返回值。见http://apache.github.io/systemml/dml-language-reference.html#user-defined-function-udf

下面的 Python 示例返回 2 个矩阵。

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()

您的代码段未显示“前馈”的调用。你能发帖吗?

于 2017-07-11T17:43:00.420 回答
0

当您返回输出时,您对“feedForward”的调用不正确。改成这样的:

[Yhat1, Yhat2]=feedForward(X,W1,W2)
于 2017-07-11T18:19:51.053 回答