归一化 + 矩阵乘法 + top-k
归一化 + 矩阵乘法 + top-k
import numpy as np
def l2norm(x):
return x / np.linalg.norm(x, axis=1, keepdims=True)
docs = l2norm(np.random.rand(50000, 384).astype("float32"))
q = l2norm(np.random.rand(1, 384).astype("float32"))
scores = (docs @ q.T).ravel()
top = np.argpartition(-scores, 10)[:10]
top = top[np.argsort(-scores[top])]
print(top, scores[top])数万条以内的原型场景可用内存矩阵;规模再大或需要过滤与更新时,再迁移到向量库。