【发布时间】:2018-01-30 12:05:06
【问题描述】:
import multiprocessing as mp
with mp.Pool(3) as pool:
res = pool.starmap(sim.cosine, ((std_matrix[i], std_matrix[j]) for i in range(nU) for j in range(nU)))
pool.close()
pool.join()
cos_matrix_uu = np.array(res).reshape(nU, nU)
在上面的代码nU = 6040中,我总共循环了36.481.600 (6040 * 6040)。
我的值在大小为 (6040, 3060) 的二维数组 std_matrix 中。
对于std_matrix中的每一对行我计算余弦相似度:
def cosine(a, b):
"""Calculates the Cosine Similarity.
:param a: array of ratings
:param b: array of ratings
:return: Cosine Similarity
"""
divisor = (np.sqrt(np.dot(a, a)) * np.sqrt(np.dot(b, b)))
if divisor == 0:
return 0
return np.divide(np.dot(a, b), divisor)
当我在不使用多线程的情况下运行此代码时,它会执行,但使用多线程时,我的电脑会死机。即使我使用 3 个线程。
我的 GPU 是 GTX850M。我可能做错了什么?
【问题讨论】:
标签: python python-3.x threadpool python-multithreading