def get_neighbors(self, n_id): neighbors = [] neighbor_src = [] total_fanout = 0 for i in range(len(self.metapath)): n_id = tf.reshape(n_id, [-1]) if i == len(self.metapath) - 1: one_neighbor = tf_euler.get_full_neighbor( n_id, self.metapath[i])[0] else: total_fanout += self.fanouts[i] one_neighbor = tf_euler.sample_node(total_fanout, self.metapath[i][0]) one_neighbor, _ = tf.unique(one_neighbor) one_neighbor = tf_euler.sparse_get_adj(n_id, one_neighbor, self.metapath[i], -1, -1) neighbors.append(tf.reshape(one_neighbor.values, [-1])) one_indices = one_neighbor.indices[:, 0] neighbor_src.append(tf.cast(one_indices, tf.int32)) new_n_id = tf.reshape(one_neighbor.values, [-1]) n_id = tf.concat([new_n_id, n_id], axis=0) n_id, _ = tf.unique(n_id) return neighbors, neighbor_src
def sampler(self, inputs): batch_size = tf.size(inputs) num_negs = self.num_negs src, pos, negs = [], [], [] for i in range(self.walk_num): path = tf_euler.random_walk(inputs, [self.edge_type] * self.walk_len, default_node=self.max_id + 1) pair = tf_euler.gen_pair(path, self.left_win_size, self.right_win_size) num_pairs = pair.shape[1] src_, pos_ = tf.split(pair, [1, 1], axis=-1) negs_ = tf_euler.sample_node(batch_size * num_pairs * num_negs, self.node_type) src_ = tf.reshape(src_, [batch_size * num_pairs, 1]) pos_ = tf.reshape(pos_, [batch_size * num_pairs, 1]) negs_ = tf.reshape(negs_, [batch_size * num_pairs, num_negs]) src.append(src_) pos.append(pos_) negs.append(negs_) if self.walk_num == 1: return src[0], pos[0], negs[0] # no need to concat src = tf.concat(src, axis=0, name='src') pos = tf.concat(pos, axis=0, name='pos') negs = tf.concat(negs, axis=0, name='negs') return src, pos, negs
def to_sample(self, inputs): batch_size = tf.size(inputs) src = tf.expand_dims(inputs, -1) pos = tf_euler.sample_neighbor(inputs, self.edge_type, 1, self.max_id + 1)[0] negs = tf_euler.sample_node(batch_size * self.num_negs, self.node_type) negs = tf.reshape(negs, [batch_size, self.num_negs]) return src, pos, negs
def __call__(self, inputs): batch_size = tf.shape(inputs)[0] neg_nodes_group = [] for n_type in self.neg_type: neg_node = tf_euler.sample_node(batch_size * self.num_negs, n_type) neg_node = tf.reshape(neg_node, [batch_size, self.num_negs]) neg_nodes_group.append(neg_node) if len(self.neg_type) == 1: return neg_nodes_group[0] else: return neg_nodes_group
def sampler(self, inputs): batch_size = tf.size(inputs) path = tf_euler.random_walk(inputs, [self.edge_type] * self.walk_len, default_node=self.max_id + 1) pair = tf_euler.gen_pair(path, self.left_win_size, self.right_win_size) num_pairs = pair.shape[1] src, pos = tf.split(pair, [1, 1], axis=-1) negs = tf_euler.sample_node(batch_size * num_pairs * self.num_negs, self.node_type) src = tf.reshape(src, [batch_size * num_pairs, 1]) pos = tf.reshape(pos, [batch_size * num_pairs, 1]) negs = tf.reshape(negs, [batch_size * num_pairs, self.num_negs]) return src, pos, negs
def sample_subgraph_fanout(self, batch_size, degrees, edge_types): tf.reset_default_graph() self.session = tf.Session() # sample source nodes at random src_nodes = tf_euler.sample_node(batch_size, tf_euler.ALL_NODE_TYPE) src_nodes = self.session.run(src_nodes) batchs = [] for src_node in src_nodes: cur_nodes = np.asarray([src_node]).astype(np.int64) neighbors, weights, etypes = self.sample_fanout( cur_nodes, edge_types, degrees) sample = [[neighbors[0][0]]] for s in zip(neighbors[1:], weights, etypes): sample.append([i.tolist() for i in s]) batchs.append(sample) return batchs
labels = tf_euler.get_dense_feature(inputs, [self.label_idx], [self.label_dim])[0] return inputs, labels def decoder(self, embedding, labels): logits = self.predict_layer(embedding) loss = tf.nn.softmax_cross_entropy_with_logits(labels=labels, logits=logits) #predictions = tf.floor(tf.nn.sigmoid(logits) + 0.5) #f1 = tf_euler.metrics.f1_score(labels, predictions) return tf.reduce_mean(loss) #, f1 tf_euler.initialize_embedded_graph('reddit') source = tf_euler.sample_node(1000, 0) source.set_shape([1000]) model = GraphSage(0, 41, [[0], [0]], [4, 4], 64, 1, 602) _, loss = model(source) global_step = tf.train.get_or_create_global_step() train_op = tf.train.AdamOptimizer(0.03).minimize(loss, global_step) tf.logging.set_verbosity(tf.logging.INFO) with tf.train.MonitoredTrainingSession(hooks=[ tf.train.LoggingTensorHook({ 'step': global_step, 'loss': loss }, 100), tf.train.StopAtStepHook(2000)
[self.label_dim])[0] return inputs, labels def decoder(self, embedding, labels): logits = self.predict_layer(embedding) loss = tf.nn.sigmoid_cross_entropy_with_logits(labels=labels, logits=logits) predictions = tf.floor(tf.nn.sigmoid(logits) + 0.5) f1 = tf_euler.metrics.f1_score(labels, predictions) return tf.reduce_mean(loss), f1 if __name__ == '__main__': print("begin....") tf_euler.initialize_embedded_graph('ppi') # 图数据目录 source = tf_euler.sample_node(512, 0) source.set_shape([512]) model = GraphSage(0, 121, [[0], [0]], [10, 10], 256, 1, 50) _, loss, metric_name, metric = model(source) global_step = tf.train.get_or_create_global_step() train_op = tf.train.AdamOptimizer(0.01).minimize(loss, global_step) tf.logging.set_verbosity(tf.logging.INFO) with tf.train.MonitoredTrainingSession(hooks=[ tf.train.LoggingTensorHook( { 'step': global_step, 'loss': loss, metric_name: metric
def get_train_from_input(self, inputs, params): result = tf_euler.sample_node(inputs, params['train_node_type']) result.set_shape([self.params['batch_size']]) return result
def generate_negative(self, batch_size): return tf_euler.sample_node(batch_size * self.num_negs, self.node_type)
def decoder(self, embedding, embedding_pos, embedding_negs): logits = tf.matmul(embedding, embedding_pos, transpose_b=True) neg_logits = tf.matmul(embedding, embedding_negs, transpose_b=True) true_xent = tf.nn.sigmoid_cross_entropy_with_logits( labels=tf.ones_like(logits), logits=logits) negative_xent = tf.nn.sigmoid_cross_entropy_with_logits( labels=tf.zeros_like(neg_logits), logits=neg_logits) loss = tf.reduce_sum(true_xent) + tf.reduce_sum(negative_xent) mrr = tf_euler.metrics.mrr_score(logits, neg_logits) return loss, mrr if __name__ == '__main__': print("begin....") tf_euler.initialize_embedded_graph('ppi') # 图数据目录 source = tf_euler.sample_node(128, tf_euler.ALL_NODE_TYPE) source.set_shape([128]) model = DeepWalk(tf_euler.ALL_NODE_TYPE, [0, 1], 56944, 256) _, loss, metric_name, metric = model(source) global_step = tf.train.get_or_create_global_step() train_op = tf.train.GradientDescentOptimizer(0.2).minimize( loss, global_step) tf.logging.set_verbosity(tf.logging.INFO) with tf.train.MonitoredTrainingSession(hooks=[ tf.train.LoggingTensorHook( { 'step': global_step, 'loss': loss,