Getting decoded vector in IVFPQ index?
I was wondering if there was a way to get a decoded vector from an IVFPQ index. For example in python:
test_vectors = np.random.randn(3, d).astype(np.float32)
index.add(test_vectors)
search_vectors = np.array([test_vectors[2]])
D, I = index.search(search_vectors, 5)
# how to do this?
index.get_decoded_vector(I[0])
# returns the decoded version of vector
Hi
Plase see the reconstruct and reconstruct_n methods.
Thanks. Does key refer to the vector's ID? I tried the following:
# ...
print(index.reconstruct(2))
But am getting this error:
../../../../.pyenv/versions/3.6.2/envs/rise/lib/python3.6/site-packages/faiss.py:134: in replacement_reconstruct
self.reconstruct_c(key, swig_ptr(x))
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <swigfaiss.IndexIVFPQ; proxy of <Swig Object of type 'faiss::IndexIVFPQ *' at 0x10d88c810> >
key = 2
recons = <Swig Object of type 'faiss::HeapArray< faiss::CMax< float,long > >::T *' at 0x10d88c840>
def reconstruct(self, key, recons):
> return _swigfaiss.IndexIVFPQ_reconstruct(self, key, recons)
E RuntimeError: Error in virtual void faiss::IndexIVFPQ::reconstruct(idx_t, float *) const at IndexIVFPQ.cpp:304: Error: 'direct_map.size() == ntotal' failed
../../../../.pyenv/versions/3.6.2/envs/rise/lib/python3.6/site-packages/swigfaiss.py:2820: RuntimeError
Printed those values:
print('index.ntotal =', index.ntotal)
# >>> index.ntotal = 3
print('index.direct_map.size() =', index.direct_map.size())
# >>> index.direct_map.size() = 0
So it looks like direct_map is empty, whatever that is. I'm guessing it has to do with the IDMap proxy?
Tried again with an IndexIDMap but different error:
import faiss
from sklearn.preprocessing import normalize
def l2_normalize(v):
return normalize(v, norm='l2')
d=128
nlists=8
M=32
nbits=8
coarse_quantizer = faiss.IndexFlatL2(d)
ivfpq_index = faiss.IndexIVFPQ(
# coarse quantization / IVF related params
coarse_quantizer, d, nlists,
# PQ related params
M, nbits
)
index = faiss.IndexIDMap(ivfpq_index)
training_vectors = l2_normalize(
np.random.randn(266, d).astype(np.float32)
)
test_vectors = l2_normalize(
np.random.randn(3, d).astype(np.float32)
)
index.train(training_vectors)
ids = np.arange(test_vectors.shape[0])
index.add_with_ids(test_vectors, ids)
print(index.reconstruct(2))
../../../../.pyenv/versions/3.6.2/envs/rise/lib/python3.6/site-packages/faiss.py:134: in replacement_reconstruct
self.reconstruct_c(key, swig_ptr(x))
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <swigfaiss.IndexIDMap; proxy of <Swig Object of type 'faiss::IndexIDMap *' at 0x10d0b09f0> >
key = 2
recons = <Swig Object of type 'faiss::HeapArray< faiss::CMax< float,long > >::T *' at 0x10d0b0a20>
def reconstruct(self, key, recons):
> return _swigfaiss.Index_reconstruct(self, key, recons)
E RuntimeError: Error in virtual void faiss::Index::reconstruct(idx_t, float *) const at Index.cpp:45: Can not compute reconstruct without knowing how to do so
../../../../.pyenv/versions/3.6.2/envs/rise/lib/python3.6/site-packages/swigfaiss.py:1109: RuntimeError
Oh, setting index.maintain_direct_map = True fixed the issue. Is that because otherwise there's no way to access a vector by ID in constant time (e.g. you'd have to iterate over all the IVF lists)? And how much memory does that direct map cost? 64 bits * # of vectors?
Yes exactly
Thanks for the quick replies 👍
great post, thanks for the answer.
What if we have index without index.maintain_direct_map = True ? In my scenario I used 'PCAR64,IVF4096(IVF512,PQ32x4fs,RFlat),SQ8' index str and now I can use reconstruct_n and search_and_reconstruct but it throws error on reconstruct.
` 1914 1915 def reconstruct(self, key, recons): -> 1916 return _swigfaiss.IndexPreTransform_reconstruct(self, key, recons) 1917 1918 def reconstruct_n(self, i0, ni, recons):
RuntimeError: Error in faiss::DirectMap::idx_t faiss::DirectMap::get(faiss::DirectMap::idx_t) const at /__w/faiss-wheels/faiss-wheels/faiss/faiss/invlists/DirectMap.cpp:78: Error: 'lo >= 0' failed: -1 entry in direct_map`
I have tried faiss.downcast_index(index.index).make_direct_map() but nothing changed. Thanks for the help.