แพ็คเกจนี้ประกอบด้วยไลบรารีส่วนขยายขนาดเล็กของอัลกอริธึมกราฟกราฟที่ได้รับการปรับปรุงสูงสำหรับการใช้งานใน Pytorch แพ็คเกจประกอบด้วยอัลกอริทึมการจัดกลุ่มต่อไปนี้:
รวมการดำเนินงานทั้งหมดทำงานเกี่ยวกับประเภทข้อมูลที่แตกต่างกันและมีการใช้งานทั้งสำหรับ CPU และ GPU
อัปเดต: ตอนนี้คุณสามารถติดตั้ง pytorch-cluster ผ่าน Anaconda สำหรับชุดที่สำคัญทั้งหมด/pytorch/cuda? ระบุว่าคุณติดตั้ง pytorch >= 1.8.0 ติดตั้งเพียงแค่เรียกใช้
conda install pytorch-cluster -c pyg
เราหรือให้ล้อ PIP สำหรับชุดค่าผสม OS/Pytorch/Cuda ที่สำคัญทั้งหมดดูที่นี่
ในการติดตั้งไบนารีสำหรับ 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 | |
|---|---|---|---|---|
| ลินเวกซ์ | ||||
| หน้าต่าง | ||||
| แม็กอส |
ในการติดตั้งไบนารีสำหรับ 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 | |
|---|---|---|---|---|
| ลินเวกซ์ | ||||
| หน้าต่าง | ||||
| แม็กอส |
หมายเหตุ: ไบนารีของรุ่นเก่ามีไว้สำหรับ 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 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/2.1.2, pytorch 2.2.0/2.2.1/2.2.2 สำหรับรุ่นเก่าคุณต้องระบุหมายเลขเวอร์ชันล่าสุดที่รองรับล่าสุดหรือติดตั้งผ่าน 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
เมื่อทำงานในคอนเทนเนอร์ Docker ที่ไม่มีไดรเวอร์ Nvidia 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 รวมกันซึ่งอยู่ใกล้กับจุดค้นหาที่กำหนดใน 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])
ตัวอย่างการเดินแบบสุ่มที่มีความยาว walk_length จากดัชนีโหนดทั้งหมดใน start ในกราฟที่กำหนดโดย (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