graphslim package

Subpackages

Submodules

graphslim.configs module

graphslim.train_all module

graphslim.train_all.main()[source]

graphslim.utils module

graphslim.utils.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

graphslim.utils.add_feature_noise(data, noise_ratio, seed)[source]
graphslim.utils.add_feature_noise_test(data, noise_ratio, seed)[source]
graphslim.utils.add_mask(data, dataset)[source]

data: ogb-arxiv pyg data format

graphslim.utils.add_self_loops(edge_index, edge_weight=None, fill_value=1, num_nodes=None)[source]
graphslim.utils.calc(gntk, feat1, feat2, diag1, diag2, A1, A2)[source]
graphslim.utils.check_type(var, device)[source]
graphslim.utils.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

graphslim.utils.compute_alpha(n, sum_log_degrees, d_min)[source]
graphslim.utils.compute_log_likelihood(n, alpha, sum_log_degrees, d_min)[source]
graphslim.utils.degree_normalize_adj(mx)[source]

Row-normalize sparse matrix

graphslim.utils.degree_normalize_adj_tensor(adj, sparse=True)[source]

degree_normalize_adj_tensor.

graphslim.utils.degree_normalize_sparse_tensor(adj, fill_value=1)[source]

degree_normalize_sparse_tensor.

graphslim.utils.degree_sequence_log_likelihood(degree_sequence, d_min)[source]

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

graphslim.utils.dense_gcn_norm(adj, device)[source]
graphslim.utils.encode_onehot(labels)[source]

Convert label to onehot format.

Parameters:

labels (numpy.array) – node labels

Returns:

onehot labels

Return type:

numpy.array

graphslim.utils.f1_macro(output, labels, is_sigmoid=False)[source]

Return F1-macro score of output compared to labels.

Parameters:
  • output (torch.Tensor) – output from model

  • labels (torch.Tensor or numpy.array) – true labels (0 or 1)

Returns:

F1-macro score

Return type:

float

graphslim.utils.feature_smoothing(adj, X)[source]
graphslim.utils.gcn_norm(edge_index, edge_weight=None, num_nodes=None, improved=False, add_self_loops=True, flow='source_to_target', dtype=None)[source]
graphslim.utils.gcn_normalize_adj(adj, device='cpu')[source]
graphslim.utils.get_degree_squence(adj)[source]
graphslim.utils.get_splits_each_class(labels, train_size)[source]

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

graphslim.utils.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

graphslim.utils.get_train_test_labelrate(labels, label_rate)[source]

Get train test according to given label rate.

graphslim.utils.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

graphslim.utils.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

graphslim.utils.index_to_mask(index, size)[source]
graphslim.utils.is_identity(mx, device)[source]
graphslim.utils.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

graphslim.utils.loss_acc_fn_eval(data, k_ss, k_ts, y_support, y_target, reg=0.05)[source]
graphslim.utils.mask_to_index(index, size)[source]
graphslim.utils.maxdegree(adj)[source]
graphslim.utils.normalize_adj_sgformer(adj)[source]

Normalize the adjacency matrix. Works for both dense and sparse matrices.

Args: adj (torch.Tensor): The adjacency matrix (either dense or sparse COO). device (torch.device): The device to run the normalization on.

Returns: torch.Tensor or SparseTensor: The normalized adjacency matrix.

graphslim.utils.normalize_adj_tensor(adj, sparse=False)[source]

Normalize adjacency tensor matrix, return sparse or not

graphslim.utils.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

graphslim.utils.normalize_sparse_tensor(adj, fill_value=1)[source]

Normalize sparse tensor. Need to import torch_scatter

graphslim.utils.one_hot(x, class_count)[source]
graphslim.utils.one_hot_sfgc(x, num_classes, center=True, dtype=<class 'numpy.float32'>)[source]
graphslim.utils.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

graphslim.utils.regularization(adj, x, eig_real=None)[source]
graphslim.utils.reshape_mx(mx, shape)[source]
graphslim.utils.roc_auc(output, labels, is_sigmoid=False)[source]

Return ROC-AUC score of output compared to labels.

Parameters:
  • output (torch.Tensor) – output from model

  • labels (torch.Tensor or numpy.array) – true labels (0 or 1)

  • is_sigmoid (bool, optional) – If True, apply sigmoid thresholding on the output, by default False.

Returns:

ROC-AUC score

Return type:

float

graphslim.utils.row_normalize_tensor(mx)[source]
graphslim.utils.seed_everything(seed)[source]
graphslim.utils.sparse_gcn_norm(adj)[source]
graphslim.utils.sparse_mx_to_torch_sparse_tensor(sparse_mx)[source]

Convert a scipy sparse matrix to a torch sparse tensor.

graphslim.utils.sparsity(adj)[source]
graphslim.utils.sparsity2(adj)[source]
graphslim.utils.tensor2onehot(labels)[source]

Convert label tensor to label onehot tensor.

Parameters:

labels (torch.LongTensor) – node labels

Returns:

onehot labels tensor

Return type:

torch.LongTensor

graphslim.utils.to_camel_case(snake_str)[source]
graphslim.utils.to_scipy(tensor)[source]

Convert a dense/sparse tensor to scipy matrix

graphslim.utils.to_tensor(*vars, **kwargs)[source]
graphslim.utils.unravel_index(index, array_shape)[source]
graphslim.utils.update_sum_log_degrees(sum_log_degrees_before, n_old, d_old, d_new, d_min)[source]
graphslim.utils.updated_log_likelihood_for_edge_changes(node_pairs, adjacency_matrix, d_min)[source]

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

Module contents