我有两个密集矩阵A
和B
,它们的大小都为3e5x100
。另外还有一个稀疏二进制矩阵C
,大小为3e5x3e5
。我想要计算以下值:C ∘ (AB')
,其中∘
是Hadamard乘积(即逐元素相乘),B'
是B
的转置。显式计算AB'
将需要大量的内存(~500GB)。由于最终结果不需要整个AB'
,所以只需计算乘法A_iB_j'
,其中C_ij != 0
,其中A_i
是矩阵A
的第i
列,C_ij
是矩阵C
中位置(i,j)
的元素。建议的方法如下所示:
result = numpy.initalize_sparse_matrix(shape = C.shape)
while True:
(i,j) = C_ij.pop_nonzero_index() #prototype function returns the nonzero index and then points to the next nonzero index
if (i,j) is empty:
break
result(i,j) = A_iB_j'
这个算法需要太多时间。有没有办法使用LAPACK/BLAS
算法来改进它?我用Python编程,所以我认为numpy
可以作为LAPACK/BLAS
的更人性化的封装。