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]
forward(x, adj, output_layer_features=False)[source]
initialize()[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

class graphslim.models.gcn.GCN(*args: Any, **kwargs: Any)[source]

Bases: BaseGNN

graphslim.models.appnp module

multiple transformaiton and multiple propagation

class graphslim.models.appnp.APPNP(*args: Any, **kwargs: Any)[source]

Bases: BaseGNN

forward(x, adj, output_layer_features=False)[source]
forward_sampler(x, adjs)[source]
class graphslim.models.appnp.SparseDropout(*args: Any, **kwargs: Any)[source]

Bases: Module

forward(x, training)[source]

graphslim.models.cheby module

class graphslim.models.cheby.Cheby(*args: Any, **kwargs: Any)[source]

Bases: BaseGNN

graphslim.models.gat module

Extended from https://github.com/rusty1s/pytorch_geometric/tree/master/benchmark/citation

class graphslim.models.gat.GAT(*args: Any, **kwargs: Any)[source]

Bases: BaseGNN

simple GAT model, one head and no edge weight only for evaluation

forward(x, adj, output_layer_features=False)[source]

graphslim.models.gntk module

class graphslim.models.gntk.GNTK(num_layers, num_mlp_layers, jk, scale)[source]

Bases: object

implement 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

gntk(feat1, feat2, diag_list1, diag_list2, A1, A2)[source]

compute the GNTK value Theta(g1, g2) g1: graph1 g2: graph2 diag_list1, diag_list2: g1, g2’s the diagonal elements of covariance matrix in all layers A1, A2: g1, g2’s adjacency matrix

show_memory(unit='KB', threshold=1)[source]

查看变量占用内存情况

Parameters:
  • unit – 显示的单位,可为`B`,`KB`,`MB`,`GB`

  • threshold – 仅显示内存数值大于等于threshold的变量

graphslim.models.graphsage module

class graphslim.models.graphsage.GraphSage(*args: Any, **kwargs: Any)[source]

Bases: BaseGNN

graphslim.models.sgc module

multiple transformaiton and multiple propagation

class graphslim.models.sgc.SGC(*args: Any, **kwargs: Any)[source]

Bases: BaseGNN

multiple transformation layers

forward(x, adj, output_layer_features=False)[source]

graphslim.models.layers module

class graphslim.models.layers.ChebConvolution(*args: Any, **kwargs: Any)

Bases: Module

Simple 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: MessagePassing

The 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: Module

Simple Linear layer, modified from https://github.com/tkipf/pygcn

forward(input)
reset_parameters()
class graphslim.models.layers.SageConvolution(*args: Any, **kwargs: Any)

Bases: Module

forward(x, adj_t)
reset_parameters()

graphslim.models.sgformer module

class graphslim.models.sgformer.GraphConv(*args: Any, **kwargs: Any)[source]

Bases: Module

forward(x, edge_index)[source]
reset_parameters()[source]
class graphslim.models.sgformer.GraphConvLayer(*args: Any, **kwargs: Any)[source]

Bases: Module

forward(x, adj, x0)[source]
reset_parameters()[source]
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]
forward(x, edge_index)[source]
get_attentions(x)[source]
predict(features=None, adj=None, normadj=True, output_layer_features=False)
reset_parameters()[source]
test(data, setting='trans', verbose=False)

Evaluate GCN performance on test set. :param idx_test: node testing indices

class graphslim.models.sgformer.TransConv(*args: Any, **kwargs: Any)[source]

Bases: Module

forward(x)[source]
get_attentions(x)[source]
reset_parameters()[source]
class graphslim.models.sgformer.TransConvLayer(*args: Any, **kwargs: Any)[source]

Bases: Module

transformer with fast attention

forward(query_input, source_input, output_attn=False)[source]
reset_parameters()[source]
graphslim.models.sgformer.normalize_adj(adj)[source]

Normalize the adjacency matrix (sparse COO tensor).

