使用纯 Python 时,以下代码运行时间为 45 秒。
for iteration in range(maxiter):
for node in range(n):
for dest in adjacency_list[node]:
rs[iteration + 1][dest] += beta * rs[iteration][node] / len(adjacency_list[node])
但是,通过简单地初始化rs为 numpy ndarray 而不是 python 列表列表,代码在 145 秒内运行。我真的不知道为什么 numpy 使用此数组索引需要 3 倍的时间。
我的想法是尽可能多地向量化,但只设法向量化beta/len(adjacency_list[node]). 此代码在 77 秒内运行。
beta_over_out_degree = np.array([beta / len(al) for al in adjacency_list])
for iteration in range(1, maxiter + 1):
r_next = np.full(shape=n, fill_value=(1 - beta) / n)
f = beta_over_out_degree * r
for i in range(n):
r_next[adjacency_list[i]] += f[i]
r = np.copy(r_next)
rs[iteration] = np.copy(r)
问题是这adjacency_list是一个具有不同列大小的列表列表,包含 100 000 行和 1-15 列。使用邻接矩阵的更标准方法,至少作为普通的 ndarray,不是一种选择,因为对于 n=100 000,其 (n,n) 的形状太大而无法分配给内存。
有什么方法可以使用其索引进行矢量化以进行 numpy 高级索引(可能将其变成 numpy ndarray)?
我也非常感谢任何其他速度提示。提前致谢!
编辑:感谢@stevemo,我设法创建adjacency_matrix了csr_matrix功能并将其用于迭代乘法。程序现在只需 2 秒即可运行!
for iteration in range(1, 101):
rs[iteration] += rs[iteration - 1] * adjacency_matrix
三国纷争
相关分类