Index#
- class scio.scores.utils.Index(*, dim, metric)[source]#
Bases:
objectPractical
faissindex wrapper.Eases the use of nearest neighbors search library
faiss.- Parameters:
dim (
int) – Dimensionality of the data space.metric (
IndexMetricLike) – SeeIndexMetric.
Notes
The current implementation uses
faiss-cpu, which may involve aGPU > CPU > GPUbottleneck.To avoid a
dtypeconversion bottleneck, usefloat32data.Current implementation only supports flat indexes.
Useful methods defined here
add(samples)Add reference samples to the index.
remove_ids(ids)Remove selected reference samples from the index.
search(samples, k, *[, self_query])Run nearest neighbors search.
Useful attributes defined here
Whether
Dis a similarity measure.Dimensionality of the data space.
See
IndexMetric.Number of samples in reference population.
- remove_ids(ids)[source]#
Remove selected reference samples from the index.
- Parameters:
ids (
Tensor) – Indices to remove from reference population.
- search(samples, k, *, self_query=False)[source]#
Run nearest neighbors search.
- Parameters:
samples (
Tensor) – The query samples.k (
int) – Number of neighbors to look up.self_query (
bool) – Set toTrueifindexwas built using exactlysamples, unshuffled. In this case, if a sample is itself amongst its closest neighbors (it is not always the case, e.g. inner product without prior normalization), it is excluded from the result. Requires looking upk + 1neighbors. Defaults toFalse.
- Returns:
D, I (
tuple[Tensor, Tensor]) – Result fromfaiss.Index.search()with postprocessing, includingtorchconversion back tosamples’ device (and dtype forD), with self removal if required.
Note
Dis the squared distance for \(L^2\)metric, and (decreasing) similarity for inner productmetric.