이 패키지는 Pytorch에서 사용하기위한 고도로 최적화 된 그래프 클러스터 알고리즘의 소형 확장 라이브러리로 구성됩니다. 패키지는 다음 클러스터링 알고리즘으로 구성됩니다.
포함 된 모든 작업은 다양한 데이터 유형에 대해 작동하며 CPU 및 GPU 모두에 구현됩니다.
업데이트 : 이제 모든 주요 OS/Pytorch/Cuda 조합에 대해 Anaconda를 통해 pytorch-cluster 설치할 수 있습니까? 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} Pytorch 설치에 따라 cpu , cu118 , cu121 또는 cu124 로 대체해야합니다.
cpu | cu118 | cu121 | cu124 | |
|---|---|---|---|---|
| 리눅스 | ✅ | ✅ | ✅ | ✅ |
| 창 | ✅ | ✅ | ✅ | ✅ |
| 마코스 | ✅ |
Pytorch 2.4.0 용 바이너리를 설치하려면 간단히 실행하십시오
pip install torch-cluster -f https://data.pyg.org/whl/torch-2.4.0+${CUDA}.html
여기서 ${CUDA} Pytorch 설치에 따라 cpu , cu118 , cu121 또는 cu124 로 대체해야합니다.
cpu | cu118 | cu121 | cu124 | |
|---|---|---|---|---|
| 리눅스 | ✅ | ✅ | ✅ | ✅ |
| 창 | ✅ | ✅ | ✅ | ✅ |
| 마코스 | ✅ |
참고 : 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, Pytorch 1.12.12. Pytorch 1.13.0/1.13.1, Pytorch 2.0.0/2.0.1, Pytorch 2.1.0/2.1.1/2.1.2, Pytorch 2.2.0/2.2.1/2.2.2 및 Pytorch 2.3.0/2.3.1 (동일한 절차에 따라). 이전 버전의 경우 소스에서 수동 설치를 방지하기 위해 최신 지원 버전 번호를 명시 적으로 지정하거나 pip install --no-index 설치해야합니다. 최신 지원 버전 번호를 여기에서 찾을 수 있습니다.
최소한 Pytorch 1.4.0이 설치되어 있는지 확인하고 cuda/bin 및 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는 컴퓨팅 기능을 평가해야하며 실패 할 수 있습니다. 이 경우 Compute 기능이 TORCH_CUDA_ARCH_LIST 를 통해 설정되어 있는지 확인하십시오 .
export TORCH_CUDA_ARCH_LIST = "6.0 6.1 7.2+PTX 7.5+PTX"
표시되지 않은 정점을 선택하여 표시되지 않은 이웃과 일치시키는 탐욕스러운 클러스터링 알고리즘 (가장자리 무게를 극대화하는). GPU 알고리즘은 Fagginger Auer 및 Bisseling에서 조정되었습니다 : Greedy Graph Matching을위한 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])
포인트 클라우드에 대한 사용자 정의 크기의 일반 그리드를 오버레이하고 복셀 내의 모든 포인트를 클러스터하는 클러스터링 알고리즘.
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에서 y 의 주어진 쿼리 포인트에 가장 가까운 포인트입니다. 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])
샘플 (row, col) 에 의해 주어진 그래프에서 start 모든 노드 지수에서 길이의 랜덤 walk_length .
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 ++ API를 제공합니다.
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