OCGNNBase#

class pygod.nn.OCGNNBase(in_dim, hid_dim, num_layers=2, dropout=0.0, act=<function relu>, backbone=<class 'torch_geometric.nn.models.basic_gnn.GCN'>, beta=0.5, warmup=2, eps=0.001, **kwargs)[source]#

Bases: Module

One-Class Graph Neural Networks for Anomaly Detection in Attributed Networks

OCGNN is an anomaly detector that measures the distance of anomaly to the centroid, in a similar fashion to the support vector machine, but in the embedding space after feeding towards several layers of GCN.

See [WJD+21] for details.

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

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

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

  • 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.

  • beta (float, optional) – The weight between the reconstruction loss and radius. Default: 0.5.

  • warmup (int, optional) – The number of epochs for warm-up training. Default: 2.

  • eps (float, optional) – The slack variable. Default: 0.001.

  • **kwargs – Other parameters for the backbone model.

forward(x, edge_index)[source]#

Forward computation.

Parameters:
Returns:

emb – Output embeddings.

Return type:

torch.Tensor

loss_func(emb)[source]#

Loss function for OCGNN

Parameters:

emb (torch.Tensor) – Embeddings.

Returns:

  • loss (torch.Tensor) – Loss value.

  • score (torch.Tensor) – Outlier scores of shape \(N\) with gradients.