在 theano 扫描中连接

Concatenate in theano scan

我无法弄清楚为什么以下代码不起作用:

import theano as th
import theano.tensor as tt

a = tt.vector("a")

res, up = th.scan(
    fn = lambda a : tt.concatenate([a,a]), 
    outputs_info = a,
    n_steps = 2
)

f = th.function(inputs = [a], outputs=res)

f(np.array([1.]))

我希望它 return 和

一样
f = th.function(
    inputs = [a], 
    outputs = tt.concatenate([tt.concatenate([a,a]),tt.concatenate([a,a])])
)

theano.scan 文档字符串中:

... the initial state should have the same shape as the output ...

如果您的扫描涉及向量上的循环表达式,则该向量的形状不得改变。

lambda a: T.sqr(a)  # OK

lambda a: T.concat([a,a])  # ERROR

因此,scan 在内部使用矩阵来存储所有时间步长的向量。如果形状发生变化,矩阵将变得参差不齐。虽然理论上并非不可能实施,但它引入了更多的复杂性和潜在问题。

所以,是的,scan 有点受限。