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

标签 python theano composition

我想用 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() 时,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)

关于python - 如何使用 Theano 形成复合函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36841330/

相关文章:

java - 重写给定的类以使用组合而不是继承

c++ - 这有什么问题吗?包含 Setters/Getters(已编辑)头文件

java - 与实体不直接相关的属性的最佳设计方法是什么?

python - celery.utils.log.ProcessAwareLoggerobject 在 logging.Logger.manager.loggerDict 中做什么

python - 如何使用 python Hook Windows 中的事件/消息

python - 如何更改保存图片的目录,scrapy

python - 是否可以使用 XGBoost 预测给定输入向量或一系列向量的整个输出向量?

python - 当 input_shape 指定为 3-d 时,Keras SimpleRNN 出错

nlp - 在 Keras 中实现 word2vec

python - 在 TensorFlow 中展平张量的最后两个维度