首页 > 解决方案 > Faiss:如何创建大小为 1024 的 10M 向量的索引

问题描述

我想创建一个大小为 1024 的近 10M 向量的索引。这是我使用的代码。

import numpy as np
import faiss  
import random                

f = 1024

vectors = []
no_of_vectors=10000000
for k in range(no_of_vectors):
    v = [random.gauss(0, 1) for z in range(f)]
    vectors.append(v)
        
np_vectors = np.array(vectors).astype('float32')

index = faiss.IndexFlatL2(f)  
index.add(np_vectors)                 

faiss.write_index(index, "faiss_index.index")

该代码适用于少量向量。但是当向量的数量大约为 2M 时,内存限制就会超过。我使用index.add()而不是将向量附加到列表(向量 = [])。但它也没有奏效。

我想知道如何为大量向量创建索引。

标签: pythonmemoryfaiss

解决方案


推荐阅读