def sample(self): # batch, length, feature y_batch = np.zeros( (self.batch_size, self.n, self.max_prev_node)) # here zeros are padded for small graph # generate input x, y pairs for i in range(self.batch_size): # first sample and get a permuted adj adj_idx = np.random.randint(len(self.adj_all)) adj_copy = self.adj_all[adj_idx].copy() # print('graph size',adj_copy.shape[0]) x_idx = np.random.permutation(adj_copy.shape[0]) adj_copy = adj_copy[np.ix_(x_idx, x_idx)] adj_copy_matrix = np.asmatrix(adj_copy) G = nx.from_numpy_matrix(adj_copy_matrix) # then do bfs in the permuted G start_idx = np.random.randint(adj_copy.shape[0]) x_idx = np.array(bfs_seq(G, start_idx)) adj_copy = adj_copy[np.ix_(x_idx, x_idx)] # get the feature for the permuted G # encode adj adj_encoded = encode_adj(adj_copy.copy(), max_prev_node=self.max_prev_node) # get x and y and adj # for small graph the rest are zero padded y_batch[i, 0:adj_encoded.shape[0], :] = adj_encoded return torch.from_numpy(y_batch).float()
def sample(self): # batch, length, feature x_batch = np.zeros( (self.batch_size, self.n, self.max_prev_node)) # here zeros are padded for small graph y_batch = np.zeros( (self.batch_size, self.n, self.max_prev_node)) # here zeros are padded for small graph len_batch = np.zeros(self.batch_size) # generate input x, y pairs for i in range(self.batch_size): # first sample and get a permuted adj adj_idx = np.random.randint(len(self.adj_all)) adj_copy = self.adj_all[adj_idx].copy() len_batch[i] = adj_copy.shape[0] x_idx = np.random.permutation(adj_copy.shape[0]) adj_copy = adj_copy[np.ix_(x_idx, x_idx)] adj_copy_matrix = np.asmatrix(adj_copy) G = nx.from_numpy_matrix(adj_copy_matrix) # then do bfs in the permuted G start_idx = np.random.randint(adj_copy.shape[0]) x_idx = np.array(bfs_seq(G, start_idx)) adj_copy = adj_copy[np.ix_(x_idx, x_idx)] adj_encoded = encode_adj(adj_copy.copy(), max_prev_node=self.max_prev_node) # get x and y and adj # for small graph the rest are zero padded y_batch[i, 0:adj_encoded.shape[0], :] = adj_encoded x_batch[i, 1:adj_encoded.shape[0] + 1, :] = adj_encoded # sort in descending order len_batch_order = np.argsort(len_batch)[::-1] len_batch = len_batch[len_batch_order] x_batch = x_batch[len_batch_order, :, :] y_batch = y_batch[len_batch_order, :, :] return ( torch.from_numpy(x_batch).float(), torch.from_numpy(y_batch).float(), len_batch.astype("int").tolist(), )
def augmented_graph(config, dataset_loader, epoch=50, sparse=2, pendant=0, generate=True, test=False, draw_graph=False): config.epochs = epoch if not test: config.test_epochs = epoch if generate: graph_gen(config, dataset_loader) dataset = dataset_loader['train'].dataset.data original_adjacency_matrix = dataset['adjacency_matrix'].toarray().astype(np.float64) original_graph = nx.from_numpy_matrix(original_adjacency_matrix) synthetic_graph = read_synthetic_graph(config, epoch) synthetic_adjacency_matrix = nx.to_numpy_array(synthetic_graph) sparse_index = [] check = np.zeros(config.max_num_nodes) for idx in range(config.max_num_nodes): if check[idx] == 1: continue bfs_idx = np.array(data.bfs_seq(original_graph, idx)) check[bfs_idx] = 1 if bfs_idx.shape[0] <= sparse: sparse_index.extend(bfs_idx) if pendant > 0: degree_vector = original_adjacency_matrix.sum(axis=1) dependent_index = np.where(degree_vector <= pendant)[0] sparse_index.extend(dependent_index) sparse_index = np.unique(sparse_index) matrix_index = np.ix_(sparse_index, sparse_index) augmented_adjacency_matrix = deepcopy(original_adjacency_matrix) # augmented_adjacency_matrix[matrix_index] = 0 zero_index = np.where(augmented_adjacency_matrix[matrix_index] == 0) augmented_slice = augmented_adjacency_matrix[matrix_index] augmented_slice[zero_index] += synthetic_adjacency_matrix[matrix_index][zero_index] augmented_adjacency_matrix[matrix_index] = augmented_slice if draw_graph: pos = draw(augmented_adjacency_matrix) draw(synthetic_adjacency_matrix, pos) draw(original_adjacency_matrix, pos) return augmented_adjacency_matrix
def calc_max_prev_node(self, iter): max_prev_node = [] for i in range(iter): if i % (iter / 10) == 0: print(i) adj_idx = np.random.randint(len(self.adj_all)) adj_copy = self.adj_all[adj_idx].copy() # print('Graph size', adj_copy.shape[0]) x_idx = np.random.permutation(adj_copy.shape[0]) adj_copy = adj_copy[np.ix_(x_idx, x_idx)] adj_copy_matrix = np.asmatrix(adj_copy) G = nx.from_numpy_matrix(adj_copy_matrix) # then do bfs in the permuted G start_idx = np.random.randint(adj_copy.shape[0]) x_idx = np.array(bfs_seq(G, start_idx)) adj_copy = adj_copy[np.ix_(x_idx, x_idx)] # encode adj adj_encoded = encode_adj_flexible(adj_copy.copy()) max_encoded_len = max( [len(adj_encoded[i]) for i in range(len(adj_encoded))]) max_prev_node.append(max_encoded_len) max_prev_node = sorted(max_prev_node)[-100:] return max_prev_node
def sample(self): # generate input x, y pairs # first sample and get a permuted adj adj_idx = np.random.randint(len(self.adj_all)) adj_copy = self.adj_all[adj_idx].copy() # print('graph size',adj_copy.shape[0]) x_idx = np.random.permutation(adj_copy.shape[0]) adj_copy = adj_copy[np.ix_(x_idx, x_idx)] adj_copy_matrix = np.asmatrix(adj_copy) G = nx.from_numpy_matrix(adj_copy_matrix) # then do bfs in the permuted G start_idx = np.random.randint(adj_copy.shape[0]) x_idx = np.array(bfs_seq(G, start_idx)) adj_copy = adj_copy[np.ix_(x_idx, x_idx)] # get the feature for the permuted G # encode adj adj_encoded = encode_adj_flexible(adj_copy.copy()) # get x and y and adj # for small graph the rest are zero padded self.y_batch = adj_encoded return self.y_batch, adj_copy
def sample(self): # batch, length, feature # self.x_batch = np.ones((self.batch_size, self.n - 1, self.max_prev_node)) x_batch = np.zeros( (self.batch_size, self.n, self.max_prev_node)) # here zeros are padded for small graph # self.x_batch[:,0,:] = np.ones((self.batch_size, self.max_prev_node)) # first input is all ones # batch, length, feature y_batch = np.zeros( (self.batch_size, self.n, self.max_prev_node)) # here zeros are padded for small graph # batch, length, length adj_batch = np.zeros((self.batch_size, self.n, self.n)) # here zeros are padded for small graph # batch, size, size adj_norm_batch = np.zeros( (self.batch_size, self.n, self.n)) # here zeros are padded for small graph # batch, size, feature_len: degree and clustering coefficient if self.has_feature is None: feature_batch = np.zeros( (self.batch_size, self.n, self.n)) # use one hot feature else: feature_batch = np.zeros((self.batch_size, self.n, 2)) # generate input x, y pairs for i in range(self.batch_size): # first sample and get a permuted adj adj_idx = np.random.randint(len(self.adj_all)) adj_copy = self.adj_all[adj_idx].copy() # print('Graph size', adj_copy.shape[0]) x_idx = np.random.permutation(adj_copy.shape[0]) adj_copy = adj_copy[np.ix_(x_idx, x_idx)] adj_copy_matrix = np.asmatrix(adj_copy) G = nx.from_numpy_matrix(adj_copy_matrix) # then do bfs in the permuted G start_idx = np.random.randint(adj_copy.shape[0]) x_idx = np.array(bfs_seq(G, start_idx)) adj_copy = adj_copy[np.ix_(x_idx, x_idx)] # get the feature for the permuted G node_list = [G.nodes()[i] for i in x_idx] feature_degree = np.array(list( G.degree(node_list).values()))[:, np.newaxis] feature_clustering = np.array( list(nx.clustering(G, nodes=node_list).values()))[:, np.newaxis] # encode adj adj_encoded = encode_adj(adj_copy.copy(), max_prev_node=self.max_prev_node) # get x and y and adj # for small graph the rest are zero padded y_batch[i, 0:adj_encoded.shape[0], :] = adj_encoded x_batch[i, 1:adj_encoded.shape[0] + 1, :] = adj_encoded adj_batch[i, 0:adj_copy.shape[0], 0:adj_copy.shape[0]] = adj_copy adj_copy_norm = preprocess(adj_copy) adj_norm_batch[i, 0:adj_copy.shape[0], 0:adj_copy.shape[0]] = adj_copy_norm if self.has_feature is None: feature_batch[i, 0:adj_copy.shape[0], 0:adj_copy.shape[0]] = np.eye(adj_copy.shape[0]) else: feature_batch[i, 0:adj_copy.shape[0], :] = np.concatenate( (feature_degree, feature_clustering), axis=1) return ( torch.from_numpy(x_batch).float(), torch.from_numpy(y_batch).float(), torch.from_numpy(adj_batch).float(), torch.from_numpy(adj_norm_batch).float(), torch.from_numpy(feature_batch).float(), )