一种暴力搜索方法,遍历数据库中的每一个向量与查询向量对比。
import faiss
import numpy as np
size = 1024 * 10
d = 128
M = 8
data = np.random.rand(size, d).astype('float32')
query = data[:5]
index = faiss.IndexFlatL2(d)
# index = faiss.index_factory(d, "Flat") #两种定义方式
index.add(data)
dis, ind = index.search(query, 10)
print(dis)
import faiss
import numpy as np
size = 1024 * 10
d = 128
M = 8
data = np.random.rand(size, d).astype('float32')
query = data[:5]
index = faiss.IndexFlatIP(d)
index.add(data)
dis, ind = index.search(query, 1)
print(dis)
import faiss
import numpy as np
size = 1024 * 10
d = 128
M = 8
data = np.random.rand(size, d).astype('float32')
query = data[:5]
index = faiss.IndexHNSWFlat(d,16)
index.add(data)
dis, ind = index.search(query, 10)
print(dis)