grb.utils

grb.utils.normalize

grb.utils.normalize.GCNAdjNorm(adj, order=-0.5)[source]

Normalization of adjacency matrix proposed in GCN.

Parameters
  • adj (scipy.sparse.csr.csr_matrix or torch.FloatTensor) – Adjacency matrix in form of N * N sparse matrix (or in form of N * N dense tensor).

  • order (float, optional) – Order of degree matrix. Default: -0.5.

Returns

adj – Normalized adjacency matrix in form of N * N sparse matrix.

Return type

scipy.sparse.csr.csr_matrix

grb.utils.normalize.RobustGCNAdjNorm(adj)[source]

Normalization of adjacency matrix proposed in RobustGCN.

Parameters

adj (tuple of scipy.sparse.csr.csr_matrix) – Tuple of adjacency matrix in form of N * N sparse matrix.

Returns

  • adj0 (scipy.sparse.csr.csr_matrix) – Adjacency matrix in form of N * N sparse matrix.

  • adj1 (scipy.sparse.csr.csr_matrix) – Adjacency matrix in form of N * N sparse matrix.

grb.utils.normalize.SAGEAdjNorm(adj, order=-1)[source]

Normalization of adjacency matrix proposed in GraphSAGE.

Parameters
  • adj (scipy.sparse.csr.csr_matrix) – Adjacency matrix in form of N * N sparse matrix.

  • order (float, optional) – Order of degree matrix. Default: -0.5.

Returns

adj – Normalized adjacency matrix in form of N * N sparse matrix.

Return type

scipy.sparse.csr.csr_matrix

grb.utils.normalize.SPARSEAdjNorm(adj, order=-0.5)[source]

Normalization of adjacency matrix proposed in GCN.

Parameters
  • adj (scipy.sparse.csr.csr_matrix or torch.FloatTensor) – Adjacency matrix in form of N * N sparse matrix (or in form of N * N dense tensor).

  • order (float, optional) – Order of degree matrix. Default: -0.5.

Returns

adj – Normalized adjacency matrix in form of N * N sparse matrix.

Return type

scipy.sparse.csr.csr_matrix

grb.utils.normalize.feature_normalize(features)[source]

grb.utils.trainer

grb.utils.utils

grb.utils.utils.adj_preprocess(adj, adj_norm_func=None, mask=None, model_type='torch', device='cpu')[source]

Preprocess the adjacency matrix.

Parameters
  • adj (scipy.sparse.csr.csr_matrix or a tuple) – Adjacency matrix in form of N * N sparse matrix.

  • adj_norm_func (func of utils.normalize, optional) – Function that normalizes adjacency matrix. Default: None.

  • mask (torch.Tensor, optional) – Mask of nodes in form of N * 1 torch bool tensor. Default: None.

  • model_type (str, optional) – Type of model’s backend, choose from [“torch”, “cogdl”, “dgl”]. Default: "torch".

  • device (str, optional) – Device used to host data. Default: cpu.

Returns

adj – Adjacency matrix in form of N * N sparse tensor or a tuple.

Return type

torch.Tensor or a tuple

grb.utils.utils.adj_to_tensor(adj)[source]

Convert adjacency matrix in scipy sparse format to torch sparse tensor.

Parameters

adj (scipy.sparse.csr.csr_matrix) – Adjacency matrix in form of N * N sparse matrix.

Returns

adj_tensor – Adjacency matrix in form of N * N sparse tensor.

Return type

torch.Tensor

grb.utils.utils.build_adj(attr, edge_index, adj_type='csr')[source]
grb.utils.utils.check_feat_range(features, feat_lim_min, feat_lim_max)[source]

Check if the generated features are within the limited range.

Parameters
  • features (torch.Tensor) – Features in form of torch tensor.

  • feat_lim_min (float) – Minimum limit of feature range.

  • feat_lim_max (float) – Maximum limit of feature range.

Return type

bool

grb.utils.utils.check_symmetry(adj)[source]

Check if the adjacency matrix is symmetric.

Parameters

adj (scipy.sparse.csr.csr_matrix) – Adjacency matrix in form of N * N sparse matrix.

Return type

bool

grb.utils.utils.download(url, save_path)[source]

Download dataset from URL.

Parameters
  • url (str) – URL to the dataset.

  • save_path (str) – Path to save the downloaded dataset.

grb.utils.utils.evaluate(model, features, adj, labels, feat_norm=None, adj_norm_func=None, eval_metric=<function eval_acc>, mask=None, device='cpu')[source]
Parameters
  • model (torch.nn.module) – Model implemented based on torch.nn.module.

  • features (torch.Tensor or numpy.array) – Features in form of torch tensor or numpy array.

  • adj (scipy.sparse.csr.csr_matrix) – Adjacency matrix in form of N * N sparse matrix.

  • labels (torch.Tensor or numpy.array) – Labels in form of torch tensor or numpy array.

  • feat_norm (str, optional) – Type of features normalization, choose from [“arctan”, “tanh”, None]. Default: None.

  • adj_norm_func (func of utils.normalize, optional) – Function that normalizes adjacency matrix. Default: None.

  • eval_metric (func of grb.metric, optional) – Evaluation metric, like accuracy or F1 score. Default: grb.metric.eval_acc.

  • mask (torch.tensor, optional) – Mask of target nodes. Default: None.

  • device (str, optional) – Device used to host data. Default: cpu.

