該軟件包由一個小型擴展庫組成,該庫具有高度優化的圖形群集算法,用於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