NN-Descent#

索引构建参数#

class cuvs.neighbors.nn_descent.IndexParams(
metric=None,
*,
metric_arg=None,
graph_degree=None,
intermediate_graph_degree=None,
max_iterations=None,
termination_threshold=None,
n_clusters=None,
)#

构建 NN-Descent 索引的参数

参数:
metricstr, 默认为 “sqeuclidean”

表示度量类型的字符串。新增数据的分布。

graph_degreeint

对于维度为 (N, D) 的输入数据集,确定所有邻居 knn 图的最终维度,其维度为 (N, graph_degree)

intermediate_graph_degreeint

在内部,nn-descent 在选择最终的 graph_degree 邻居之前,会构建一个维度为 (N, intermediate_graph_degree) 的所有邻居 knn 图。建议 intermediate_graph_degree >= 1.5 * graph_degree

max_iterationsint

nn-descent 精炼图的迭代次数。更多迭代会产生更高质量的图,但会牺牲性能

termination_thresholdfloat

nn-descent 终止迭代的阈值 delta

属性:
graph_degree
intermediate_graph_degree
max_iterations
metric
metric_arg
n_clusters
termination_threshold

索引#

class cuvs.neighbors.nn_descent.Index#

NN-Descent 索引对象。此对象存储经过训练的 NN-Descent 索引,构建后可用于获取 NN-Descent 图和距离

属性:
graph
trained

索引构建#

cuvs.neighbors.nn_descent.build(IndexParams index_params, dataset, graph=None, resources=None)[source]#

从数据集构建 KNN 图

参数:
index_paramscuvs.neighbors.nn_descent.IndexParams
dataset符合数组接口的矩阵,可在主机或设备内存上

支持的 dtype [float, int8, uint8]

graph用于存储输出图的可选主机矩阵
resources用于复用 CUDA 资源的可选 cuVS Resource 句柄。

如果未提供 Resources,将在函数内部分配 CUDA 资源,并在函数退出前同步。如果提供了 resources,则需要在访问输出之前显式调用 resources.sync() 进行同步。

返回:
index: py:class:cuvs.neighbors.nn_descent.Index

示例

>>> import cupy as cp
>>> from cuvs.neighbors import nn_descent
>>> n_samples = 50000
>>> n_features = 50
>>> n_queries = 1000
>>> k = 10
>>> dataset = cp.random.random_sample((n_samples, n_features),
...                                   dtype=cp.float32)
>>> build_params = nn_descent.IndexParams(metric="sqeuclidean")
>>> index = nn_descent.build(build_params, dataset)
>>> graph = index.graph