После моего первоначального векторного поиска в FAISS скажем, k = 50, я хочу получить больше NN (т. е. 51, 52, 53...), пока не удовлетворю критериям, которые я установил для пространственных расчетов. (Я выполняю комбинацию поиска пространственного и семантического сходства.)
Есть ли способ получить следующий (k) из списка NN без необходимости запуска искать заново с k=51, затем k=52 и так далее?
Код: Выделить всё
import faiss
import numpy as np
d = 128 # dimension of vectors
index = faiss.IndexFlatL2(d)
# Add some vectors to the index
np.random.seed(123)
xb = np.random.random((1000, d)).astype('float32')
index.add(xb)
# Query vector
xq = np.random.random((1, d)).astype('float32')
def incremental_knn(index, xq, k, step_size):
"""Simulate an iterator-like behavior to get the next k-NN."""
start = 0
while True:
# Incrementally increase the number of nearest neighbors
k_next = start + step_size
D, I = index.search(xq, k_next) # Perform the search
yield I[0][start:k_next], D[0][start:k_next] # Return new results
start = k_next # Update starting point for the next iteration
if k_next >= k:
break
# Simulate fetching k-NN incrementally
step_size = 5 # Fetch 5 neighbors at a time
k = 20 # Total number of neighbors to fetch
for neighbors, distances in incremental_knn(index, xq, k, step_size):
print(f"Next batch of neighbors: {neighbors}, distances: {distances}")
Подробнее здесь: https://stackoverflow.com/questions/790 ... ity-search