Exemple #1
0
    def __init__(self):
        """initialize the parameters, prepare the data and build the network"""

        self.n_node, self.linked_nodes = utils.read_edges(
            config.train_filename, config.test_filename, config.n_node)
        self.discriminator = None
        self.generator = None
        print self.n_node
        self.n_node = config.n_node  # If self.n_node is smaller than config.n_node, there is isolated nodes.
        self.root_nodes = [i for i in range(self.n_node)]
        print("start reading initial embeddings")
        # read the initial embeddings
        self.node_embed_init_d = utils.read_emd(
            filename=config.pretrain_emd_filename_d,
            n_node=config.n_node,
            n_embed=config.n_embed)
        self.node_embed_init_g = utils.read_emd(
            filename=config.pretrain_emd_filename_g,
            n_node=config.n_node,
            n_embed=config.n_embed)
        print("finish reading initial embeddings")
        # use the BFS to construct the trees
        print("Constructing Trees")
        if config.import_tree:
            self.trees = pickle.load(open(config.tree_path, 'r'))
        else:
            if config.app == "recommendation":
                self.mul_construct_trees_for_recommend(self.user_nodes)
            else:  # classification
                self.mul_construct_trees(self.root_nodes)
            pickle.dump(self.trees, open(config.tree_path, 'w'))
        config.max_degree = utils.get_max_degree(self.linked_nodes)
        self.build_gan()
        self.initialize_network()
Exemple #2
0
    def __init__(self):
        """initialize the parameters, prepare the data and build the network"""

        self.n_node, self.linked_nodes = utils.read_edges(
            config.train_filename, config.test_filename)
        self.root_nodes = [i for i in range(self.n_node)]
        self.discriminator = None
        self.generator = None
        assert self.n_node == config.n_node
        print("start reading initial embeddings")
        # read the initial embeddings
        self.node_embed_init_d = utils.read_emd(
            filename=config.pretrain_emd_filename_d,
            n_node=config.n_node,
            n_embed=config.n_embed)
        self.node_embed_init_g = utils.read_emd(
            filename=config.pretrain_emd_filename_g,
            n_node=config.n_node,
            n_embed=config.n_embed)
        print("finish reading initial embeddings")
        # use the BFS to construct the trees
        print("Constructing Trees")
        if config.app == "recommendation":
            self.mul_construct_trees_for_recommend(self.user_nodes)
        else:  # classification
            self.mul_construct_trees(self.root_nodes)
        config.max_degree = utils.get_max_degree(self.linked_nodes)

        self.generator = Generator(lambda_gen=config.lambda_gen,
                                   node_emd_init=self.node_embed_init_g)
        self.discriminator = Discriminator(
            lambda_dis=config.lambda_dis, node_emd_init=self.node_embed_init_d)
        self.generator.cuda()
        self.discriminator.cuda()
        self.all_score = None
Exemple #3
0
    def __init__(self):
        """initialize the parameters, prepare the data and build the network"""

        self.n_node, self.linked_nodes = utils.read_edges(
            config.train_filename, config.test_filename)
        self.root_nodes = [i for i in range(self.n_node)]
        self.discriminator = None
        self.generator = None
        assert self.n_node == config.n_node
        print("start reading initial embeddings")
        # read the initial embeddings
        self.node_embed_init_d = utils.read_emd(
            filename=config.pretrain_emd_filename_d,
            n_node=config.n_node,
            n_embed=config.n_embed)
        self.node_embed_init_g = utils.read_emd(
            filename=config.pretrain_emd_filename_g,
            n_node=config.n_node,
            n_embed=config.n_embed)
        print("finish reading initial embeddings")
        # use the BFS to construct the trees
        print("Constructing Trees")
        if config.app == "recommendation":
            self.mul_construct_trees_for_recommend(self.user_nodes)
        else:  # classification
            self.mul_construct_trees(self.root_nodes)
        config.max_degree = utils.get_max_degree(self.linked_nodes)
        self.build_gan()
        self.config = tf.ConfigProto()
        self.config.gpu_options.allow_growth = True
        self.init_op = tf.group(tf.global_variables_initializer(),
                                tf.local_variables_initializer())
        self.sess = tf.Session(config=self.config)
        self.sess.run(self.init_op)
Exemple #4
0
 def __init__(self, embed_filename, test_filename, test_neg_filename, n_node, n_embed):
     self.embed_filename = embed_filename  # each line: node_id, embeddings[n_embed]
     self.test_filename = test_filename  # each line: node_id1, node_id2
     self.test_neg_filename = test_neg_filename
     self.n_node = n_node
     self.n_embed = n_embed
     self.emd = utils.read_emd(embed_filename, n_node=n_node, n_embed=n_embed)
Exemple #5
0
 def __init__(self, embed_filename, test_filename, test_neg_filename,
              n_node, n_embed, nFeatures, emdFeaturesFilename,
              FeatWtFilename, modes):
     self.embed_filename = embed_filename  # each line: node_id, embeddings[n_embed]
     self.test_filename = test_filename  # each line: node_id1, node_id2
     self.test_neg_filename = test_neg_filename
     self.n_node = n_node
     self.n_embed = n_embed
     self.emd = utils.read_emd(embed_filename,
                               n_node=n_node,
                               n_embed=n_embed)
     self.emdFeatures = utils.read_emd(emdFeaturesFilename,
                                       n_node=n_node,
                                       n_embed=nFeatures)
     self.FeatureWtFilename = FeatWtFilename
     self.mode = modes
