# deeprobust.graph package

## Subpackages

- deeprobust.graph.data package
  - Submodules
  - deeprobust.graph.data.attacked_data module
  - deeprobust.graph.data.dataset module
  - Module contents
- deeprobust.graph.defense package
  - Submodules
  - deeprobust.graph.defense.adv_training module
  - deeprobust.graph.defense.gcn module
  - deeprobust.graph.defense.gcn_preprocess module
  - deeprobust.graph.defense.pgd module
  - deeprobust.graph.defense.prognn module
  - deeprobust.graph.defense.r_gcn module
  - Module contents
- 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)

  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')

  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)

  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)

  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)

  Row-normalize sparse matrix

`degree_normalize_adj_tensor`(adj, sparse=True)

  degree_normalize_adj_tensor.

`degree_normalize_sparse_tensor`(adj, fill_value=1)

  degree_normalize_sparse_tensor.

`degree_sequence_log_likelihood`(degree_sequence, d_min)

  Compute the (maximum) log likelihood of the Powerlaw distribution fit on a degree distribution.

`encode_onehot`(labels)

  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)

  We randomly sample n instances for class, where n = train_size.

`get_train_test`(nnodes, test_size=0.8, stratify=None, seed=None)

  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)

  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)

  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)

  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)

  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)

  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)

  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)

  Normalize adjacency tensor matrix.

`normalize_feature`(mx)

  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)

  Normalize sparse tensor. Need to import torch_scatter

`preprocess`(adj, features, labels, preprocess_adj=False, preprocess_feature=False, sparse=False, device='cpu')

  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)

  “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)

  Convert a scipy sparse matrix to a torch sparse tensor.

`tensor2onehot`(labels)

  Convert label tensor to label onehot tensor.

  Parameters

    labels (torch.LongTensor) – node labels

  Returns

    onehot labels tensor

  Return type

    torch.LongTensor

`to_scipy`(tensor)

  Convert a dense/sparse tensor to scipy matrix

`to_tensor`(adj, features, labels=None, device='cpu')

  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)

  Adopted from https://github.com/danielzuegner/nettack

`visualize`(your_var)

  visualize computation graph

## Module contents