Theano堆栈矩阵以编程方式?

app*_*der 5 python theano

我有以下代码将2个矩阵堆叠成3D张量.

import theano
import theano.tensor as T
A = T.matrix("A")
B = theano.tensor.stack(A, A)
f = theano.function(inputs=[A], outputs=B)
print f([range(10)]*2)
Run Code Online (Sandbox Code Playgroud)

但是,我不知道我需要提前多少次堆叠矩阵.例如,第四行代码可能是:

B = theano.tensor.stack(A, A, A)
B = theano.tensor.stack(A, A, A, A)
etc...
Run Code Online (Sandbox Code Playgroud)

是否有一个theano函数复制矩阵n次:

theano.some_function(A, 3) = theano.tensor.stack(A, A, A)
Run Code Online (Sandbox Code Playgroud)

然后我可以传递那个3,作为theano函数f的参数.这可能吗?我研究了广播,但广播没有明确改变维度/堆栈.

jur*_*ure 1

我不了解 theano,但您可以使用列表理解和解包参数列表来完成此操作:

n = 5
B = theano.tensor.stack(*[A for dummy in range(n)])
Run Code Online (Sandbox Code Playgroud)

这相当于:

B = theano.tensor.stack(A, A, A, A, A)
Run Code Online (Sandbox Code Playgroud)

其作用是,它首先构造一个n包含 的副本的列表A,然后将该列表解压缩为单独的参数(请参阅https://docs.python.org/2/tutorial/controlflow.html#unpacking-argument-lists)。