Skip to content

Python C code snippets

Matthijs Douze edited this page Aug 26, 2020 · 21 revisions

It is not always obvious how the C++ and Python layers interact. Therefore, we give some handy code in Python notebooks that can be copy/pasted to perform some useful operations.

They rely mostly on vector_to_array and a few other Python/C++ tricks described here

How can I get the PCA matrix in numpy from a PCA object?

See get_matrix_from_PCA.ipynb. This applies to any LinearTransform object.

How can I get / change the centroids from a ProductQuantizer object?

See access_PQ_centroids.ipynb.

How can I get the content of inverted lists?

See here: get_invlists.ipynb

Faiss/pytorch interop: how can I use a PQ codec with without leaving the GPU?

See PQ_codec_pytorch.ipynb.

How to explore the contents of an opaque index?

We have an index file but don't know what's in it. When accessing the Index fields of a wrapper index, they show up as a plain Index object. The downcast_index converts this plain index to the "leaf" class the index belongs to. This snippet is a demo on how to use downcast_index to extract all info from it: demo_explore_indedex.ipynb

How can I get all the ids from an IDMap or an IDMap2?

IDMap2 inherits IDMap, so this code works for both.

How to train a CPU index with a GPU just for k-means?

See train_ivf_with_gpu.ipynb

How to use the GPU at add time?

See assign_on_gpu.ipynb.

How can I force the k-means initialization?

plus: how to do this for IVF training

See initial_centroids_demo.ipynb

Clone this wiki locally