我有如下所示的简单代码:
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/