Apache SystemML DML 不允许在函数中使用多个 return 值
Apache SystemML DML doesn't allow for multiple return values in function
我正在尝试在 SystemlML 的 DML 中构建一个简单的 hello world 神经网络,但无法从 UDF 函数返回多个值。我受到 this code 成功运行的启发,但我无法弄清楚其中的区别:
根据 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
有效,否则我得到:
Caused by: org.apache.sysml.parser.LanguageException: ERROR: null --
line 22, column 0 -- Assignment statement cannot return multiple
values
有什么想法吗?
SystemML 支持函数中的多个 return 值。参见 http://apache.github.io/systemml/dml-language-reference.html#user-defined-function-udf
以下 Python 示例 returns 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()
您的代码段未显示 "feedforward" 的调用。可以吗post?
您对 "feedForward" 的调用不正确,因为您 return 输出。更改为类似的内容:
[Yhat1, Yhat2]=feedForward(X,W1,W2)
我正在尝试在 SystemlML 的 DML 中构建一个简单的 hello world 神经网络,但无法从 UDF 函数返回多个值。我受到 this code 成功运行的启发,但我无法弄清楚其中的区别:
根据 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
有效,否则我得到:
Caused by: org.apache.sysml.parser.LanguageException: ERROR: null -- line 22, column 0 -- Assignment statement cannot return multiple values
有什么想法吗?
SystemML 支持函数中的多个 return 值。参见 http://apache.github.io/systemml/dml-language-reference.html#user-defined-function-udf
以下 Python 示例 returns 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()
您的代码段未显示 "feedforward" 的调用。可以吗post?
您对 "feedForward" 的调用不正确,因为您 return 输出。更改为类似的内容:
[Yhat1, Yhat2]=feedForward(X,W1,W2)