gpt4 book ai didi

python - 如何将 theano.tensor 转换为 numpy.array?

转载 作者:太空狗 更新时间:2023-10-29 21:07:15 29 4
gpt4 key购买 nike

我有如下所示的简单代码:

class testxx(object):
def __init__(self, input):
self.input = input
self.output = T.sum(input)
a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype = np.float32)
classfier = testxx(a)
outxx = classfier.output
outxx = np.asarray(outxx, dtype = np.float32)

但是,我得到以下错误信息:

ValueError: setting an array element with a sequence.

此外,当我使用theano.tensor的函数时,它返回的似乎是所谓的“张量”,我不能简单地将其切换为numpy.array类型,即使结果应该是这样的一个矩阵。

这就是我的问题:如何将 outxx 切换为键入 numpy.array?

最佳答案

Theano“张量”变量是符号变量。你用它们构建的东西就像你编写的程序。你需要编译一个 Theano 函数来执行这个程序所做的事情。有两种编译 Theano 函数的方法:

f = theano.function([testxx.input], [outxx])
f_a1 = f(a)

# Or the combined computation/execution
f_a2 = outxx.eval({testxx.input: a})

当你编译一个 Theano 函数时,你必须告诉输入是什么,输出是什么。这就是为什么在调用 theano.function() 时有 2 个参数。 eval() 是一个接口(interface),它将在具有相应值的给定符号输入上编译和执行 Theano 函数。

关于python - 如何将 theano.tensor 转换为 numpy.array?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23643850/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com