예제 #1
0
 def __init__(self, input_dim, data, dist_calculator):
     self.input_dim = input_dim
     print('original_input_dim', self.input_dim)
     self.laplacians_1, self.laplacians_2, self.features_1, self.features_2, \
     self.num_nonzero_1, self.num_nonzero_2, self.dropout, \
     self.val_test_laplacians_1, self.val_test_laplacians_2, \
     self.val_test_features_1, self.val_test_features_2, \
     self.val_test_num_nonzero_1, self.val_test_num_nonzero_2 = \
         self._create_basic_placeholders(FLAGS.batch_size, FLAGS.batch_size,
                                         level=get_coarsen_level())
     self.train_y_true = tf.placeholder(tf.float32,
                                        shape=(FLAGS.batch_size, 1))
     self.val_test_y_true = tf.placeholder(tf.float32, shape=(1, 1))
     # Build the model.
     super(SiameseRegressionModel, self).__init__()
     self.sim_kernel = create_sim_kernel(FLAGS.sim_kernel,
                                         get_flags('yeta'),
                                         get_flags('scale'))
     self.train_triples = self._load_train_triples(data, dist_calculator)
예제 #2
0
 def __init__(self, input_dim, data, dist_sim_calculator):
     self.input_dim = input_dim
     print('original_input_dim', self.input_dim)
     if is_transductive():
         self._create_transductive_gembs_placeholders(
             data, FLAGS.batch_size, FLAGS.batch_size)
     else:
         self._create_basic_placeholders(FLAGS.batch_size,
                                         FLAGS.batch_size,
                                         level=get_coarsen_level())
     self.train_y_true = tf.placeholder(tf.float32,
                                        shape=(FLAGS.batch_size, 1))
     self.val_test_y_true = tf.placeholder(tf.float32, shape=(1, 1))
     # Build the model.
     super(SiameseRegressionModel, self).__init__()
     self.ds_kernel = create_ds_kernel(FLAGS.ds_kernel, get_flags('yeta'),
                                       get_flags('scale'))
     self.train_triples = self._load_train_triples(data,
                                                   dist_sim_calculator)
예제 #3
0
파일: model.py 프로젝트: zslwyuan/UGraphEmb
 def _gemb_layer_id(self):
     id = get_flags('gemb_layer_id')
     if id is not None:
         assert (id >= 1)
         id -= 1
     else:
         for i, layer in enumerate(self.layers):
             if layer.produce_graph_level_emb() and FLAGS.coarsening:
                 return i
     return id
예제 #4
0
파일: model.py 프로젝트: zslwyuan/UGraphEmb
    def _build(self):
        # Create layers according to FLAGS.
        self.layers = create_layers(self, 'layer', FLAGS.layer_num)
        assert (len(self.layers) > 0)
        print('Created {} layers: {}'.format(
            len(self.layers), ', '.join(l.get_name() for l in self.layers)))
        self.gembd_layer_id = self._gemb_layer_id()
        print('Graph embedding layer index (0-based): {}'.format(
            self.gembd_layer_id))

        # Build the siamese model for train and val_test, respectively,
        for tvt in ['train', 'val_test']:
            print(tvt)
            # Go through each layer except the last one.
            acts = [self._get_ins(self.layers[0], tvt)]
            outs = None
            for k, layer in enumerate(self.layers):
                print(layer.name)
                ins = self._proc_ins(acts[-1], k, layer, tvt)
                outs = layer(ins)
                outs = self._proc_outs(outs, k, layer, tvt)
                acts.append(outs)
            if tvt == 'train':
                self.train_outputs = outs
                self.train_acts = acts
            else:
                self.val_test_output = outs
                self.val_test_pred_score = self._val_test_pred_score()
                self.val_test_acts = acts

        self.node_embeddings = self._get_all_gcn_layer_outputs('val_test')

        if get_flags('branch_from'):
            branch_from = FLAGS.branch_from
            assert (branch_from >= 1)  # 1-based
            self._build_branch(branch_from)

        # Store model variables for easy access.
        variables = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES)
        self.vars = {var.name: var for var in variables}
