假设有以下情况。我得到一个双模网络的邻接矩阵,其中一个维度代表一些项目(帖子),另一个维度代表每个项目下的标签。现在我想把这个二模网络进行折叠,以得到一个项目与项目关系的单模网络,其中每个链接的值代表两个项目的共享标签数。这可以通过一个简单的矩阵乘法达到,如下图。
或者在代码中:
from scipy.sparse import csr_matrix, save_npz, load_npz
# load matrix
tpm = csr_matrix(load_npz('tag_post_matrix.npz'))
# compute dot product
cn = tpm.transpose().dot(tpm)
# save result
save_npz('content_network_abs.npz', cn)
在运行一段时间后会出现这个错误。
---------------------------------------------------------------------------
MemoryError Traceback (most recent call last)
<ipython-input-27-10ff98c2505a> in <module>()
----> 1 cn = tpm.transpose().dot(tpm)
2 save_npz(expand('content_network_abs.npz'), cn)
3
/opt/anaconda/lib/python3.7/site-packages/scipy/sparse/base.py in dot(self, other)
359
360 """
--> 361 return self * other
362
363 def power(self, n, dtype=None):
/opt/anaconda/lib/python3.7/site-packages/scipy/sparse/base.py in __mul__(self, other)
477 if self.shape[1] != other.shape[0]:
478 raise ValueError('dimension mismatch')
--> 479 return self._mul_sparse_matrix(other)
480
481 # If it's a list or whatever, treat it like a matrix
/opt/anaconda/lib/python3.7/site-packages/scipy/sparse/compressed.py in _mul_sparse_matrix(self, other)
500 maxval=nnz)
501 indptr = np.asarray(indptr, dtype=idx_dtype)
--> 502 indices = np.empty(nnz, dtype=idx_dtype)
503 data = np.empty(nnz, dtype=upcast(self.dtype, other.dtype))
504
MemoryError:
我在执行过程中监控了RAM,没有任何特别的观察(我有足够的内存:~1TB)。
初始矩阵有大约2400万个非零条目(非常稀疏),我希望结果矩阵也是相当稀疏的。
我是否对这个主题有一个普遍的误解,或者是代码中的某个地方有错误?
先谢谢你
尝试增加虚拟内存。1.5倍的额外虚拟机对我来说效果更好。