Exemple #6
0
 def __init__(self):
     """initialize the parameters, prepare the data and build the network"""
     self.n_node, self.linked_nodes = utils.read_edges(
         config.train_filename, config.test_filename)
     self.root_nodes = [i for i in range(self.n_node)
                        ]  #root nodes for each node
     self.discriminator = None  #none right now
     self.generator = None
     assert self.n_node == config.n_node  # if equal
     print("start reading initial embeddings")
     # read the initial embeddings
     self.node_embed_init_d = utils.read_emd(
         filename=config.pretrain_emd_filename_d,
         n_node=config.n_node,
         n_embed=config.n_embed)
     self.node_embed_init_g = utils.read_emd(
         filename=config.pretrain_emd_filename_g,
         n_node=config.n_node,
         n_embed=config.n_embed)
     print("finish reading initial embeddings")
     # use the BFS to construct the trees
     print("Constructing Trees")
     if config.app == "recommendation":
         self.mul_construct_trees_for_recommend(self.user_nodes)
     else:  # classification
         self.mul_construct_trees(self.root_nodes)
     print("after storing the data")
     config.max_degree = utils.get_max_degree(self.linked_nodes)
     print("config.max")
     self.build_gan()
     print("build gan")
     self.config = tf.ConfigProto()  #device_count = {"GPU": 0, "CPU":1})
     print("config self")
     self.config.gpu_options.allow_growth = True
     print("config gpu")
     #tf.group An operation that executes all its input
     self.init_op = tf.group(tf.global_variables_initializer(),
                             tf.local_variables_initializer())
     print("init op")
     self.sess = tf.Session(config=self.config)
     #self.sess = tf.Session()
     print("before run")
     self.sess.run(self.init_op)
Exemple #7
0
def eval_test(config):
    """do the evaluation when training

    :return:
    """
    results = []
    if config.app == "link_prediction":
        LPE = elp.LinkPredictEval(config.emb_filename, config.test_filename,
                                  config.test_neg_filename, config.n_node,
                                  config.n_embed)
        result = LPE.eval_link_prediction()
        results.append(config.model + ":" + str(result) + "\n")

    with open(config.result_filename, mode="a+") as f:
        f.writelines(results)

    test_edges = utils.read_edges_from_file(config.test_filename)
    test_edges_neg = utils.read_edges_from_file(config.test_neg_filename)
    test_edges.extend(test_edges_neg)
    emd = utils.read_emd(config.emb_filename,
                         n_node=config.n_node,
                         n_embed=config.n_embed)
    score_res = []
    for i in range(len(test_edges)):
        score_res.append(np.dot(emd[test_edges[i][0]], emd[test_edges[i][1]]))
    test_label = np.array(score_res)
    bar = np.median(test_label)
    ind_pos = test_label >= bar
    ind_neg = test_label < bar
    test_label[ind_pos] = 1
    test_label[ind_neg] = 0
    true_label = np.zeros(test_label.shape)
    true_label[0:len(true_label) // 2] = 1
    f1 = f1_score(true_label, test_label, average='macro')
    result = config.model + ":" + str(f1) + "\n"
    print(result)
    with open(config.result_filename_f1, mode="a+") as f:
        f.writelines(result)
Exemple #8
0
embed_filename = "/home/raavan/mountedDrives/home/ayush/twitterGanFEAT/pre_train/citeseerNew/citeseerNew40_gen_median_6510.emb"
#---------------------------------------------------------------------------------------------------------
test_pos_filename = "/home/raavan/mountedDrives/home/ayush/twitterGanFEAT/data/citeseerNew/test60/testciteseer60.txt"
test_neg_filename = "/home/raavan/mountedDrives/home/ayush/twitterGanFEAT/data/citeseerNew/test60/testNegciteseer_60_5x.txt"
n_node = 3312
n_embed = 32
emdFeaturesFilename = "/home/raavan/mountedDrives/home/ayush/twitterGanFEAT/features/citeseerNew/features.txt"
nFeatures = 3703
#for generator---------------change here------------------------------------------------------------------
#FeatureWtFilename="/home/raavan/mountedDrives/home/ayush/twitterGanFEAT/results/gplus/v_6_/disFeatWeights2.txt"
FeatureWtFilename = "/home/raavan/mountedDrives/home/ayush/twitterGanFEAT/pre_train/citeseerNew/citeseerNew20_Weights_gen_median_6510.emb"

#---------------------------------------------------------------------------------------------------------
#read
emd = utils.read_emd(embed_filename, n_node=n_node, n_embed=n_embed)
emdFeatures = utils.read_emd(emdFeaturesFilename,
                             n_node=n_node,
                             n_embed=nFeatures)
featureWt = utils.readFeatureWt(FeatureWtFilename)
test_edges_pos = utils.read_edges_from_file(test_pos_filename)
test_edges_neg = utils.read_edges_from_file(test_neg_filename)

#for generator---------------change here------------------------------------------------------------------
featureWt = np.multiply(featureWt, featureWt)
#---------------------------------------------------------------------------------------------------------
score_res_pos = []
score_res_neg = []
#score_res= []
wProduct = []
for i in range(len(test_edges_pos)):