faiss_search
faiss_search ¶
This module contains the class FaissSearch that can be used to perform k-nearest neighbor search using the Faiss library.
FaissSearch ¶
Class to perform k-nearest neighbor search using the Faiss library.
Methods:
-
k_nearest_neighbors–Perform k-nearest neighbor search using Faiss.
Source code in hadal/faiss_search.py
__init__ ¶
__init__(
device: str | None = None,
*,
enable_logging: bool = True,
log_level: int | None = logging.INFO
) -> None
Initialize FaissSearch object.
Parameters:
-
device(str | None, default:None) –Device for the Faiss search. If
None, it will use GPU if available, otherwise CPU. Default isNone. -
enable_logging(bool, default:True) –Logging option.
-
log_level(int | None, default:logging.INFO) –Logging level.
Source code in hadal/faiss_search.py
k_nearest_neighbors ¶
k_nearest_neighbors(
source_embeddings: numpy.ndarray,
target_embeddings: numpy.ndarray,
k: int = 4,
knn_metric: str = "inner_product",
device: str | None = None,
) -> tuple[numpy.ndarray, numpy.ndarray]
Perform k-nearest neighbor search using Faiss.
Parameters:
-
source_embeddings(numpy.ndarray) –The source embeddings.
-
target_embeddings(numpy.ndarray) –The target embeddings.
-
k(int, default:4) –The number of nearest neighbors.
-
knn_metric(str, default:'inner_product') –The metric to use for k-nearest neighbor search. Can be
inner_productorsqeuclidean. -
device(str | None, default:None) –The device to use for Faiss search. If
None, it will use GPU if available, otherwise CPU.
Note
It is fully relying on the Faiss library for the k-nearest neighbor search faiss.knn and faiss.gpu_knn.
inner_productusesfaiss.METRIC_INNER_PRODUCTsqeuclideanusesfaiss.METRIC_L2(squared Euclidean distance)
Returns:
-
numpy.ndarray–- d (numpy.ndarray): The distances of the k-nearest neighbors.
-
numpy.ndarray–- ind (numpy.ndarray): The indices of the k-nearest neighbors.