graphslim.models package
graphslim.models.base module
- class graphslim.models.base.BaseGNN(*args: Any, **kwargs: Any)[source]
Bases:
Module- fit_with_val(data, train_iters=600, verbose=False, normadj=True, setting='trans', reduced=False, final_output=False, best_val=None, **kwargs)[source]
- predict(features=None, adj=None, normadj=True, output_layer_features=False)
- test(data, setting='trans', verbose=False)
Evaluate GCN performance on test set. :param idx_test: node testing indices
graphslim.models.gcn module
graphslim.models.appnp module
multiple transformaiton and multiple propagation
graphslim.models.cheby module
graphslim.models.gat module
Extended from https://github.com/rusty1s/pytorch_geometric/tree/master/benchmark/citation
graphslim.models.gntk module
- class graphslim.models.gntk.GNTK(num_layers, num_mlp_layers, jk, scale)[source]
Bases:
objectimplement the Graph Neural Tangent Kernel
- diag(feat, A)[source]
compute the diagonal element of GNTK for graph g with adjacency matrix A g: graph g A: adjacency matrix
graphslim.models.graphsage module
graphslim.models.sgc module
multiple transformaiton and multiple propagation
graphslim.models.layers module
- class graphslim.models.layers.ChebConvolution(*args: Any, **kwargs: Any)
Bases:
ModuleSimple GCN layer, similar to https://github.com/tkipf/pygcn
- forward(x, adj, size=None)
Graph Convolutional Layer forward function
- reset_parameters()
- class graphslim.models.layers.GATConv(*args: Any, **kwargs: Any)
Bases:
MessagePassingThe graph attentional operator from the `"Graph Attention Networks"`_:
\[\mathbf{x}^{\prime}_i = \alpha_{i,i}\mathbf{\Theta}\mathbf{x}_{i} + \sum_{j \in \mathcal{N}(i)} \alpha_{i,j}\mathbf{\Theta}\mathbf{x}_{j}\]where the attention coefficients \(\alpha_{i,j}\) are computed as:
\[\alpha_{i,j} = \frac{ \exp\left(\mathrm{LeakyReLU}\left(\mathbf{a}^{\top} [\mathbf{\Theta}\mathbf{x}_i \, \Vert \, \mathbf{\Theta}\mathbf{x}_j] \right)\right)} {\sum_{k \in \mathcal{N}(i) \cup \{ i \}} \exp\left(\mathrm{LeakyReLU}\left(\mathbf{a}^{\top} [\mathbf{\Theta}\mathbf{x}_i \, \Vert \, \mathbf{\Theta}\mathbf{x}_k] \right)\right)}\]- Parameters:
in_channels (int or tuple) – Size of each input sample. A tuple corresponds to the sizes of source and target dimensionalities.
out_channels (int) – Size of each output sample.
heads (int, optional) – Number of multi-head attentions. (default:
1)concat (bool, optional) – If set to
False, the multi-head attentions are averaged instead of concatenated. (default:True)negative_slope (float, optional) – LeakyReLU angle of the negative slope. (default:
0.2)dropout (float, optional) – Dropout probability of the normalized attention coefficients which exposes each node to a stochastically sampled neighborhood during training. (default:
0)add_self_loops (bool, optional) – If set to
False, will not add self-loops to the input graph. (default:True)bias (bool, optional) – If set to
False, the layer will not learn an additive bias. (default:True)**kwargs (optional) – Additional arguments for
torch_geometric.nn.conv.MessagePassing.
References
- forward(x: torch.Tensor | torch_geometric.typing.OptPairTensor, edge_index: torch_geometric.typing.Adj, size: torch_geometric.typing.Size | None = None, return_attention_weights=None, edge_weight=None)
- Parameters:
return_attention_weights (bool, optional) – If set to
True, will additionally return the tuple(edge_index, attention_weights), holding the computed attention weights for each edge. (default:None)
- message(x_j: torch.Tensor, alpha_j: torch.Tensor, alpha_i: torch_geometric.typing.OptTensor, index: torch.Tensor, ptr: torch_geometric.typing.OptTensor, size_i: int | None) torch.Tensor
- reset_parameters()
- class graphslim.models.layers.GraphConvolution(*args: Any, **kwargs: Any)
Bases:
Module- forward(x, adj)
Graph Convolutional Layer forward function
- reset_parameters()
- class graphslim.models.layers.MyLinear(*args: Any, **kwargs: Any)
Bases:
ModuleSimple Linear layer, modified from https://github.com/tkipf/pygcn
- forward(input)
- reset_parameters()
graphslim.models.sgformer module
- class graphslim.models.sgformer.SGFormer(*args: Any, **kwargs: Any)[source]
Bases:
Module- fit_with_val(data, train_iters=600, verbose=False, normadj=True, setting='trans', reduced=False, reindex=False, **kwargs)[source]
- predict(features=None, adj=None, normadj=True, output_layer_features=False)
- test(data, setting='trans', verbose=False)
Evaluate GCN performance on test set. :param idx_test: node testing indices
graphslim.models.ignr module
- class graphslim.models.ignr.GraphonLearner(*args: Any, **kwargs: Any)[source]
Bases:
Module- inference(c)
graphslim.models.krr module
graphslim.models.prbcd module
Robustness of Graph Neural Networks at Scale. NeurIPS 2021.
Modified from https://github.com/sigeisler/robustness_of_gnns_at_scale/blob/main/rgnn_at_scale/attacks/prbcd.py
- class graphslim.models.prbcd.PRBCD(data, model=None, make_undirected=True, eps=1e-07, search_space_size=10000000, max_final_samples=20, fine_tune_epochs=100, epochs=400, lr_adj=0.1, with_early_stopping=True, do_synchronize=True, device='cuda', **kwargs)[source]
Bases:
object- sample_final_edges(n_perturbations)
- graphslim.models.prbcd.bisection(edge_weights, a, b, n_perturbations, epsilon=1e-05, iter_max=100000.0)[source]
- graphslim.models.prbcd.entropy(x: torch.Tensor) torch.Tensor
Entropy of softmax distribution from log_softmax.
- graphslim.models.prbcd.softmax_entropy(x: torch.Tensor) torch.Tensor
Entropy of softmax distribution from logits.
graphslim.models.reparam_module module
graphslim.models.sntk module
graphslim.models.random_attack module
- class graphslim.models.random_attack.RandomAttack(*args: Any, **kwargs: Any)[source]
Bases:
BaseAttackRandomly adding noise to the input graph
- Parameters:
model – model to attack. Default None.
nnodes (int) – number of nodes in the input graph
attack_structure (bool) – whether to attack graph structure
attack_features (bool) – whether to attack node features
device (str) – ‘cpu’ or ‘cuda’
Examples
>>> from deeprobust.graph.data import Dataset >>> from deeprobust.graph.global_attack import Random >>> data = Dataset(root='/tmp/', name='cora') >>> adj, features, labels = data.adj, data.features, data.labels >>> model = Random() >>> model.attack(adj, n_perturbations=10) >>> modified_adj = model.modified_adj
- attack(target, n_perturbations, type='add', **kwargs)[source]
Generate attacks on the input graph.
- Parameters:
ori_adj (scipy.sparse.csr_matrix) – Original (unperturbed) adjacency matrix.
n_perturbations (int) – Number of edge removals/additions.
type (str) – perturbation type. Could be ‘add’, ‘remove’ or ‘flip’.
- Return type:
None.
- inject_nodes(adj, n_add, n_perturbations)[source]
For each added node, randomly connect with other nodes.
- perturb_adj(adj, n_perturbations, type='add')[source]
Randomly add, remove or flip edges.
- Parameters:
adj (scipy.sparse.csr_matrix) – Original (unperturbed) adjacency matrix.
n_perturbations (int) – Number of edge removals/additions.
type (str) – perturbation type. Could be ‘add’, ‘remove’ or ‘flip’.
- Returns:
perturbed adjacency matrix
- Return type:
scipy.sparse matrix
- perturb_features(features, n_perturbations)[source]
Randomly perturb features by setting n_perturbations columns to zero.
- Parameters:
features (tensor) – The tensor of features to perturb.
n_perturbations (int) – The number of columns to set to zero.
- Returns:
The perturbed feature tensor.
- Return type:
tensor