我有如下所示的简单代码:
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.
此外,当我使用ano.tensor函数时,它返回的似乎是“tensor”,而且我不能简单地将它切换到numpy.array类型,即使结果的形状应该像矩阵一样。
所以这就是我的问题:如何将outxx切换到numpy.array类型?
因为
testxx
使用sum()
fromtheano.tensor
,而不是numpy
,所以它可能需要TensorVariable
作为输入,而不是numpy数组。=>;用
a = T.matrix(dtype=theano.config.floatX)
替换a = np.array(...)
。在最后一行之前,
outxx
将是一个依赖于a
的TensorVariable
。所以可以通过给出a
的值来计算它。=>;将最后一行
outxx = np.asarray(...)
替换为以下两行。以下代码运行时没有错误。
关于adding scalars的no文档提供了其他类似的示例。
无“张量”变量是符号变量。你用它们构建的东西就像你编写的程序。您需要编译一个Theano函数来执行这个程序的功能。编译Theano函数有两种方法:
编译Theano函数时,必须知道输入是什么,输出是什么。这就是调用theano.function()时有2个参数的原因。eval()是一个接口,它将编译给定符号输入上具有相应值的Theano函数并对其执行。
相关问题 更多 >
编程相关推荐