如何在Theano中创建多对角方阵?

问题描述 投票:0回答:1

与以下方法相比,是否有更好的方法在theano中创建多对角方阵,

A = theano.tensor.nlinalg.AllocDiag(offset=0)(x)
A += theano.tensor.nlinalg.AllocDiag(offset=1)(x[:-1])
A += theano.tensor.nlinalg.AllocDiag(offset=-1)(x[1:])

x在对角线上我想要的矢量?每次调用AllocDiag()()时,都会创建一个新的Apply节点,这会导致内存问题和效率低下。

我希望有一种类似于scipy的方法,其中向量列表可以通过相应的偏移量列表传递到函数中,请参阅https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.sparse.diags.html

非常感谢您的协助。

theano diagonal
1个回答
0
投票

不需要AllocDiag()()的一种方法是将theano.tensor.set_subtensor()A[range(n),range(n)]结合使用以获得对角线索引,其中An*n矩阵。类似于以下内容:

A = tt.set_subtensor(A0[range(n),range(n)], x)
A = tt.set_subtensor(A[range(n-1),range(1,n)], x[:-1])
A = tt.set_subtensor(A[range(1,n),range(n-1), x[1:])

其中A0是初始矩阵,例如零矩阵。

© www.soinside.com 2019 - 2024. All rights reserved.