[英]How can I form a composite Function with Theano?
我想用 Theano 计算一个复合函数 f(x, g(x))。 不幸的是,当我尝试编写函数组合时,Python 会抱怨 TypeError。 例如,考虑以下简单脚本:
import theano
import theano.tensor as T
x = T.dscalar('x')
def g():
y1 = T.sqr(x)
return theano.function([x], y1)
def composition():
input = g()
yComp = x * input
return theano.function([x], yComp)
def f():
y1 = T.sqr(x)
yMult = x * y1
return theano.function([x], yMult)
编写funComp = composition()
Python 会返回 TypeError:
TypeError: unsupported operand type(s) for *: 'TensorVariable' and 'Function'
但是,我可以编译和计算函数fun = f()
。 有没有办法成功建立功能组合? 我很感激任何帮助!
对于这种情况,您实际上不需要多个功能。 这个效果很好。
import theano
import theano.tensor as T
x = T.dscalar('x')
def g():
y1 = T.sqr(x)
return y1
def composition():
input = g()
yComp = x * input
return theano.function([x], yComp)
tfunc = composition()
print tfunc(4)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.