如何在Python中处理大矩阵和矩阵乘法
我正在尝试执行具有以下方案的矩阵乘法:
C = np.dot(np.dot(sparse.csr_matrix(np.double(A).transpose()),sparse.spdiags(B,0,Ngrid,Ngrid)), sparse.csr_matrix(np.double(A)))
因此,我想转置矩阵A,该矩阵A导致n x m矩阵带有m>> n,并用对角矩阵乘以m x m x m矩阵。 b是“主角”。所得矩阵(N x M)应乘以矩阵A(M x N)并导致N x n矩阵C。
出现错误是以下内容:
<2000x921600 sparse matrix of type '<class 'numpy.float64'>'
with 1843066024 stored elements in Compressed Sparse Row format>
由于最终矩阵为n x n,我想拥有此 错误矩阵作为numpy数组。您如何看待,我试图将矩阵在中间制作,因为毫无用处的对角矩阵。但是,我无法理解为什么Python需要使用1843066024元素进行这种疯狂的大型矩阵才能进行乘法。
您是否有一些想法和/或解释为什么会出现此问题?
I'm trying to execute a matrix multiplication which has the following scheme:
C = np.dot(np.dot(sparse.csr_matrix(np.double(A).transpose()),sparse.spdiags(B,0,Ngrid,Ngrid)), sparse.csr_matrix(np.double(A)))
Thus, I want to transpose matrix A, which lead to a N x M matrix with M>>N and multiply with the diagonal matrix which is a M x M matrix. B is the „main diagonal“. The resulting matrix (N x M) should be multiplied with matrix A (M x N) and lead to the N x N matrix C.
The error appears is the following:
<2000x921600 sparse matrix of type '<class 'numpy.float64'>'
with 1843066024 stored elements in Compressed Sparse Row format>
As the final matrix is N x N, I want to have this matrix as a numpy array. How you see, I try to make the matrix in the mid as a sparsity diagonal matrix which works well. But, I cant understand why Python need this insane large matrix with 1843066024 elements to conduct the multiplication.
Do you have some ideas and/or explanation why this problem appears?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果
b
是对角线,则无需使用稀疏来保存内存。您可以使用1D阵列进行计算,只有对角值。我将用较小的维度演示,其中制作完整的
b
不会引起问题。其他人可以用较大的维度进行测试。双重matmul:
等效
einsum
:b
的1D对角线:广播的乘法与Matmul相同的事情:
用
einsum
表达它:一些比较时间:
对于
einsum
“冷凝”版本更快。使用matmul
完整的对角线略快。但是,在大型数组中创建完整的
b
可能会有问题,使用b
可能会更快。另外,它在其他方面也观察到,因此由于记忆力更好,较小阵列上的迭代速度可以更快。If
B
is diagonal, you don't need to use sparse to save memory. You can do the calculation with a 1d array, just the diagonal values.I'll demonstrate with small dimensions, where making a full
B
is doesn't cause problems. Others can test this with large dimensions.The double matmul:
The equivalent
einsum
:The 1d diagonal of
B
:A broadcasted multiplication does the same thing as the matmul:
Expressing that with
einsum
:Some comparative times:
For
einsum
the 'condensed' version is faster. Withmatmul
the full diagonal is marginally faster.But with large arrays where creating a full
B
might a problem, usingb
might be faster. Also it's been observed in other SO that iterations on smaller arrays can be faster, due to better memory handling.您正在这样做...过于复杂。这是
m&gt;&gt; n
(您对此不一致)。c
是您想要的数组。您的中间产品都不是m x m
。如果您仍然存在内存问题,则需要获得更多的内存,或者需要将问题切成m
轴上的较小零件,然后将它们计算为分段。You're doing this... overly complicated. Here's a straightforward path for
M >> N
(you're inconsistent on that).C
is then the array that you want. None of your intermediate products areM x M
. If you still have memory problems, you either need to get more memory, or you need to chunk your problem into smaller pieces on yourm
axis and calculate them piecewise.