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

Function segmented_knn_graph

python/dgl/transforms/functional.py:337–487  ·  view source on GitHub ↗

r"""Construct multiple graphs from multiple sets of points according to k-nearest-neighbor (KNN) and return. Compared with :func:`dgl.knn_graph`, this allows multiple point sets with different capacity. The points from different sets are stored contiguously in the :attr:`x` tensor.

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

Source from the content-addressed store, hash-verified

335
336# pylint: disable=invalid-name
337def segmented_knn_graph(
338 x,
339 k,
340 segs,
341 algorithm="bruteforce-blas",
342 dist="euclidean",
343 exclude_self=False,
344):
345 r"""Construct multiple graphs from multiple sets of points according to
346 k-nearest-neighbor (KNN) and return.
347
348 Compared with :func:`dgl.knn_graph`, this allows multiple point sets with
349 different capacity. The points from different sets are stored contiguously
350 in the :attr:`x` tensor.
351 :attr:`segs` specifies the number of points in each point set. The
352 function constructs a KNN graph for each point set, where the predecessors
353 of each point are its k-nearest neighbors measured by the Euclidean distance.
354 DGL then composes all KNN graphs
355 into a batched graph with multiple (:math:`len(segs)`) connected components.
356
357 Parameters
358 ----------
359 x : Tensor
360 Coordinates/features of points. Must be 2D. It can be either on CPU or GPU.
361 k : int
362 The number of nearest neighbors per node.
363 segs : list[int]
364 Number of points in each point set. The numbers in :attr:`segs`
365 must sum up to the number of rows in :attr:`x`.
366 algorithm : str, optional
367 Algorithm used to compute the k-nearest neighbors.
368
369 * 'bruteforce-blas' will first compute the distance matrix
370 using BLAS matrix multiplication operation provided by
371 backend frameworks. Then use topk algorithm to get
372 k-nearest neighbors. This method is fast when the point
373 set is small but has :math:`O(N^2)` memory complexity where
374 :math:`N` is the number of points.
375
376 * 'bruteforce' will compute distances pair by pair and
377 directly select the k-nearest neighbors during distance
378 computation. This method is slower than 'bruteforce-blas'
379 but has less memory overhead (i.e., :math:`O(Nk)` where :math:`N`
380 is the number of points, :math:`k` is the number of nearest
381 neighbors per node) since we do not need to store all distances.
382
383 * 'bruteforce-sharemem' (CUDA only) is similar to 'bruteforce'
384 but use shared memory in CUDA devices for buffer. This method is
385 faster than 'bruteforce' when the dimension of input points
386 is not large. This method is only available on CUDA device.
387
388 * 'kd-tree' will use the kd-tree algorithm (CPU only).
389 This method is suitable for low-dimensional data (e.g. 3D
390 point clouds)
391
392 * 'nn-descent' is an approximate approach from paper
393 `Efficient k-nearest neighbor graph construction for generic similarity
394 measures <https://www.cs.princeton.edu/cass/papers/www11.pdf>`_. This method

Callers 1

forwardMethod · 0.85

Calls 15

DGLErrorClass · 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