如何使用 Theano 形成复合函数?
How can I form a composite Function with Theano?
我想用 Theano 计算复合函数 f(x, g(x))。不幸的是,当我尝试编写一个函数组合时,Python 抱怨类型错误。例如,考虑以下简单脚本:
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()
Pythonreturns时出现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)
我想用 Theano 计算复合函数 f(x, g(x))。不幸的是,当我尝试编写一个函数组合时,Python 抱怨类型错误。例如,考虑以下简单脚本:
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()
Pythonreturns时出现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)