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

Create a tensor variable of given length in Theano functions

例如:

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教程文档中写得很清楚。偷懒我。

我从 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 函数会将 0 作为 int8 类型,可以使用 numpy.int32(0) 将其转换为 32 位 0。