import numpy as np import random import scipy.sparse as sp class GraphAugmentor(object): def __init__(self): pass @staticmethod def node_dropout(sp_adj, drop_rate): """Input: a sparse adjacency matrix and a dropout rate.""" adj_shape = sp_adj.get_shape() row_idx, col_idx = sp_adj.nonzero() drop_user_idx = random.sample(range(adj_shape[0]), int(adj_shape[0] * drop_rate)) drop_item_idx = random.sample(range(adj_shape[1]), int(adj_shape[1] * drop_rate)) indicator_user = np.ones(adj_shape[0], dtype=np.float32) indicator_item = np.ones(adj_shape[1], dtype=np.float32) indicator_user[drop_user_idx] = 0. indicator_item[drop_item_idx] = 0. diag_indicator_user = sp.diags(indicator_user) diag_indicator_item = sp.diags(indicator_item) mat = sp.csr_matrix( (np.ones_like(row_idx, dtype=np.float32), (row_idx, col_idx)), shape=(adj_shape[0], adj_shape[1])) mat_prime = diag_indicator_user.dot(mat).dot(diag_indicator_item) return mat_prime @staticmethod def edge_dropout(sp_adj, drop_rate): """Input: a sparse user-item adjacency matrix and a dropout rate.""" adj_shape = sp_adj.get_shape() edge_count = sp_adj.count_nonzero() row_idx, col_idx = sp_adj.nonzero() keep_idx = random.sample(range(edge_count), int(edge_count * (1 - drop_rate))) user_np = np.array(row_idx)[keep_idx] item_np = np.array(col_idx)[keep_idx] edges = np.ones_like(user_np, dtype=np.float32) dropped_adj = sp.csr_matrix((edges, (user_np, item_np)), shape=adj_shape) return dropped_adj