Faiss: How to reconstruct ids from an index object?

Created on 28 Sep 2018  Â·  5Comments  Â·  Source: facebookresearch/faiss

If I add data with my ids usingadd_with_ids, at search phase it can return the stored ids. So I think vectors and ids are all stored inside the index object.
like this

data = [[1, 2, 3, 4], [2, 3, 4, 5], [3, 4, 5, 6]]
ids = [100, 200, 300]
...
index.add_with_ids(data, ids)

Here is my question: I can use reconstruct_n to reconstruct vectors, how can I obtain ids?

question

Most helpful comment

@liqima

Here is a solution that worked for me when I used IndexIDMap

size = index.id_map.size()  # total number of ids
ids = [index.id_map.at(i) for i in range(size)]

All 5 comments

You can use search() to obtain the ids of the nearest neighbors of a given vector. What exactly would you like to do?

You can use search() to obtain the ids of the nearest neighbors of a given vector. What exactly would you like to do?

sorry for my vague description, I will make it as clear as possible.

# this is my vectors and ids
data = [[1, 2, 3, 4], [2, 3, 4, 5], [3, 4, 5, 6]]
ids = [100, 200, 300]
...
index.add_with_ids(data, ids)
faiss.write_index(index, 'index_file.index')
# now I have an index can be used to search

For some reason I lost raw data and ids and I really need them, but I only have an .index file. So I used reconstruct_n to reconstruct vectors(data) from it.
vectors = index.reconstruct_n(0, index.ntotal)
So how can I reconstruct ids? Here it should be ids = [100, 200, 300]
thanks for your reply.

What kind of index are you using?

No activity. Closing.

@liqima

Here is a solution that worked for me when I used IndexIDMap

size = index.id_map.size()  # total number of ids
ids = [index.id_map.at(i) for i in range(size)]
Was this page helpful?
0 / 5 - 0 ratings