Example #1
0
        else:
            node_num = self._node_num
        adj_mtx_r = np.zeros((node_num, node_num))
        for v_i in range(node_num):
            for v_j in range(node_num):
                if v_i == v_j:
                    continue
                adj_mtx_r[v_i, v_j] = self.get_edge_weight(v_i, v_j)
        return adj_mtx_r


if __name__ == '__main__':
    # load Zachary's Karate graph
    edge_f = 'data/karate.edgelist'
    G = graph_util.loadGraphFromEdgeListTxt(edge_f, directed=False)
    G = G.to_directed()
    res_pre = 'results/testKarate'
    graph_util.print_graph_stats(G)
    t1 = time()
    embedding = HOPE(4, 0.01)
    embedding.learn_embedding(graph=G,
                              edge_f=None,
                              is_weighted=True,
                              no_python=True)
    print('HOPE:\n\tTraining time: %f' % (time() - t1))

    viz.plot_embedding2D(embedding.get_embedding()[:, :2],
                         di_graph=G,
                         node_colors=None)
    plt.show()
Example #2
0
def call_exps(params, data_set):
    # Load Dataset
    print('Dataset: %s' % data_set)

    ########  for SBM, r_mat, hyperbolic
    #if data_set[10:13] == 'r_m' or data_set[10:13] == 'sto' or data_set[10:13] == 'hyp':
    #   di_graph = nx.read_gpickle('gem/data/%s/graph.gpickle' % data_set)[0]
    #else:

    #di_graph = nx.read_gpickle('gem/data/%s/graph.gpickle' % data_set)[0]
    di_graph = nx.read_gpickle('gem/data/%s/graph.gpickle' % data_set)

    di_graph, nodeListMap = graph_util.get_lcc(di_graph)
    try:
        os.makedirs('gem/nodeListMap')
    except:
        pass
    pickle.dump(nodeListMap, open('gem/nodeListMap/%s.pickle' % data_set,
                                  'wb'))
    graph_util.print_graph_stats(di_graph)

    # Load node labels if given
    if bool(params["node_labels"]):
        node_labels = pickle.load(
            open('gem/data/%s/node_labels.pickle' % data_set, 'rb'))
        node_labels_gc = np.zeros(
            (di_graph.number_of_nodes(), node_labels.shape[1]))
        for k, v in nodeListMap.items():
            try:
                node_labels_gc[v, :] = node_labels[k, :].toarray()
            # Already a numpy array
            except AttributeError:
                node_labels_gc[v, :] = node_labels[k, :]
        node_labels = node_labels_gc
    else:
        node_labels = None

    # Search through the hyperparameter space
    if params["find_hyp"]:
        choose_best_hyp(data_set, di_graph, node_labels, params)

    # Load best hyperparameter and test it again on new test data
    for d, meth, exp in itertools.product(params["dimensions"],
                                          params["methods"],
                                          params["experiments"]):
        dim = int(d)
        MethClass = getattr(importlib.import_module("gem.embedding.%s" % meth),
                            methClassMap[meth])
        opt_hyp_f_pre = 'gem/experiments/config/synthetic/%s_%s_%s' % (
            data_set, meth, params["samp_scheme"])
        try:
            if exp != "viz":
                if exp == 'lpt':
                    model_hyp = json.load(
                        open('%s_lp.conf' % opt_hyp_f_pre, 'r'))
                else:
                    model_hyp = json.load(
                        open('%s_%s.conf' % (opt_hyp_f_pre, exp), 'r'))
            else:
                model_hyp = json.load(
                    open('%s_%s.conf' % (opt_hyp_f_pre, params["viz_params"]),
                         'r'))
        except IOError:
            print('Default hyperparameter of the method chosen')
            model_hyp = json.load(
                open('gem/experiments/config/%s.conf' % meth, 'r'))
        hyp = {}
        hyp.update(model_hyp[meth])
        hyp.update({"d": dim})
        if meth == "sdne":
            hyp.update({
                "modelfile": [
                    "gem/intermediate/en_mdl_%s_%d.json" % (data_set, dim),
                    "gem/intermediate/dec_mdl_%s_%d.json" % (data_set, dim)
                ],
                "weightfile": [
                    "gem/intermediate/enc_wts_%s_%d.hdf5" % (data_set, dim),
                    "gem/intermediate/dec_wts_%s_%d.hdf5" % (data_set, dim)
                ]
            })
        elif meth == "gf" or meth == "node2vec":
            hyp.update({"data_set": data_set})
        MethObj = MethClass(hyp)
        run_exps(MethObj, meth, dim, di_graph, data_set, node_labels, params)