dgld.models.CONAD.models
- class dgld.models.CONAD.models.CONAD(feat_size)[source]
Bases:
Module
Contrastive Attributed Network Anomaly Detection with Data Augmentation.[PAKDD 2022] ref:https://github.com/zhiming-xu/conad
- Parameters
feat_size (int) – dimension of feature
Examples
>>> from dgld.models.CONAD import CONAD >>> model = CONAD(feat_size=1433) >>> model.fit(g, num_epoch=1) >>> result = model.predict(g)
- fit(graph, lr=0.001, weight_decay=0.0, num_epoch=1, margin=0.5, alpha=0.9, eta=0.7, device='cpu', rate=0.2, contrast_type='siamese', batch_size=0)[source]
Fitting model
- Parameters
graph (dgl.DGLGraph) – graph dataset
lr (float, optional) – learning rate, by default 1e-3
weight_decay (float, optional) – weight decay (L2 penalty), by default 0.
num_epoch (int, optional) – number of training epochs, by default 1
margin (float, optional) – parameter of the contrastive loss function, by default 0.5
alpha (float, optional) – balance parameter, by default 0.9
eta (float, optional) – balance parameter, by default 0.7
device (str, optional) – device of computation, by default ‘cpu’
rate (float, optional) – the rate of anomalies, by default 0.2
contrast_type (str, optional) – categories of contrastive loss functions, by default ‘siamese’
batch_size (int, optional) – the size of training batch, by default 0
- predict(graph, alpha=0.9, device='cpu', batch_size=0)[source]
predict and return anomaly score of each node
- Parameters
graph (dgl.DGLGraph) – graph dataset
alpha (float, optional) – balance parameter, by default 0.9
device (str, optional) – device of computation, by default ‘cpu’
batch_size (int, optional) – the size of training batch, by default 0
- Returns
anomaly score of each node
- Return type
numpy.ndarray
- training: bool
- class dgld.models.CONAD.models.CONAD_Base(in_feats, hid_feats=128, out_feats=64, num_heads=2, activation=LeakyReLU(negative_slope=0.01))[source]
Bases:
Module
This is a basic structure model of CONAD.
- Parameters
in_feats (int) – the feature dimension of the input data.
hid_feats (int, optional) – the dimension of hidden feature, by default 128
out_feats (int, optional) – the dimension of output feature, by default 64
num_heads (int, optional) – number of heads in Multi-Head Attention.
activation (callable activation function/layer or None, optional) – if not None, applies an activation function to the updated node features, by default nn.LeakyReLU()
- embed(g, h)[source]
compute embeddings
- Parameters
g (dgl.DGLGraph or list) – graph dataset
h (torch.Tensor) – features of nodes
- Returns
embeddings of nodes
- Return type
torch.Tensor
- embed_batch(blocks, h)[source]
compute embeddings for mini-batch graph training
- Parameters
g (list) – graph dataset
h (torch.Tensor) – features of nodes
- Returns
embeddings of nodes
- Return type
torch.Tensor
- forward(g, h)[source]
Forward Propagation
- Parameters
g (dgl.DGLGraph) – graph dataset
h (torch.Tensor or list) – feature representation of nodes
- Returns
torch.Tensor – reconstructed adjacency matrix
torch.Tensor – reconstructed attribute matrix
- forward_batch(blocks, h)[source]
Forward Propagation for mini-batch graph training
- Parameters
g (dgl.DGLGraph) – graph dataset
h (list) – feature representation of nodes
- Returns
torch.Tensor – reconstructed adjacency matrix
torch.Tensor – reconstructed attribute matrix
- reconstruct(g, h)[source]
reconstruct attribute matrix and adjacency matrix
- Parameters
g (dgl.DGLGraph or list) – graph dataset
h (torch.Tensor) – feature representation of nodes
- Returns
torch.Tensor – reconstructed adjacency matrix
torch.Tensor – reconstructed attribute matrix
- reconstruct_batch(blocks, h)[source]
reconstruct attribute matrix and adjacency matrix for mini-batch graph training
- Parameters
g (list) – graph dataset
h (torch.Tensor) – feature representation of nodes
- Returns
torch.Tensor – reconstructed adjacency matrix
torch.Tensor – reconstructed attribute matrix
- training: bool
- class dgld.models.CONAD.models.KnowledgeModel(rate, num_added_edge, surround, scale_factor)[source]
Bases:
BaseTransform
Knowledge Modeling Module, introduce a certain amount of anomalies belonging to each anomaly type to the input attributed network to form an augmented attributed network
- Parameters
rate (float) – rate for generating anomalies
num_added_edge (int) – parameter for generating high-degree anomalies
surround (int) – parameter for generating outlying anomalies
scale_factor (float) – parameter for generating disproportionate anomalies
- class dgld.models.CONAD.models.SiameseContrastiveLoss(margin)[source]
Bases:
Module
siamese contrastive loss function
- Parameters
margin (float) – parameter of the contrastive loss function
- forward(z, z_hat, l, adj)[source]
Forward Propagation
- Parameters
z (torch.Tensor) – feature representation of the original graph
z_hat (torch.Tensor) – feature representation for data augmentation graphs
l (torch.Tensor) – anomaly labels
adj (torch.Tensor) – adjacency matrix
- Returns
the result of the loss function
- Return type
torch.Tensor
- training: bool
- class dgld.models.CONAD.models.TripletContrastiveLoss(margin)[source]
Bases:
Module
triplet contrastive loss function
- Parameters
margin (float) – parameter of the contrastive loss function
- forward(orig, aug, l, adj)[source]
Forward Propagation
- Parameters
orig (torch.Tensor) – feature representation of the original graph
aug (torch.Tensor) – feature representation for data augmentation graphs
l (torch.Tensor) – anomaly labels
adj (torch.Tensor) – adjacency matrix
- Returns
the result of the loss function
- Return type
torch.Tensor
- training: bool