deeprobust.graph package¶
Subpackages¶
- deeprobust.graph.data package
- deeprobust.graph.defense package
- deeprobust.graph.global_attack package
- Submodules
- deeprobust.graph.global_attack.base_attack module
- deeprobust.graph.global_attack.dice module
- deeprobust.graph.global_attack.mettack module
- deeprobust.graph.global_attack.nipa module
- deeprobust.graph.global_attack.random_attack module
- deeprobust.graph.global_attack.topology_attack module
- Module contents
- deeprobust.graph.rl package
- Submodules
- deeprobust.graph.rl.env module
- deeprobust.graph.rl.nipa_config module
- deeprobust.graph.rl.nipa_env module
- deeprobust.graph.rl.nipa_nstep_replay_mem module
- deeprobust.graph.rl.nipa_q_net_node module
- deeprobust.graph.rl.nstep_replay_mem module
- deeprobust.graph.rl.q_net_node module
- deeprobust.graph.rl.rl_s2v_config module
- deeprobust.graph.rl.rl_s2v_env module
- Module contents
- deeprobust.graph.targeted_attack package
- Submodules
- deeprobust.graph.targeted_attack.base_attack module
- deeprobust.graph.targeted_attack.fga module
- deeprobust.graph.targeted_attack.ig_attack module
- deeprobust.graph.targeted_attack.nettack module
- deeprobust.graph.targeted_attack.rl_s2v module
- deeprobust.graph.targeted_attack.rnd module
- Module contents
Submodules¶
deeprobust.graph.black_box module¶
-
load_victim_model
(data, model_name='gcn', device='cpu', file_path=None)[source]¶ load_victim_model.
Parameters: - data (deeprobust.graph.Dataset) – graph data
- model_name (str) – victime model name, e.g. (‘gcn’, ‘deepwalk’) But currently it only supports gcn as victim model.
- device (str) – ‘cpu’ or ‘cuda’
- file_path – if given, the victim model will be loaded from this path.
deeprobust.graph.utils module¶
-
accuracy
(output, labels)[source]¶ Return accuracy of output compared to labels.
Parameters: - output (torch.Tensor) – output from model
- labels (torch.Tensor or numpy.array) – node labels
Returns: accuracy
Return type: float
-
classification_margin
(output, true_label)[source]¶ Calculate classification margin for outputs. probs_true_label - probs_best_second_class
Parameters: - output (torch.Tensor) – output vector (1 dimension)
- true_label (int) – true label for this node
Returns: classification margin for this node
Return type: list
-
degree_sequence_log_likelihood
(degree_sequence, d_min)[source]¶ Compute the (maximum) log likelihood of the Powerlaw distribution fit on a degree distribution.
-
encode_onehot
(labels)[source]¶ Convert label to onehot format.
Parameters: labels (numpy.array) – node labels Returns: onehot labels Return type: numpy.array
-
get_splits_each_class
(labels, train_size)[source]¶ We randomly sample n instances for class, where n = train_size.
-
get_train_test
(nnodes, test_size=0.8, stratify=None, seed=None)[source]¶ This function returns training and test set without validation. It can be used for settings of different label rates.
Parameters: - nnodes (int) – number of nodes in total
- test_size (float) – size of test set
- stratify – data is expected to split in a stratified fashion. So stratify should be labels.
- seed (int or None) – random seed
Returns: - idx_train – node training indices
- idx_test – node test indices
-
get_train_val_test
(nnodes, val_size=0.1, test_size=0.8, stratify=None, seed=None)[source]¶ This setting follows nettack/mettack, where we split the nodes into 10% training, 10% validation and 80% testing data
Parameters: - nnodes (int) – number of nodes in total
- val_size (float) – size of validation set
- test_size (float) – size of test set
- stratify – data is expected to split in a stratified fashion. So stratify should be labels.
- seed (int or None) – random seed
Returns: - idx_train – node training indices
- idx_val – node validation indices
- idx_test – node test indices
-
get_train_val_test_gcn
(labels, seed=None)[source]¶ This setting follows gcn, where we randomly sample 20 instances for each class as training data, 500 instances as validation data, 1000 instances as test data. Note here we are not using fixed splits. When random seed changes, the splits will also change.
Parameters: - labels (numpy.array) – node labels
- seed (int or None) – random seed
Returns: - idx_train – node training indices
- idx_val – node validation indices
- idx_test – node test indices
-
is_sparse_tensor
(tensor)[source]¶ Check if a tensor is sparse tensor.
Parameters: tensor (torch.Tensor) – given tensor Returns: whether a tensor is sparse tensor Return type: bool
-
likelihood_ratio_filter
(node_pairs, modified_adjacency, original_adjacency, d_min, threshold=0.004)[source]¶ Filter the input node pairs based on the likelihood ratio test proposed by Zügner et al. 2018, see https://dl.acm.org/citation.cfm?id=3220078. In essence, for each node pair return 1 if adding/removing the edge between the two nodes does not violate the unnoticeability constraint, and return 0 otherwise. Assumes unweighted and undirected graphs.
-
normalize_adj
(mx)[source]¶ Normalize sparse adjacency matrix, A’ = (D + I)^-1/2 * ( A + I ) * (D + I)^-1/2 Row-normalize sparse matrix
Parameters: mx (scipy.sparse.csr_matrix) – matrix to be normalized Returns: normalized matrix Return type: scipy.sprase.lil_matrix
-
normalize_feature
(mx)[source]¶ Row-normalize sparse matrix or dense matrix
Parameters: mx (scipy.sparse.csr_matrix or numpy.array) – matrix to be normalized Returns: normalized matrix Return type: scipy.sprase.lil_matrix
-
normalize_sparse_tensor
(adj, fill_value=1)[source]¶ Normalize sparse tensor. Need to import torch_scatter
-
preprocess
(adj, features, labels, preprocess_adj=False, preprocess_feature=False, sparse=False, device='cpu')[source]¶ Convert adj, features, labels from array or sparse matrix to torch Tensor, and normalize the input data.
Parameters: - adj (scipy.sparse.csr_matrix) – the adjacency matrix.
- features (scipy.sparse.csr_matrix) – node features
- labels (numpy.array) – node labels
- preprocess_adj (bool) – whether to normalize the adjacency matrix
- preprocess_feature (bool) – whether to normalize the feature matrix
- sparse (bool) – whether to return sparse tensor
- device (str) – ‘cpu’ or ‘cuda’
-
ravel_multiple_indices
(ixs, shape, reverse=False)[source]¶ “Flattens” multiple 2D input indices into indices on the flattened matrix, similar to np.ravel_multi_index. Does the same as ravel_index but for multiple indices at once. :param ixs: The array of n indices that will be flattened. :type ixs: array of ints shape (n, 2) :param shape: The shape of the corresponding matrix. :type shape: list or tuple of ints of length 2
Returns: The indices on the flattened matrix corresponding to the 2D input indices. Return type: array of n ints between 0 and shape[0]*shape[1]-1
-
sparse_mx_to_torch_sparse_tensor
(sparse_mx)[source]¶ Convert a scipy sparse matrix to a torch sparse tensor.
-
tensor2onehot
(labels)[source]¶ Convert label tensor to label onehot tensor.
Parameters: labels (torch.LongTensor) – node labels Returns: onehot labels tensor Return type: torch.LongTensor
-
to_tensor
(adj, features, labels=None, device='cpu')[source]¶ Convert adj, features, labels from array or sparse matrix to torch Tensor.
Parameters: - adj (scipy.sparse.csr_matrix) – the adjacency matrix.
- features (scipy.sparse.csr_matrix) – node features
- labels (numpy.array) – node labels
- device (str) – ‘cpu’ or ‘cuda’
-
updated_log_likelihood_for_edge_changes
(node_pairs, adjacency_matrix, d_min)[source]¶ Adopted from https://github.com/danielzuegner/nettack