如何在Theano中将几个输出变量组合在一起?

时间:2014-08-05 15:59:42

标签: python numerical-methods theano

我正在尝试在Theano中实现一个将矢量映射到矢量的函数,但输出矢量的每个维度都是手动指定的。如果我像这样创建Theano函数:

import theano
import theano.tensor as T
x = T.dvector('x')
dx = 28.0 * (x[1] - x[0])
dy = x[0] * (10.0 - x[1]) - x[2]
dz = x[0] * x[1] - 8.0/3/0 * x[2]
f = theano.function([x],[dx,dy,dz])

然后f([1,2,3])[array(10.0), array(23.0), array(-6.0)]作为输出,当我希望它返回array([10.0, 23.0, -6.0])时。什么是Theanic的做法?

2 个答案:

答案 0 :(得分:3)

Kyle Kastner的另一个答案会奏效,但是你可以让Theano为你做那个(我把你的例子中的除法修正为0):

import theano
import theano.tensor as T
x = T.dvector('x')
dx = 28.0 * (x[1] - x[0])
dy = x[0] * (10.0 - x[1]) - x[2]
dz = x[0] * x[1] - 8.0/3.0 * x[2]
o = T.as_tensor_variable([dx,dy,dz])
f = theano.function([x],o)
f([1,2,3])
# output array([ 28.,   5.,  -6.])

答案 1 :(得分:1)

函数的输出只是一个numpy数组的列表 - 你可以np.array(f([1, 2, 3]))将输出列表转换为numpy向量。