deeprobust.graph package

Subpackages

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.
train_victim_model(data, model_name='gcn', file_path=None, device='cpu')[source]

Train the victim model (target classifer) and save the model Note that the attacker can only do black query to this model.

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_normalize_adj(mx)[source]

Row-normalize sparse matrix

degree_normalize_adj_tensor(adj, sparse=True)[source]

degree_normalize_adj_tensor.

degree_normalize_sparse_tensor(adj, fill_value=1)[source]

degree_normalize_sparse_tensor.

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_test_labelrate(labels, label_rate)[source]

Get train test according to given label rate.

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_adj_tensor(adj, sparse=False)[source]

Normalize adjacency tensor 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_scipy(tensor)[source]

Convert a dense/sparse tensor to scipy matrix

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

visualize(your_var)[source]

visualize computation graph

Module contents