如何在Theano中形成复合函数?

2024-09-28 23:41:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用no计算一个复合函数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() 时,Python返回一个TypeError:

^{pr2}$

但是,我可以编译和计算函数 fun = f() 。有没有办法成功地建立一个功能组合?我很感激你的帮助!在


Tags: 函数noimport类型inputreturndef错误
1条回答
网友
1楼 · 发布于 2024-09-28 23:41:22

在这种情况下,实际上并不需要多个函数。这个很好用。在

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)

相关问题 更多 >