Closed1
faiss_indexから学習に使用したembeddingを再構築する

faiss indexから連続する部分要素のembeddingsを得るにはconstruct_n
[1]を使う。
使用方法
> help(faiss_index.reconstruct_n)
Help on method replacement_reconstruct_n in module faiss.class_wrappers:
replacement_reconstruct_n(n0=0, ni=-1, x=None) method of faiss.swigfaiss.IndexFlatL2 instance
Approximate reconstruction of vectors `n0` ... `n0 + ni - 1` from the index.
Missing vectors trigger an exception.
Parameters
----------
n0 : int
Id of the first vector to reconstruct (default 0)
ni : int
Number of vectors to reconstruct (-1 = default = ntotal)
x : array_like, optional
pre-allocated array to store the results
Returns
-------
x : array_like
Reconstructed vectors, size (`ni`, `self.d`), `dtype`=float32
コード
batch_size = 4096
embeddings = np.zeros((faiss_index.ntotal, 384), dtype=np.float16)
for idx in tqdm(range(0, faiss_index.ntotal, batch_size), total=faiss_index.ntotal // batch_size):
real_batch_size = min(idx + batch_size, faiss_index.ntotal) - idx
embeddings[idx : idx + real_batch_size, :] = faiss_index.reconstruct_n(idx, real_batch_size)
参考資料
このスクラップは2023/10/09にクローズされました