예제 #5
0
파일: eval.py 프로젝트: yunshengb/GraphSim
 def _eval(self, models, rs, true_r, metrics, plot, loss_list=None,
           node_embs_dict=None, graph_embs_mat=None, attentions=None,
           extra_dir=None, model=None, data=None):
     rtn = OrderedDict()
     for metric in metrics:
         if metric == 'mrr' or metric == 'mse' or metric == 'dev' \
                 or metric == 'time' or 'acc' in metric \
                 or metric == 'kendalls_tau' or metric == 'spearmans_rho':
             d = plot_single_number_metric(
                 FLAGS.dataset_val_test, FLAGS.ds_metric, models, rs, true_r, metric,
                 self.norms,
                 ds_kernel=create_ds_kernel(get_flags('ds_kernel'),
                                            yeta=get_flags('yeta'),
                                            scale=get_flags('scale'))
                 if get_flags('ds_kernel') else None,
                 thresh_poss=[get_flags('thresh_val_test_pos')],
                 thresh_negs=[get_flags('thresh_val_test_neg')],
                 thresh_poss_sim=[0.5],
                 thresh_negs_sim=[0.5],
                 plot_results=plot, extra_dir=extra_dir)
             rtn.update(d)
         elif metric == 'draw_heat_hist':
             if node_embs_dict is not None:
                 draw_emb_hist_heat(
                     FLAGS.dataset_val_test,
                     node_embs_dict,
                     true_result=true_r,
                     ds_norm=FLAGS.ds_norm,
                     extra_dir=extra_dir + '/mne',
                     plot_max_num=FLAGS.plot_max_num)
         elif metric == 'ranking':
             em = self._get_node_mappings(data)
             draw_ranking(
                 FLAGS.dataset_val_test, FLAGS.ds_metric, true_r, rs[FLAGS.model],
                 node_feat_name=FLAGS.node_feat_name,
                 model_name=FLAGS.model_name,
                 plot_node_ids=FLAGS.dataset_val_test != 'webeasy' and em and not FLAGS.supersource,
                 plot_gids=False,
                 ds_norm=FLAGS.ds_norm,
                 existing_mappings=em,
                 extra_dir=extra_dir + '/ranking',
                 plot_max_num=FLAGS.plot_max_num)
         elif metric == 'attention':
             if attentions is not None:
                 draw_attention(
                     FLAGS.dataset_val_test, true_r, attentions,
                     extra_dir=extra_dir + '/attention',
                     plot_max_num=FLAGS.plot_max_num)
         elif 'prec@k' in metric:
             d = plot_preck(
                 FLAGS.dataset_val_test, FLAGS.ds_metric, models, rs, true_r, metric,
                 self.norms, plot, extra_dir=extra_dir)
             rtn.update(d)
         elif metric == 'loss':
             rtn.update({metric: np.mean(loss_list)})
         elif metric == 'train_pair_ged_vis':
             if FLAGS.ds_metric == 'ged':
                 plot_dist_hist(FLAGS.ds_metric, [FLAGS.dataset_val_test],
                                [self._transform_train_pairs_to_dist_list(model)],
                                extra_dir)
         elif metric == 'graph_classification':
             if graph_embs_mat is not None:
                 results_dict = graph_classification(
                     FLAGS.dataset_val_test, graph_embs_mat,
                     FLAGS.ds_metric,
                     extra_dir=extra_dir)
                 rtn.update(results_dict)
         else:
             raise RuntimeError('Unknown metric {}'.format(metric))
     return rtn
예제 #6
0
파일: eval.py 프로젝트: yunshengb/SimGNN
 def _eval(self, models, rs, true_r, loss_list, metrics, plot,
           node_embs_list=None, graph_embs_mat=None, attentions=None,
           eps_dir=None):
     rtn = OrderedDict()
     for metric in metrics:
         if metric == 'mrr' or metric == 'mse' or metric == 'time' or \
                 'acc' in metric or metric == 'kendalls_tau' or \
                 metric == 'spearmans_rho':
             d = plot_single_number_metric(
                 FLAGS.dataset, models, rs, true_r, metric,
                 self.norms,
                 sim_kernel=get_flags('sim_kernel'),
                 yeta=get_flags('yeta'),
                 scale=get_flags('scale'),
                 thresh_poss=[get_flags('thresh_val_test_pos')],
                 thresh_negs=[get_flags('thresh_val_test_neg')],
                 thresh_poss_sim=[0.5],
                 thresh_negs_sim=[0.5],
                 plot_results=plot, eps_dir=eps_dir)
             rtn.update(d)
         elif metric == 'draw_gt_rk':
             comb_gt_rk(FLAGS.dataset, FLAGS.dist_algo,
                        rs[FLAGS.model], eps_dir + '/gt_rk')
         elif metric == 'groundtruth':
             pass
         elif metric == 'draw_heat_hist':
             if node_embs_list is not None:
                 draw_emb_hist_heat(
                     FLAGS.dataset, node_embs_list, FLAGS.dist_norm,
                     max_nodes=FLAGS.max_nodes,
                     apply_sigmoid=True,
                     eps_dir=eps_dir + '/mne')
         elif metric == 'emb_vis_gradual':
             if graph_embs_mat is not None:
                 visualize_embeddings_gradual(
                     FLAGS.dataset,
                     graph_embs_mat,
                     eps_dir=eps_dir + '/emb_vis_gradual')
         elif metric == 'ranking':
             pass
             # ranking(
             #     FLAGS.dataset, FLAGS.dist_algo, rs[FLAGS.model],
             #     eps_dir=eps_dir + '/ranking'
             # )
         elif metric == 'attention':
             if attentions is not None:
                 draw_attention(
                     FLAGS.dataset, FLAGS.dist_algo, attentions,
                     eps_dir=eps_dir + '/attention')
         elif metric == 'auc':
             auc_score = auc(
                 true_r, rs[FLAGS.model],
                 thresh_pos=
                 get_flags('thresh_val_test_pos'),
                 thresh_neg=
                 get_flags('thresh_val_test_neg'),
                 norm=FLAGS.dist_norm)
             print('auc', auc_score)
             rtn.update({'auc': auc_score})
         elif 'prec@k' in metric:
             d = plot_preck(
                 FLAGS.dataset, models, rs, true_r, metric,
                 self.norms, plot, eps_dir=eps_dir)
             rtn.update(d)
         elif metric == 'loss':
             rtn.update({metric: np.mean(loss_list)})
         elif metric == 'emb_vis_binary':
             if graph_embs_mat is not None:
                 visualize_embeddings_binary(
                     FLAGS.dataset, graph_embs_mat,
                     self.true_test_result,
                     thresh_pos=
                     get_flags('thresh_val_test_pos'),
                     thresh_neg=
                     get_flags('thresh_val_test_neg'),
                     thresh_pos_sim=0.5,
                     thresh_neg_sim=0.5,
                     norm=FLAGS.dist_norm,
                     eps_dir=eps_dir + '/emb_vis_binary')
         else:
             raise RuntimeError('Unknown metric {}'.format(metric))
     return rtn