CARDBase

class pygod.nn.CARDBase(in_dim, subgraph_num_neigh=4, fp=0.6, gama=0.4, alpha=0.1, hid_dim=64, num_layers=4, dropout=0.0, act=<function relu>, backbone=<class 'torch_geometric.nn.models.basic_gnn.GCN'>, **kwargs)[source]

Bases: Module

Community-Guided Contrastive Learning with Anomaly-Aware Reconstruction for Anomaly Detection on Attributed Networks.

CARD is a contrastive learning based method and utilizes mask reconstruction and community information to make anomalies more distinct. This model is train with contrastive loss and local and global attribute reconstruction loss. Random neighbor sampling instead of random walk sampling is used to sample the subgraph corresponding to each node. Since random neighbor sampling cannot accurately control the number of neighbors for each sampling, it may run slower compared to the method implementation in the original paper.

See:cite:Wang2024Card for details.

Parameters:
  • in_dim (int) – Input dimension of model.

  • subgraph_num_neigh (int, optional) – Number of neighbors in subgraph sampling for each node, Values not exceeding 4 are recommended for efficiency. Default: 4.

  • fp (float, optional) – The balance parameter between the mask autoencoder module and contrastive learning. Default: 0.6

  • gama (float, optional) – The proportion of the local reconstruction in contrastive learning module. Default: 0.5

  • alpha (float, optional) – The proprotion of the community embedding in the conbine_encoder. Default: 0.1

  • hid_dim (int, optional) – Hidden dimension of model. Default: 64.

  • num_layers (int, optional) – Total number of layers in model. Default: 4.

  • dropout (float, optional) – Dropout rate. Default: 0..

  • act (callable activation function or None, optional) – Activation function if not None. Default: torch.nn.functional.relu.

  • backbone (torch.nn.Module) – The backbone of the deep detector implemented in PyG. Default: torch_geometric.nn.GCN.

  • **kwargs – Other parameters for the backbone.

forward(data)[source]

Forward computation.

Parameters:

Data (torch_geometric.data.Data) – Input graph.

Returns:

  • logits (torch.Tensor) – Discriminator logits of positive examples.

  • neg_logits (torch.Tensor) – Discriminator logits of negative examples.

  • x_ (torch.Tensor) – feature reconstract matrix

  • local_x_ (torch.Tensor) – subgraph feature reconstract matrix

loss_func(logits, diff_logits, x_, local_x_, x, con_label)[source]

The loss function proposed in the CARD paper. This implementation ignores the KL-loss as it contributes little to the accuracy.

Parameters:
  • logits (torch.Tensor) – Discriminator logits of positive subgraphs batch.

  • diff_logits (torch.Tensor) – Discriminator logits of negative subgraphs batch.

  • x (torch.Tensor) – Global reconstructed attribute embeddings.

  • local_x (torch.Tensor) – Local reconstructed attribute embeddings.

  • x – Input attribute embeddings.

  • con_label (torch.Tensor) – Contrastive learning pseudo label

Returns:

  • final_loss (torch.Tensor) – The total loss value used to backpropagate and update the model parameters.

  • score (torch.Tensor) – The anomaly score for each node.

static process_graph(data)[source]

Obtain the community structure matrix and the diffusion graph data.

Parameters:

data (torch_geometric.data.Data) – Input graph.

Returns:

  • community_adj (torch.Tensor) – Community structure matrix, corresponding to the B matrix in the paper.

  • diff_data (torch_geometric.data.Data) – Diffusion graph Data