GraphSL.GNN.IVGD package
Submodules
GraphSL.GNN.IVGD.correction module
GraphSL.GNN.IVGD.main module
- class GraphSL.GNN.IVGD.main.IVGD[source]
Bases:
objectImplement the Invertible Validity-aware Graph Diffusion (IVGD) model.
Wang, Junxiang, Junji Jiang, and Liang Zhao. “An invertible graph diffusion neural network for source localization.” Proceedings of the ACM Web Conference 2022. 2022.
- normalize(x)[source]
The input tensor x is normalized to between 0 and 1.
Args:
x (torch.Tensor): Input tensor to be normalized.
Returns:
x (torch.Tensor): Normalized tensor.
- test(adj, test_dataset, diffusion_model, IVGD_model, thres)[source]
Test the IVGD model on the given test dataset.
Args:
adj (scipy.sparse.csr_matrix): The adjacency matrix of the graph.
test_dataset (torch.utils.data.dataset.Subset): the test dataset (number of simulations * number of graph nodes * 2 (the first column is seed vector and the second column is diffusion vector)).
diffusion_model (torch.nn.Module): Trained diffusion model.
IVGD_model (torch.nn.Module): Trained IVGD model.
thres (float): Threshold value.
Returns:
metric (Metric): Object containing test metrics.
Example:
import os
curr_dir = os.getcwd()
from GraphSL.utils import load_dataset, diffusion_generation, split_dataset
from GraphSL.GNN.IVGD.main import IVGD
data_name = ‘karate’
graph = load_dataset(data_name, data_dir=curr_dir)
dataset = diffusion_generation(graph=graph, infect_prob=0.3, diff_type=’IC’, sim_num=100, seed_ratio=0.1)
adj, train_dataset, test_dataset =split_dataset(dataset)
ivgd = IVGD()
diffusion_model = ivgd.train_diffusion(adj, train_dataset)
ivgd_model, thres, auc, f1, pred =ivgd.train(adj, train_dataset, diffusion_model)
print(“IVGD:”)
print(f”train auc: {auc:.3f}, train f1: {f1:.3f}”)
metric = ivgd.test(adj, test_dataset, diffusion_model, ivgd_model, thres)
print(f”test acc: {metric.acc:.3f}, test pr: {metric.pr:.3f}, test re: {metric.re:.3f}, test f1: {metric.f1:.3f}, test auc: {metric.auc:.3f}”)
- train(adj, train_dataset, diffusion_model, num_thres=10, lr=0.0001, weight_decay=0.0001, num_epoch=200, print_epoch=10, random_seed=0)[source]
Train the IVGD model.
Args:
adj (scipy.sparse.csr_matrix): The adjacency matrix of the graph.
train_dataset (torch.utils.data.dataset.Subset): the training dataset (number of simulations * number of graph nodes * 2 (the first column is seed vector and the second column is diffusion vector)).
diffusion_model (torch.nn.Module): Trained diffusion model.
num_thres (int): Number of threshold values to try.
lr (float): Learning rate.
weight_decay (float): Weight decay.
num_epoch (int): Number of epochs for training.
print_epoch (int): Number of epochs every time to print loss.
random_epoch (int): Random seed.
Returns:
ivgd (torch.nn.Module): Trained IVGD model.
opt_thres (float): Optimal threshold value.
train_auc (float): Training AUC.
opt_f1 (float): Optimal F1 score.
pred (numpy.ndarray): Predicted seed vector of the training set, every column is the prediction of every simulation. It is used to adjust thres_list.
Example:
import os
curr_dir = os.getcwd()
from GraphSL.utils import load_dataset, diffusion_generation, split_dataset
from GraphSL.GNN.IVGD.main import IVGD
data_name = ‘karate’
graph = load_dataset(data_name, data_dir=curr_dir)
dataset = diffusion_generation(graph=graph, infect_prob=0.3, diff_type=’IC’, sim_num=100, seed_ratio=0.1)
adj, train_dataset, test_dataset =split_dataset(dataset)
ivgd = IVGD()
diffusion_model = ivgd.train_diffusion(adj, train_dataset)
ivgd_model, thres, auc, f1, pred =ivgd.train(adj, train_dataset, diffusion_model)
print(“IVGD:”)
print(f”train auc: {auc:.3f}, train f1: {f1:.3f}”)
- train_diffusion(adj, train_dataset, lr=0.0001, weight_decay=0.0001, num_epoch=50, print_epoch=10, random_seed=0)[source]
Train the diffusion model.
Args:
adj (scipy.sparse.csr_matrix): Adjacency matrix of the graph.
train_dataset (torch.utils.data.dataset.Subset): the training dataset (number of simulations * number of graph nodes * 2 (the first column is seed vector and the second column is diffusion vector)).
lr (float): Learning rate.
weight_decay (float): Weight decay.
epoch_num (int): Number of epochs.
print_epoch (int): Number of epochs every time to print loss.
random_seed (int): Random seed.
Returns:
diffusion_model (torch.nn.Module): Trained diffusion model.
Example:
import os
curr_dir = os.getcwd()
from GraphSL.utils import load_dataset, diffusion_generation, split_dataset
from GraphSL.GNN.IVGD.main import IVGD
data_name = ‘karate’
graph = load_dataset(data_name, data_dir=curr_dir)
dataset = diffusion_generation(graph=graph, infect_prob=0.3, diff_type=’IC’, sim_num=100, seed_ratio=0.1)
adj, train_dataset, test_dataset =split_dataset(dataset)
ivgd = IVGD()
diffusion_model = ivgd.train_diffusion(adj, train_dataset)
GraphSL.GNN.IVGD.diffusion_model module
- class GraphSL.GNN.IVGD.diffusion_model.I_GCN(hidden_dim=32, num_layers=3, d=2)[source]
Bases:
ModuleInvertible Graph Convolutional Network
- backward(adj, influ_vector)[source]
Perform a backward pass (inverse) using fixed-point iteration.
Arguments:
adj (scipy.sparse matrix): Adjacency matrix of the graph.
influ_vector (torch.Tensor): Influence vector obtained from forward pass.
Returns:
seed_vector (torch.Tensor): Seed vector approximated using fixed-point iteration.
- forward(adj, seed_vector)[source]
Perform a forward pass through the I_GCN model.
Arguments:
adj (scipy.sparse matrix): Adjacency matrix of the graph.
seed_vector (torch.Tensor): Seed vector representing initial node activations.
Returns:
x (torch.Tensor): Output vector after passing through the network.
- class GraphSL.GNN.IVGD.diffusion_model.I_GCNLayer(in_features, out_features)[source]
Bases:
ModuleInvertible Graph Convolutional Network Layer
- forward(x, adj)[source]
Perform a forward pass through the graph convolution layer.
Arguments:
x (torch.Tensor): Input feature matrix of shape (num_nodes, in_features).
adj (torch.Tensor): Adjacency matrix of the graph of shape (num_nodes, num_nodes).
Returns:
x (torch.Tensor): Output feature matrix of shape (num_nodes, out_features).
GraphSL.GNN.IVGD.validity_net module
- class GraphSL.GNN.IVGD.validity_net.validity_net(alpha, tau, rho)[source]
Bases:
ModuleValidity-aware layers.
Attributes:
number_layer (int): Number of layers.
alpha1, alpha2, alpha3, alpha4, alpha5 (float): Alpha values for each layer.
tau1, tau2, tau3, tau4, tau5 (float): Tau values for each layer.
net1, net2, net3, net4, net5 (correction): Correction layer.
rho1, rho2, rho3, rho4, rho5 (float): Rho values for each layer.