该软件包由一个小型扩展库组成,该库具有高度优化的图形群集算法,用于Pytorch。该软件包包括以下聚类算法:
所有包括在不同数据类型上的操作工作,并针对CPU和GPU实施。
更新:您现在可以通过pytorch-cluster安装所有主要OS/Pytorch/CUDA组合?鉴于您安装了pytorch >= 1.8.0 ,只需运行
conda install pytorch-cluster -c pyg
或者,我们为所有主要的OS/Pytorch/CUDA组合提供PIP轮毂,请参见此处。
要安装Pytorch 2.5.0的二进制文件,只需运行
pip install torch-cluster -f https://data.pyg.org/whl/torch-2.5.0+${CUDA}.html
${CUDA}应取代cpu , cu118 , cu121或cu124 ,具体取决于您的pytorch安装。
cpu | cu118 | cu121 | cu124 | |
|---|---|---|---|---|
| Linux | ✅ | ✅ | ✅ | ✅ |
| 视窗 | ✅ | ✅ | ✅ | ✅ |
| macos | ✅ |
要安装Pytorch 2.4.0的二进制文件,只需运行
pip install torch-cluster -f https://data.pyg.org/whl/torch-2.4.0+${CUDA}.html
${CUDA}应取代cpu , cu118 , cu121或cu124 ,具体取决于您的pytorch安装。
cpu | cu118 | cu121 | cu124 | |
|---|---|---|---|---|
| Linux | ✅ | ✅ | ✅ | ✅ |
| 视窗 | ✅ | ✅ | ✅ | ✅ |
| macos | ✅ |
Note: Binaries of older versions are also provided for PyTorch 1.4.0, PyTorch 1.5.0, PyTorch 1.6.0, PyTorch 1.7.0/1.7.1, PyTorch 1.8.0/1.8.1, PyTorch 1.9.0, PyTorch 1.10.0/1.10.1/1.10.2, PyTorch 1.11.0, PyTorch 1.12.0/1.12.1,Pytorch 1.13.0/1.13.1,Pytorch 2.0.0/2.0.1,Pytorch 2.1.0/2.1.1.1/2.1.2,Pytorch 2.2.0/2.2.1.2.1.2.1/2.2.2,以及Pytorch 2.3.0/2.3.0/2.3.0/2.3.1/2.3.1(遵循同一过程)。对于较旧的版本,您需要明确指定最新的支持版本号,或通过pip install --no-index安装,以防止源头安装手动安装。您可以在此处查找最新支持的版本号。
确保安装了至少Pytorch 1.4.0,并验证cuda/bin and cuda/include分别在您的$PATH和$CPATH中,例如:
$ python -c "import torch; print(torch.__version__)"
>>> 1.4.0
$ python -c "import torch; print(torch.__version__)"
>>> 1.1.0
$ echo $PATH
>>> /usr/local/cuda/bin:...
$ echo $CPATH
>>> /usr/local/cuda/include:...
然后运行:
pip install torch-cluster
当在没有NVIDIA驱动程序的Docker容器中运行时,Pytorch需要评估计算功能,并且可能会失败。在这种情况下,请确保通过TORCH_CUDA_ARCH_LIST设置计算功能,例如:
export TORCH_CUDA_ARCH_LIST = "6.0 6.1 7.2+PTX 7.5+PTX"
挑选一个未标记的顶点并将其与未标记的邻居匹配的贪婪聚类算法(最大化其边缘重量)。 GPU算法改编自Fagginger Auer和Bisseling:用于贪婪图匹配的GPU算法(LNCS 2012)
import torch
from torch_cluster import graclus_cluster
row = torch . tensor ([ 0 , 1 , 1 , 2 ])
col = torch . tensor ([ 1 , 0 , 2 , 1 ])
weight = torch . tensor ([ 1. , 1. , 1. , 1. ]) # Optional edge weights.
cluster = graclus_cluster ( row , col , weight ) print(cluster)
tensor([0, 0, 1])
一种聚类算法,该算法在点云上覆盖了用户定义大小的常规网格,并将所有点簇在体voxel中。
import torch
from torch_cluster import grid_cluster
pos = torch . tensor ([[ 0. , 0. ], [ 11. , 9. ], [ 2. , 8. ], [ 2. , 2. ], [ 8. , 3. ]])
size = torch . Tensor ([ 5 , 5 ])
cluster = grid_cluster ( pos , size ) print(cluster)
tensor([0, 5, 3, 0, 1])
采样算法,迭代地对其剩余点进行了最远的点进行采样。
import torch
from torch_cluster import fps
x = torch . tensor ([[ - 1. , - 1. ], [ - 1. , 1. ], [ 1. , - 1. ], [ 1. , 1. ]])
batch = torch . tensor ([ 0 , 0 , 0 , 0 ])
index = fps ( x , batch , ratio = 0.5 , random_start = False ) print(index)
tensor([0, 3])
计算图表到最接近的k点。
args:
[N, F] 。[N]的批处理向量,将每个节点分配给一个特定示例。 batch需要分类。 (默认: None )True ,则图将包含自动浮动。 (默认: False )"source_to_target"或"target_to_source" )。 (默认: "source_to_target" )True ,将使用余弦距离而不是欧几里得距离来查找最近的邻居。 (默认: False )batch不是None ,则没有任何作用,或者输入位于GPU上。 (默认: 1 ) import torch
from torch_cluster import knn_graph
x = torch . tensor ([[ - 1. , - 1. ], [ - 1. , 1. ], [ 1. , - 1. ], [ 1. , 1. ]])
batch = torch . tensor ([ 0 , 0 , 0 , 0 ])
edge_index = knn_graph ( x , k = 2 , batch = batch , loop = False ) print(edge_index)
tensor([[1, 2, 0, 3, 0, 3, 1, 2],
[0, 0, 1, 1, 2, 2, 3, 3]])
计算给定距离内所有点的图形边缘。
args:
[N, F] 。[N]的批处理向量,将每个节点分配给一个特定示例。 batch需要分类。 (默认: None )True ,则图将包含自动浮动。 (默认: False )max_num_neighbors ,则随机选择返回的邻居。 (默认: 32 )"source_to_target"或"target_to_source" )。 (默认: "source_to_target" )batch不是None ,则没有任何作用,或者输入位于GPU上。 (默认: 1 ) import torch
from torch_cluster import radius_graph
x = torch . tensor ([[ - 1. , - 1. ], [ - 1. , 1. ], [ 1. , - 1. ], [ 1. , 1. ]])
batch = torch . tensor ([ 0 , 0 , 0 , 0 ])
edge_index = radius_graph ( x , r = 2.5 , batch = batch , loop = False ) print(edge_index)
tensor([[1, 2, 0, 3, 0, 3, 1, 2],
[0, 0, 1, 1, 2, 2, 3, 3]])
簇在X中指向X中最接近给定的查询点。 batch_{x,y}向量需要对矢量进行排序。
import torch
from torch_cluster import nearest
x = torch . Tensor ([[ - 1 , - 1 ], [ - 1 , 1 ], [ 1 , - 1 ], [ 1 , 1 ]])
batch_x = torch . tensor ([ 0 , 0 , 0 , 0 ])
y = torch . Tensor ([[ - 1 , 0 ], [ 1 , 0 ]])
batch_y = torch . tensor ([ 0 , 0 ])
cluster = nearest ( x , y , batch_x , batch_y ) print(cluster)
tensor([0, 0, 1, 1])
样本从所有节点索引中的所有节点start中的随机walk_length步道(row, col)给出的图中的所有节点索引。
import torch
from torch_cluster import random_walk
row = torch . tensor ([ 0 , 1 , 1 , 1 , 2 , 2 , 3 , 3 , 4 , 4 ])
col = torch . tensor ([ 1 , 0 , 2 , 3 , 1 , 4 , 1 , 4 , 2 , 3 ])
start = torch . tensor ([ 0 , 1 , 2 , 3 , 4 ])
walk = random_walk ( row , col , start , walk_length = 3 ) print(walk)
tensor([[0, 1, 2, 4],
[1, 3, 4, 2],
[2, 4, 2, 1],
[3, 4, 2, 4],
[4, 3, 1, 0]])
pytest
torch-cluster还提供了一个C ++ API,其中包含C ++等效的Python模型。
export Torch_DIR=`python -c 'import torch;print(torch.utils.cmake_prefix_path)'`
mkdir build
cd build
# Add -DWITH_CUDA=on support for the CUDA if needed
cmake ..
make
make install