简体   繁体   English

如何使用 Theano 形成复合函数?

[英]How can I form a composite Function with Theano?

I would like to compute a composite function f(x, g(x)) with Theano.我想用 Theano 计算一个复合函数 f(x, g(x))。 Unfortunately, when I try to code a function composition, Python complains about a TypeError.不幸的是,当我尝试编写函数组合时,Python 会抱怨 TypeError。 For example, consider the following simple script:例如,考虑以下简单脚本:

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)

When writing funComp = composition() Python returns a TypeError:编写funComp = composition() Python 会返回 TypeError:

TypeError: unsupported operand type(s) for *: 'TensorVariable' and 'Function' 

However, I can compile and calculate the function fun = f() .但是,我可以编译和计算函数fun = f() Is there a way to successfully establish a function composition?有没有办法成功建立功能组合? I am grateful for any help!我很感激任何帮助!

You don't need multiple function actually for this case.对于这种情况,您实际上不需要多个功能。 This one works well.这个效果很好。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM