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) – 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.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.trainer¶
- class grb.utils.trainer.EarlyStop(patience=1000, epsilon=1e-05)[source]¶
Bases:
object
Strategy to early stop training process.
- class grb.utils.trainer.Trainer(dataset, optimizer, loss, adj_norm_func=None, feat_norm=None, lr_scheduler=False, early_stop=False, eval_metric=<function eval_acc>, device='cpu')[source]¶
Bases:
object
Trainer for GNNs.
- Parameters
dataset (grb.dataset.Dataset or grb.dataset.CustomDataset) – GRB supported dataset.
optimizer (torch.optim) – Optimizer for training.
loss (func of torch.nn.functional) – Loss function.
adj_norm_func (func of utils.normalize, optional) – Function that normalizes adjacency matrix. Default:
None
.feat_norm (str, optional) – Type of feature normalization, [‘arctan’, ‘tanh’]. Default:
None
.lr_scheduler (bool, optional) – Whether to use learning rate scheduler.
early_stop (bool, optional) – Whether to use early stop.
eval_metric (func of grb.metric, optional) – Evaluation metric, like accuracy or F1 score. Default:
grb.metric.eval_acc
.device (str, optional) – Device used to host data. Default:
cpu
.
- inference(model)[source]¶
Inference of a GNN model.
- Parameters
model (torch.nn.module) – Model implemented based on
torch.nn.module
.- Returns
logits (torch.Tensor) – Output logits of model.
test_score (float) – Score on test set.
- train(model, n_epoch, save_dir=None, save_name=None, eval_every=10, save_after=0, train_mode='trasductive', dropout=0.0, verbose=True)[source]¶
Train a GNN model.
- Parameters
model (torch.nn.module) – Model implemented based on
torch.nn.module
.n_epoch (int) – Number of epoch.
save_dir (str, optional) – Directory for saving model. Default:
None
.save_name (str, optional) – Name for saved model. Default:
None
.eval_every (int, optional) – Evaluation step. Default:
10
.save_after (int, optional) – Save after certain number of epoch. Default:
0
.train_mode (str, optional) – Training mode, [‘inductive’, ‘transductive’]. Default:
transductive
.dropout (float, optional) – Rate of dropout. Default:
0.0
.verbose (bool, optional) – Whether to display logs. Default:
False
.
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.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.
- Returns
- 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.- Returns
- 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.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.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
.