GPU_IVF_FLAT
GPU_IVF_FLAT索引是 IVF_FLAT 索引的 GPU 加速版本,專為 GPU 環境設計。它將向量資料分割成nlist 叢集單位,並透過首先比較目標查詢向量與每個叢集的中心點來計算相似度。透過調整nprobe 參數,只有最有希望的叢集才會被搜尋,這可以減少查詢時間,同時維持精確度與速度之間的平衡。有關基礎概念的詳細資訊,請參閱IVF_FLAT。
建立索引
要在 Milvus 中建立向量場的GPU_IVF_FLAT 索引,請使用add_index() 方法,指定index_type,metric_type, 以及索引的附加參數。
from pymilvus import MilvusClient
# Prepare index building params
index_params = MilvusClient.prepare_index_params()
index_params.add_index(
field_name="your_vector_field_name", # Name of the vector field to be indexed
index_type="GPU_IVF_FLAT", # Type of the index to create
index_name="vector_index", # Name of the index to create
metric_type="L2", # Metric type used to measure similarity
params={
"nlist": 1024, # Number of clusters for the index
} # Index building params
)
在此設定中
index_type:要建立的索引類型。在本範例中,設定值為GPU_IVF_FLAT。metric_type:用於計算向量間距離的方法。詳情請參閱公制類型。params:建立索引的附加設定選項。nlist:分割資料集的叢集數。
要瞭解
GPU_IVF_FLAT索引可用的更多建立參數,請參閱索引建立參數。
一旦配置好索引參數,您就可以直接使用create_index() 方法或在create_collection 方法中傳入索引參數來建立索引。如需詳細資訊,請參閱建立集合。
在索引上搜尋
索引建立且實體插入後,您就可以在索引上執行相似性搜尋。
search_params = {
"params": {
"nprobe": 10, # Number of clusters to search
}
}
res = MilvusClient.search(
collection_name="your_collection_name", # Collection name
anns_field="vector_field",
data=[[0.1, 0.2, 0.3, 0.4, 0.5]], # Query vector
limit=3, # TopK results to return
search_params=search_params
)
在此配置中
params:在索引上搜尋的其他設定選項。nprobe:要搜尋的群集數量。
要瞭解
GPU_IVF_FLAT索引可用的更多搜尋參數,請參閱特定於索引的搜尋參數。
索引參數
本節概述用於建立索引和在索引上執行搜尋的參數。
索引建立參數
下表列出了建立索引時可在params 中設定的參數。
參數 |
說明 |
值範圍 |
調整建議 |
|---|---|---|---|
|
在建立索引時,使用 k-means 演算法建立的叢集數目。 每個叢集以中心點表示,儲存向量清單。增加此參數可減少每個叢集中的向量數量,從而建立更小、更集中的分割。 |
類型:整數範圍:[1, 65536] 預設值: |
較大的 |
特定於索引的搜尋參數
下表列出在索引上搜尋時,可在search_params.params 中設定的參數。
參數 |
說明 |
值範圍 |
調整建議 |
|---|---|---|---|
|
搜尋候選資料的叢集數。 較高的值允許搜尋更多的叢集,藉由擴大搜尋範圍來改善召回率,但代價是增加查詢延遲。 |
類型:整數範圍:[1,nlist] 預設值: |
增加此值可提高召回率,但可能會減慢搜尋速度。請依 在大多數情況下,我們建議您設定此範圍內的值:[1, nlist]。 |