我必须解决非常高维(~30'000)向量的异或运算来计算汉明距离。例如,我需要计算一个充满 False 的向量与 16 个稀疏定位的 True 与 50'000x30'000 矩阵的每一行之间的 XOR 运算。
截至目前,我发现最快的方法是不使用 scipy.sparse 而是在每一行上使用简单的 ^ 操作。
这个:
l1distances=(self.hashes[index,:]^self.hashes[all_points,:]).sum(axis=1)
碰巧比这快十倍:
sparse_hashes = scipy.sparse.csr_matrix((self.hashes)).astype('bool')
for i in range(all_points.shape[0]):
l1distances[0,i]=(sparse_hashes[index]-sparse_hashes[all_points[i]]).sum()
但是快十倍仍然很慢,因为从理论上讲,具有 16 个激活的稀疏向量应该使计算与具有 16 维一维相同。
有什么解决办法吗?我在这里真的很挣扎,感谢您的帮助!