在Theano函数中创建给定长度的张量变量

时间:2015-12-12 07:58:06

标签: numpy parallel-processing theano

例如:

x = tensor.scalar()
ret = give_me_zeros_of_length(x)
my_zeros = theano.function(inputs=[x], outputs=ret)
my_zeros(4)  # should get [0 0 0 0]

Theano中 give_me_zeros_of_length 的最佳方法是什么?
提前谢谢。

编辑:
所以我已经回答了我自己的问题,答案是在Theano教程文档中清楚地写出来的。懒惰我。

1 个答案:

答案 0 :(得分:0)

我从here

找到了tensor.alloc()函数
x = tensor.scalar(dtype="int32")
ret = tensor.alloc(0, x)
my_zeros = theano.function(inputs=[x], outputs=ret)
my_zeros(5)  # gives [0 0 0 0 0]

请注意,默认情况下,alloc函数在int8类型中将为0,可以使用numpy.int32(0)将其转换为32位0。