Args: adj (torch.sparse_coo_tensor): The sparse COO adjacency matrix.

Returns: torch.sparse_coo_tensor: The normalized sparse COO adjacency matrix.

graphslim.models.ignr module

class graphslim.models.ignr.EdgeBlock(*args: Any, **kwargs: Any)[source]

Bases: Module

forward(x)[source]
class graphslim.models.ignr.GraphonLearner(*args: Any, **kwargs: Any)[source]

Bases: Module

forward(c, inference=False, Lx=None)[source]
inference(c)
opt_loss(adj)[source]
reset_parameters()[source]
class graphslim.models.ignr.Sine(*args: Any, **kwargs: Any)[source]

Bases: Module

forward(input)[source]
graphslim.models.ignr.get_mgrid(sidelen, dim=2)[source]
graphslim.models.ignr.mx_inv(mx, device)[source]
graphslim.models.ignr.mx_inv_sqrt(mx)[source]
graphslim.models.ignr.mx_tr(mx)[source]

graphslim.models.krr module

class graphslim.models.krr.KernelRidgeRegression(*args: Any, **kwargs: Any)[source]

Bases: Module

forward(G_t, G_s, y_t, y_s, E_t, E_s)[source]

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

attack(edge_index=None, edge_weight=None, ptb_rate=0.1)[source]
get_modified_adj()[source]
get_perf(output, labels, mask, verbose=True)[source]
loss_attack(logits, labels, type='CE')[source]
pretrain_model(model=None)[source]
project(n_perturbations, values, eps, inplace=False)[source]
resample_random_block(n_perturbations: int)[source]
sample_final_edges(n_perturbations)
sample_random_block(n_perturbations)[source]
update_edge_weights(n_perturbations, epoch, gradient)[source]
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.grad_with_checkpoint(outputs, inputs)[source]
graphslim.models.prbcd.linear_to_full_idx(n: int, lin_idx: torch.Tensor) torch.Tensor[source]
graphslim.models.prbcd.linear_to_triu_idx(n: int, lin_idx: torch.Tensor) torch.Tensor[source]
graphslim.models.prbcd.softmax_entropy(x: torch.Tensor) torch.Tensor

Entropy of softmax distribution from logits.

graphslim.models.prbcd.to_symmetric(edge_index, edge_weight, n, op='mean')[source]

graphslim.models.reparam_module module

class graphslim.models.reparam_module.ReparamModule(*args: Any, **kwargs: Any)[source]

Bases: Module

clear_views()[source]
forward(*inputs, flat_param=None, buffers=None, **kwinputs)[source]
replaced_buffers(buffers)[source]
trace(example_input, **trace_kwargs)[source]
unflattened_param(flat_param)[source]

graphslim.models.sntk module

class graphslim.models.sntk.StructureBasedNeuralTangentKernel(*args: Any, **kwargs: Any)[source]

Bases: Module

aggr(S, aggr_optor, n1, n2, scale_mat)[source]
diag(g, E)[source]
nodes_gram(g1, g2, E1, E2)[source]
sparse_kron(A, B)[source]

A, B: torch.sparse.FloatTensor of shape (m, n) and (p, q) Returns: the Kronecker product of A and B

update_diag(S)[source]
update_sigma(S, diag1, diag2)[source]

graphslim.models.random_attack module

class graphslim.models.random_attack.RandomAttack(*args: Any, **kwargs: Any)[source]

Bases: BaseAttack

Randomly 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

random_sample_edges(adj, n, exclude)[source]
sample_forever(adj, exclude)[source]

Randomly random sample edges from adjacency matrix, exclude is a set which contains the edges we do not want to sample and the ones already sampled

graphslim.models.parametrized_adj module

class graphslim.models.parametrized_adj.PGE(*args: Any, **kwargs: Any)[source]

Bases: Module

forward(x, inference=False)[source]
inference(x)
reset_parameters()[source]