Skip to content

Latest commit

 

History

History
62 lines (45 loc) · 950 Bytes

3.1-faiss索引列表.md

File metadata and controls

62 lines (45 loc) · 950 Bytes

faiss索引列表

精确搜索(L2 搜索)

一种暴力搜索方法,遍历数据库中的每一个向量与查询向量对比。

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)

HNSW 索引

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)