pythonword-embeddingfaiss

Update an element in faiss index


I am using faiss indexflatIP to store vectors related to some words. I also use another list to store words (the vector of the nth element in the list is nth vector in faiss index). I have two questions:

  1. Is there a better way to relate words to their vectors?
  2. Can I update the nth element in the faiss?

Solution

  • You can do both.

    1. Is there a better way to relate words to their vectors?

    Call index.add_with_ids(vectors, ids)

    Some index types support the method add_with_ids, but flat indexes don't.

    If you call the method on a flat index, you will receive the error add_with_ids not implemented for this type of index

    If you want to use IDs with a flat index, you must use index2 = faiss.IndexIDMap(index)

    1. Can I update the nth element in the faiss?

    If you want to update some encodings, first remove them, then add them again with add_with_ids

    If you don't remove the original IDs first, you will have duplicates and search results will be messed up.

    To remove an array of IDs, call index.remove_ids(ids_to_replace)

    Nota bene: IDs must be of np.int64 type.