DMoN (Deep Modularity Networks) Documentationο
π Overviewο
DMoN is a deep clustering model for graphs that combines graph neural networks (GNNs) with modularity optimization. It performs soft clustering by maximizing modularity while preventing collapse using a regularization term. This approach enables unsupervised, end-to-end differentiable graph clustering.
βοΈ Class Definitionο
Class Name: DMoN
This PyTorch-based class implements the DMoN model using GCN layers and a soft assignment matrix.
class DMoN(nn.Module):
def __init__(self, in_channels: int, hidden_channels: int, n_clusters: int):
...
π Parametersο
Parameter |
Type |
Default |
Description |
|---|---|---|---|
|
int |
β |
Number of input features per node |
|
int |
β |
Number of hidden units in GCN |
|
int |
β |
Desired number of clusters |
π Usage Examplesο
from soft_clustering._dmon._dmon import DMoN
import torch
x = torch.eye(6)
edge_index = torch.tensor([[0, 1, 1, 2, 3, 4, 4, 5],
[1, 0, 2, 1, 4, 3, 5, 4]], dtype=torch.long)
adj = torch.zeros((6, 6))
for i, j in edge_index.t():
adj[i, j] = 1
model = DMoN(in_channels=6, hidden_channels=8, n_clusters=2)
soft_assign = model(x, edge_index, adj)
loss = model.loss(soft_assign, adj)
π₯ Input / π€ Outputο
Input to
forward(x, edge_index, adj):x (Tensor): Node feature matrix (N x F)edge_index (Tensor): Edge list for graph in COO format (2 x E)adj (Tensor): Dense adjacency matrix (N x N)
Returns:
soft_assign (Tensor): Cluster assignment probabilities (N x K)
π οΈ Methodsο
forward(x, edge_index, adj): Returns the soft cluster assignments.loss(soft_assign, adj): Computes modularity loss and collapse regularization.
π Implementation Notesο
Uses GCNConv layers from
torch_geometricCluster assignments computed using softmax over final GCN output
Modularity matrix is computed using degree-normalized formula
Collapse regularization ensures diverse cluster use
π Referenceο
This implementation is based on:
βGraph Clustering with Graph Neural Networksβ
by Alon, Yahav, and Wolf (2021).