CDCGS (Community Detection Clustering via Gumbel Softmax) Documentation๏
๐ Overview๏
CDCGS is a graph clustering algorithm that applies the Gumbel-Softmax trick to learn soft assignments of nodes to communities. It computes a community relationship matrix R using these assignments and applies softmax to normalize interactions between clusters.
โ๏ธ Class Definition๏
Class Name: CDCGS
This class implements the Gumbel-Softmax based community clustering model.
class CDCGS(nn.Module):
def __init__(self, num_nodes: int, n_clusters: int, tau: float = 1.0):
...
๐ Parameters๏
Parameter |
Type |
Default |
Description |
|---|---|---|---|
|
int |
โ |
Number of graph nodes |
|
int |
โ |
Number of target communities |
|
float |
1.0 |
Gumbel-Softmax temperature |
๐ Usage Examples๏
from soft_clustering._cdcgs._cdcgs import CDCGS
import torch
adj = torch.tensor([
[0, 1, 0, 0, 0, 0],
[1, 0, 1, 0, 0, 0],
[0, 1, 0, 1, 0, 0],
[0, 0, 1, 0, 1, 0],
[0, 0, 0, 1, 0, 1],
[0, 0, 0, 0, 1, 0]
], dtype=torch.float)
model = CDCGS(num_nodes=6, n_clusters=2, tau=1.0)
R, soft_assign = model(adj)
loss = model.loss(R)
print("Loss:", loss.item())
๐ ๏ธ Methods๏
forward(adj): Computes community assignments and relation matrixloss(output): Encourages the relation matrix to be close to identity
๐ Implementation Notes๏
Learns soft cluster assignments via Gumbel-Softmax
Encourages orthogonal (independent) clusters via identity loss
Simple and fully unsupervised
๐ Reference๏
This implementation is based on:
โCommunity Detection Clustering via Gumbel Softmaxโ
by H. Zhang, J. Bu, Y. Wang, and C. Chen.