Ejemplo n.º 1
0
def call_plot_hyp(data_set, params):
    # Load range of hyper parameters tested on to plot
    try:
        model_hyp_range = json.load(
            open('experiments/config/%s_hypRange.conf' % data_set, 'r')
        )
    except IOError:
        model_hyp_range = json.load(
            open('experiments/config/default_hypRange.conf', 'r')
        )
    for meth in params["methods"]:
            meth_hyp_range = model_hyp_range[meth]
            exp_param = params["experiments"]
            plot_util.plot_hyp(meth_hyp_range.keys(), exp_param,
                               meth, data_set,
                               s_sch=params["samp_scheme"])
Ejemplo n.º 2
0
def call_plot_hyp(data_set, params):
    """Function to plot the result of hyperparameter search
           
           Attributes:
               data_set (str): Name of the dataset to be used for the experiment
               params (dict): Dictionary of parameters necessary for running the experiment

    """
    # Load range of hyper parameters tested on to plot
    try:
        model_hyp_range = json.load(
            open('experiments/config/%s_hypRange.conf' % data_set, 'r'))
    except IOError:
        model_hyp_range = json.load(
            open('experiments/config/default_hypRange.conf', 'r'))
    for meth in params["methods"]:
        meth_hyp_range = model_hyp_range[meth]
        exp_param = params["experiments"]
        plot_util.plot_hyp(meth_hyp_range.keys(),
                           exp_param,
                           meth,
                           data_set,
                           s_sch=params["samp_scheme"])
Ejemplo n.º 3
0
def choose_best_hyp(data_set, graphs, params):
    # Load range of hyper parameters to test on
    try:
        model_hyp_range = json.load(
            open('experiments/config/%s_hypRange2.conf' % data_set, 'r')
        )
    except IOError:
        model_hyp_range = json.load(
            open('experiments/config/default_hypRange.conf', 'r')
        )

    # Test each hyperparameter for each method and store the best
    for meth in params["methods"]:
        dim = 128
        MethClass = getattr(
            importlib.import_module("embedding.%s" % meth),
            methClassMap[meth]
        )
        meth_hyp_range = model_hyp_range[meth]
        gr_max, lp_max = 0, 0
        gr_hyp, lp_hyp = 0, 0
        gr_hyp, lp_hyp = {meth: {}}, {meth: {}}

        # Test each hyperparameter
        ev_cols = ["GR MAP", "LP MAP"]
        hyp_df = pd.DataFrame(
            columns=list(meth_hyp_range.keys()) + ev_cols + ["Round Id"]
        )
        hyp_r_idx = 0
        print()
        #print(meth_hyp_range.values())
        print()
        for hyp in list(itertools.product(*meth_hyp_range.values())):
            #hyp_d = {"d": dim}
            hyp_d = {}
            hyp_d.update(dict(zip(meth_hyp_range.keys(), hyp)))
            print(hyp_d)
            if meth == "dynAE" or meth == "dynRNN" or meth == "dynAERNN":
                hyp_d.update({
                    "modelfile": [
                        "./intermediate/encoder_model_%s_%d.json" % (data_set, dim),
                        "./intermediate/decoder_model_%s_%d.json" % (data_set, dim)
                    ],
                    "weightfile": [
                        "./intermediate/encoder_weights_%s_%d.hdf5" % (data_set, dim),
                        "./intermediate/decoder_weights_%s_%d.hdf5" % (data_set, dim)
                    ]
                })
            # elif meth == "gf" or meth == "node2vec":
            #     hyp_d.update({"data_set": data_set})
            MethObj = MethClass(dim, hyp_d)
            gr, lp = run_exps(MethObj, meth, dim, graphs,
                                  data_set, params)
            gr_m, lp_m = np.mean(gr), np.mean(lp)
            gr_max, gr_hyp[meth] = get_max(gr_m, gr_max, hyp_d, gr_hyp[meth])
            lp_max, lp_hyp[meth] = get_max(lp_m, lp_max, hyp_d, lp_hyp[meth])
            hyp_df_row = dict(zip(meth_hyp_range.keys(), hyp))
            for r_id in range(params["rounds"]):
                hyp_df.loc[hyp_r_idx, meth_hyp_range.keys()] = \
                    pd.Series(hyp_df_row)
                # hyp_df.loc[hyp_r_idx, ev_cols + ["Round Id"]] = \
                #     [gr[min(r_id, len(gr) -1)], lp[r_id], r_id]
                hyp_df.loc[hyp_r_idx, ev_cols + ["Round Id"]] = \
                    [np.mean(np.array(gr)), np.mean(np.array(lp)), r_id]
                hyp_r_idx += 1
        exp_param = params["experiments"]
        for exp in exp_param:
            hyp_df.to_hdf(
                "intermediate/%s_%s_%s_%s_hyp.h5" % (data_set, meth,
                                                         exp,
                                                         params["samp_scheme"]),
                "df"
            )
        plot_util.plot_hyp(meth_hyp_range.keys(), exp_param,
                           meth, data_set, s_sch=params["samp_scheme"])

        # Store the best hyperparameter
        opt_hyp_f_pre = 'experiments/config/%s_%s_%s' % (
            data_set,
            meth,
            params["samp_scheme"]
        )
        if gr_max:
            with open('%s_gr.conf' % opt_hyp_f_pre, 'w') as f:
                f.write(json.dumps(gr_hyp, indent=4))
        if lp_max:
            with open('%s_lp.conf' % opt_hyp_f_pre, 'w') as f:
                f.write(json.dumps(lp_hyp, indent=4))