RadiusGraph
- class dgl.nn.pytorch.factory.RadiusGraph(r, p=2, self_loop=False, compute_mode='donot_use_mm_for_euclid_dist')[source]
基类:
Module
将一个点集转换为双向图的层,其中邻居位于给定距离内。
RadiusGraph 按以下步骤实现
计算所有点之间的 NxN 对距离矩阵。
选取每个点在给定距离内的点作为其邻居。
构建图,其中从邻居指向每个点都有边。
返回图的节点对应于点集中的点,每个点的邻居都在给定距离内。
- 参数:
r (float) – 邻居的半径。
p (float, 可选) –
Minkowski 度量的幂参数。当
p = 1
时,它等同于 Manhattan 距离 (L1 范数),当p = 2
时等同于 Euclidean 距离 (L2 范数)。(默认值: 2)
self_loop (bool, 可选) –
半径图是否包含自环。
(默认值: False)
compute_mode (str, 可选) –
use_mm_for_euclid_dist_if_necessary
- 如果 P > 25 或 R > 25,将使用矩阵乘法方法计算欧几里得距离 (p = 2)。use_mm_for_euclid_dist
- 将始终使用矩阵乘法方法计算欧几里得距离 (p = 2)。donot_use_mm_for_euclid_dist
- 将永远不使用矩阵乘法方法计算欧几里得距离 (p = 2)。(默认值: donot_use_mm_for_euclid_dist)
示例
以下示例使用 PyTorch 后端。
>>> import dgl >>> from dgl.nn.pytorch.factory import RadiusGraph
>>> x = torch.tensor([[0.0, 0.0, 1.0], ... [1.0, 0.5, 0.5], ... [0.5, 0.2, 0.2], ... [0.3, 0.2, 0.4]]) >>> rg = RadiusGraph(0.75) >>> g = rg(x) # Each node has neighbors within 0.75 distance >>> g.edges() (tensor([0, 1, 2, 2, 3, 3]), tensor([3, 2, 1, 3, 0, 2]))
当
get_distances
为 True 时,前向传播返回半径图和对应边的距离。>>> x = torch.tensor([[0.0, 0.0, 1.0], ... [1.0, 0.5, 0.5], ... [0.5, 0.2, 0.2], ... [0.3, 0.2, 0.4]]) >>> rg = RadiusGraph(0.75) >>> g, dist = rg(x, get_distances=True) >>> g.edges() (tensor([0, 1, 2, 2, 3, 3]), tensor([3, 2, 1, 3, 0, 2])) >>> dist tensor([[0.7000], [0.6557], [0.6557], [0.2828], [0.7000], [0.2828]])