MCPcopy
hub / github.com/dmlc/dgl / knn_graph

Function knn_graph

python/dgl/transforms/functional.py:111–278  ·  view source on GitHub ↗

r"""Construct a graph from a set of points according to k-nearest-neighbor (KNN) and return. The function transforms the coordinates/features of a point set into a directed homogeneous graph. The coordinates of the point set is specified as a matrix whose rows correspond to points a

(
    x, k, algorithm="bruteforce-blas", dist="euclidean", exclude_self=False
)

Source from the content-addressed store, hash-verified

109
110# pylint: disable=invalid-name
111def knn_graph(
112 x, k, algorithm="bruteforce-blas", dist="euclidean", exclude_self=False
113):
114 r"""Construct a graph from a set of points according to k-nearest-neighbor (KNN)
115 and return.
116
117 The function transforms the coordinates/features of a point set
118 into a directed homogeneous graph. The coordinates of the point
119 set is specified as a matrix whose rows correspond to points and
120 columns correspond to coordinate/feature dimensions.
121
122 The nodes of the returned graph correspond to the points, where the predecessors
123 of each point are its k-nearest neighbors measured by the chosen distance.
124
125 If :attr:`x` is a 3D tensor, then each submatrix will be transformed
126 into a separate graph. DGL then composes the graphs into a large batched
127 graph of multiple (:math:`shape(x)[0]`) connected components.
128
129 See :doc:`the benchmark <../api/python/knn_benchmark>` for a complete benchmark result.
130
131 Parameters
132 ----------
133 x : Tensor
134 The point coordinates. It can be either on CPU or GPU.
135
136 * If is 2D, ``x[i]`` corresponds to the i-th node in the KNN graph.
137
138 * If is 3D, ``x[i]`` corresponds to the i-th KNN graph and
139 ``x[i][j]`` corresponds to the j-th node in the i-th KNN graph.
140 k : int
141 The number of nearest neighbors per node.
142 algorithm : str, optional
143 Algorithm used to compute the k-nearest neighbors.
144
145 * 'bruteforce-blas' will first compute the distance matrix
146 using BLAS matrix multiplication operation provided by
147 backend frameworks. Then use topk algorithm to get
148 k-nearest neighbors. This method is fast when the point
149 set is small but has :math:`O(N^2)` memory complexity where
150 :math:`N` is the number of points.
151
152 * 'bruteforce' will compute distances pair by pair and
153 directly select the k-nearest neighbors during distance
154 computation. This method is slower than 'bruteforce-blas'
155 but has less memory overhead (i.e., :math:`O(Nk)` where :math:`N`
156 is the number of points, :math:`k` is the number of nearest
157 neighbors per node) since we do not need to store all distances.
158
159 * 'bruteforce-sharemem' (CUDA only) is similar to 'bruteforce'
160 but use shared memory in CUDA devices for buffer. This method is
161 faster than 'bruteforce' when the dimension of input points
162 is not large. This method is only available on CUDA device.
163
164 * 'kd-tree' will use the kd-tree algorithm (CPU only).
165 This method is suitable for low-dimensional data (e.g. 3D
166 point clouds)
167
168 * 'nn-descent' is an approximate approach from paper

Callers 1

forwardMethod · 0.85

Calls 15

DGLErrorClass · 0.85
_knn_graph_blasFunction · 0.85
remove_self_loopFunction · 0.85
remove_edgesFunction · 0.85
formatMethod · 0.80
contextMethod · 0.80
set_batch_num_nodesMethod · 0.80
set_batch_num_edgesMethod · 0.80
knnFunction · 0.70
minFunction · 0.50
sample_neighborsFunction · 0.50
shapeMethod · 0.45

Tested by

no test coverage detected