【发布时间】:2019-04-29 19:45:57
【问题描述】:
我试图找到一个非常稀疏的邻接矩阵的最大特征值。我已尝试使用我看到的所有可用方法:
mat = scipy.io.mmread(f)
mat = scipy.sparse.csr_matrix(mat)
G = nx.to_networkx_graph(mat)
mat = None
# compute largest eigenvalue
L = nx.normalized_laplacian_matrix(G)
# impl 1
e = numpy.linalg.eigvals(L.A)
# impl 2
e, _ = scipy.sparse.linalg.eigs(L.A, k=1, which='LA')
# impl 3
e, _ = scipy.sparse.linalg.eigs(L.A)
所有这三个实现在某些时候都会遇到类似的内存错误:
e, _ = scipy.sparse.linalg.eigs(L.A)
File "/usr/lib64/python3.7/site-packages/scipy/sparse/base.py", line 674, in __getattr__
return self.toarray()
File "/usr/lib64/python3.7/site-packages/scipy/sparse/compressed.py", line 947, in toarray
out = self._process_toarray_args(order, out)
File "/usr/lib64/python3.7/site-packages/scipy/sparse/base.py", line 1184, in _process_toarray_args
return np.zeros(self.shape, dtype=self.dtype, order=order)
MemoryError
Uncaught exception. Entering post mortem debugging
Running 'cont' or 'step' will restart the program
> /usr/lib64/python3.7/site packages/scipy/sparse/base.py(1184)_process_toarray_args()
-> return np.zeros(self.shape, dtype=self.dtype, order=order)
(Pdb) print(self.shape)
(14259278, 14259278)
在尝试生成一个 1.6PB 的 numpy 数组之后,大概是为了矩阵的密集表示。显然,我没有这方面的记忆。我确实有很多(128GB)。是否有一些不需要生成密集矩阵的实现或替代方案?它不一定是 Python。
【问题讨论】:
-
你为什么一直使用
.A? -
L.A产生一个密集的,因此非常大的 numpy 数组。numpy.linalg.eigvals很可能需要这个,但其他人需要吗?
标签: python numpy scipy sparse-matrix eigenvalue