Returns

score – Score on masked nodes.

Return type

float

grb.utils.utils.feat_preprocess(features, feat_norm=None, device='cpu')[source]

Preprocess the features.

Parameters
  • features (torch.Tensor or numpy.array) – Features in form of torch tensor or numpy array.

  • feat_norm (str, optional) – Type of features normalization, choose from [“arctan”, “tanh”, None]. Default: None.

  • device (str, optional) – Device used to host data. Default: cpu.

Returns

features – Features in form of torch tensor on chosen device.

Return type

torch.Tensor

grb.utils.utils.fix_seed(seed=0)[source]

Fix random process by a seed.

Parameters

seed (int, optional) – Random seed. Default: 0.

grb.utils.utils.get_index_induc(index_a, index_b)[source]

Get index under the inductive training setting.

Parameters
  • index_a (tuple) – Tuple of index.

  • index_b (tuple) – Tuple of index.

Returns

  • index_a_new (tuple) – Tuple of mapped index.

  • index_b_new (tuple) – Tuple of mapped index.

grb.utils.utils.get_num_params(model)[source]

Convert scipy sparse matrix to torch sparse tensor.

Parameters

model (torch.nn.module) – Model implemented based on torch.nn.module.

grb.utils.utils.inference(model, features, adj, feat_norm=None, adj_norm_func=None, device='cpu')[source]

Inference of model.

Parameters
  • model (torch.nn.module) – Model implemented based on torch.nn.module.

  • features (torch.Tensor or numpy.array) – Features in form of torch tensor or numpy array.

  • adj (scipy.sparse.csr.csr_matrix) – Adjacency matrix in form of N * N sparse matrix.

  • feat_norm (str, optional) – Type of features normalization, choose from [“arctan”, “tanh”, None]. Default: None.

  • adj_norm_func (func of utils.normalize, optional) – Function that normalizes adjacency matrix. Default: None.

  • device (str, optional) – Device used to host data. Default: cpu.

Returns

logits – Output logits of model.

Return type

torch.Tensor

grb.utils.utils.label_preprocess(labels, device='cpu')[source]

Convert labels to torch tensor.

Parameters
  • labels (torch.Tensor) – Labels in form of torch tensor.

  • device (str, optional) – Device used to host data. Default: cpu.

Returns

labels – Features in form of torch tensor on chosen device.

Return type

torch.Tensor

grb.utils.utils.save_adj(adj, file_dir, file_name='adj.pkl')[source]

Save generated adversarial adjacency matrix.

Parameters
  • adj (scipy.sparse.csr.csr_matrix or a tuple) – Adjacency matrix in form of N * N sparse matrix.

  • file_dir (str) – Directory to save the file.

  • file_name (str, optional) – Name of file to save. Default: adj.pkl.

grb.utils.utils.save_df_to_csv(df, file_dir, file_name='result.csv', verbose=False)[source]

Save dataframe to .csv file.

Parameters
  • df (pandas.DataFrame) – Dataframe containing evaluation results.

  • file_dir (str) – Directory to save the file.

  • file_name (str, optional) – Name of saved file. Default: result.csv.

  • verbose (bool, optional) – Whether to display logs. Default: False.

grb.utils.utils.save_df_to_xlsx(df, file_dir, file_name='result.xlsx', verbose=False)[source]

Save dataframe to .xlsx file.

Parameters
  • df (pandas.DataFrame) – Dataframe containing evaluation results.

  • file_dir (str) – Directory to save the file.

  • file_name (str, optional) – Name of saved file. Default: result.xlsx.

  • verbose (bool, optional) – Whether to display logs. Default: False.

grb.utils.utils.save_dict_to_json(result_dict, file_dir, file_name, verbose=False)[source]

Save dictinary to .json file.

Parameters
  • result_dict (dict) – Dictionary containing evaluation results.

  • file_dir (str) – Directory to save the file.

  • file_name (str) – Name of saved file.

  • verbose (bool, optional) – Whether to display logs. Default: False.

grb.utils.utils.save_dict_to_xlsx(result_dict, file_dir, file_name='result.xlsx', index=0, verbose=False)[source]

Save result dictionary to .xlsx file.

Parameters
  • result_dict (dict) – Dictionary containing evaluation results.

  • file_dir (str) – Directory to save the file.

  • file_name (str, optional) – Name of saved file. Default: result.xlsx.

  • index (int, optional) – Index of dataframe. Default: 0.

  • verbose (bool, optional) – Whether to display logs. Default: False.

grb.utils.utils.save_features(features, file_dir, file_name='features.npy')[source]

Save generated adversarial features.

Parameters
  • features (torch.Tensor or numpy.array) – Features in form of torch tensor or numpy array.

  • file_dir (str) – Directory to save the file.

  • file_name (str, optional) – Name of file to save. Default: features.npy.

grb.utils.utils.save_model(model, save_dir, name, verbose=True)[source]

Save trained model.

Parameters
  • model (torch.nn.module) – Model implemented based on torch.nn.module.

  • save_dir (str) – Directory to save the model.

  • name (str) – Name of saved model.

  • verbose (bool, optional) – Whether to display logs. Default: False.

grb.utils.visualize

grb.utils.visualize.plot_graph(adj, pos, labels, nodelist=None, figsize=(12, 12), title=None)[source]