コード例 #1
0
def main():
    env = GaussianEnvironment(
        bo_param2model_param_dic=bo_param2model_param_dic,
        result_filename=result_filename,
        output_dir=output_dir,
        reload=reload)

    agent = GMRF_BO(bo_param_list,
                    env,
                    GAMMA=GAMMA,
                    GAMMA0=GAMMA0,
                    GAMMA_Y=GAMMA_Y,
                    ALPHA=ALPHA,
                    is_edge_normalized=IS_EDGE_NORMALIZED,
                    gt_available=True,
                    n_early_stopping=N_EARLY_STOPPING,
                    burnin=BURNIN,
                    normalize_output=NORMALIZE_OUTPUT,
                    update_hyperparam_func=UPDATE_HYPERPARAM_FUNC,
                    initial_k=INITIAL_K,
                    initial_theta=INITIAL_THETA,
                    acquisition_func=ACQUISITION_FUNC,
                    acquisition_param_dic=ACQUISITION_PARAM_DIC)

    # for i in tqdm(range(n_iter)):
    for i in range(n_iter):
        try:
            flg = agent.learn()
            agent.plot(output_dir=output_dir)
            # agent.save_mu_sigma_csv()

            if flg == False:
                print("Early Stopping!!!")
                print(agent.bestX)
                print(agent.bestT)
                break

        except KeyboardInterrupt:
            print("Learnig process was forced to stop!")
            break

    #plot_loss(agent.point_info_manager.T_seq, 'reward.png')
    subprocess.call([
        "./convert_pngs2gif.sh demo_%s_eps_%f.gif" %
        (ACQUISITION_FUNC, ACQUISITION_PARAM_DIC["eps"])
    ],
                    shell=True)
    os.system("mv ./output/*.gif ./")
コード例 #2
0
def main():
    # env = SinEnvironment(bo_param2model_param_dic=bo_param2model_param_dic, result_filename=result_filename,
    #                      output_dir=output_dir,
    #                      reload=reload)
    env = OneDimGaussianEnvironment(bo_param2model_param_dic=bo_param2model_param_dic, result_filename=result_filename,
                                    output_dir=output_dir,
                                    reload=reload)

    agent = GMRF_BO(bo_param_list, env, GAMMA=GAMMA, GAMMA0=GAMMA0, GAMMA_Y=GAMMA_Y, ALPHA=ALPHA,
                    is_edge_normalized=IS_EDGE_NORMALIZED, gt_available=True, n_early_stopping=N_EARLY_STOPPING,
                    burnin=BURNIN,
                    normalize_output=NORMALIZE_OUTPUT, update_hyperparam_func=UPDATE_HYPERPARAM_FUNC,
                    initial_k=INITIAL_K, initial_theta=INITIAL_THETA, acquisition_func=ACQUISITION_FUNC,
                    acquisition_param_dic=ACQUISITION_PARAM_DIC)
    #
    # agent = GP_BO(bo_param_list, env, gt_available=True, my_kernel=kernel, burnin=BURNIN,
    #               normalize_output=NORMALIZE_OUTPUT, acquisition_func=ACQUISITION_FUNC,
    #               acquisition_param_dic=ACQUISITION_PARAM_DIC)

    # for i in tqdm(range(n_iter)):
    for i in range(n_iter):
        try:
            flg = agent.learn()
            agent.plot(output_dir=output_dir)
            agent.save_mu_sigma_csv()

            if flg == False:
                print("Early Stopping!!!")
                print(agent.bestX)
                print(agent.bestT)
                break

        except KeyboardInterrupt:
            print("Learnig process was forced to stop!")
            break

    plot_1dim(agent.point_info_manager.T_seq, 'reward.png')
コード例 #3
0
def singleTest(ACQUISITION_FUNC, trialCount):
    print("%s: trial %d"%(ACQUISITION_FUNC, trialCount))
    OUTPUT_DIR = os.path.join(os.getcwd(), 'output_%s_clicks'%ACQUISITION_FUNC)
    if os.path.exists(OUTPUT_DIR):
        shutil.rmtree(OUTPUT_DIR)
    RESULT_FILENAME = os.path.join(OUTPUT_DIR, "gaussian_result_2dim_clicks_%s_trialCount_%d.csv"%(ACQUISITION_FUNC, trialCount))

    mu_sigma_csv_path = './mu2ratio_%s/mu_sigma.csv'%ACQUISITION_FUNC
    ratio_csv_out_path = './mu2ratio_%s/ratios.csv'%ACQUISITION_FUNC
    N_TOTAL_EXP = 100000

    print('GAMMA: ', GAMMA)
    print('GAMMA_Y: ', GAMMA_Y)
    print('GAMMA0:', GAMMA0)

    MU2RATIO_DIR = './mu2ratio_%s'%ACQUISITION_FUNC
    mkdir_if_not_exist(OUTPUT_DIR)
    mkdir_if_not_exist(MU2RATIO_DIR)
    mkdir_if_not_exist("./eval")

    param_names = sorted([x.replace('.csv', '') for x in os.listdir(parameter_dir)])


    bo_param2model_param_dic = {}
    
    bo_param_list = []
    for param_name in param_names:
        param_df = pd.read_csv(os.path.join(parameter_dir, param_name + '.csv'), dtype=str)
        bo_param_list.append(param_df[param_name].values)
    
        param_df.set_index(param_name, inplace=True)
    
        bo_param2model_param_dic[param_name] = param_df.to_dict()['bo_' + param_name]
    
    env = ClickTwoDimGaussianEnvironment(bo_param2model_param_dic=bo_param2model_param_dic, 
                                         result_filename=RESULT_FILENAME,
                                         output_dir=OUTPUT_DIR,
                                         reload=reload)
    
    agent = GMRF_BO(bo_param_list, env, GAMMA=GAMMA, GAMMA0=GAMMA0, GAMMA_Y=GAMMA_Y, ALPHA=ALPHA,
                    is_edge_normalized=IS_EDGE_NORMALIZED, gt_available=True, n_early_stopping=N_EARLY_STOPPING,
                    burnin=BURNIN,
                    normalize_output=NORMALIZE_OUTPUT, update_hyperparam_func=UPDATE_HYPERPARAM_FUNC,
                    initial_k=INITIAL_K, initial_theta=INITIAL_THETA, acquisition_func=ACQUISITION_FUNC,
                    acquisition_param_dic=ACQUISITION_PARAM_DIC, n_ctr=N_TOTAL_EXP)


    #agent.plot_click_distribution(output_dir)
    agent.save_mu_sigma_csv(outfn=mu_sigma_csv_path)

# agent.learn_from_clicks()
    nIter = 100
    for i in range(nIter):
        try:
            flg = agent.learn_from_clicks(mu2ratio_dir=MU2RATIO_DIR, 
                                          mu_sigma_csv_path=mu_sigma_csv_path, 
                                          ratio_csv_out_path=ratio_csv_out_path)
            # agent.sample_randomly()
            #agent.plot_click_distribution(output_dir)
            #break
            if flg == False:
                print("Early Stopping!!!")
                print(agent.bestX)
                print(agent.bestT)
                break
        except KeyboardInterrupt:
            print("Learnig process was forced to stop!")
            # print(agent.X)
            # print(agent.Treal)
            exit(0)
    os.system("mv %s/*.csv ./eval/"%OUTPUT_DIR)
コード例 #4
0
bo_param_list = []
for param_name in param_names:
    param_df = pd.read_csv(os.path.join(parameter_dir, param_name + '.csv'), dtype=str)
    bo_param_list.append(param_df[param_name].values)

    param_df.set_index(param_name, inplace=True)

    bo_param2model_param_dic[param_name] = param_df.to_dict()['bo_' + param_name]

env = ClickOneDimGaussianEnvironment(bo_param2model_param_dic=bo_param2model_param_dic, result_filename=result_filename,
                                     output_dir=output_dir,
                                     reload=reload)

agent = GMRF_BO(bo_param_list, env, GAMMA=GAMMA, GAMMA0=GAMMA0, GAMMA_Y=GAMMA_Y, ALPHA=ALPHA,
                is_edge_normalized=IS_EDGE_NORMALIZED, gt_available=True, n_early_stopping=N_EARLY_STOPPING,
                burnin=BURNIN,
                normalize_output=NORMALIZE_OUTPUT, update_hyperparam_func=UPDATE_HYPERPARAM_FUNC,
                initial_k=INITIAL_K, initial_theta=INITIAL_THETA, acquisition_func=ACQUISITION_FUNC,
                acquisition_param_dic=ACQUISITION_PARAM_DIC, n_ctr=n_total_exp)

# for i in tqdm(range(n_iter)):

agent.save_mu_sigma_csv(outfn=mu_sigma_fn, point_info_fn=point_fn)
agent.plot_click_distribution(output_dir)
# agent.learn_from_clicks()

for i in range(n_iter):
    try:
        flg = agent.learn_from_clicks()
        # agent.sample_randomly()
        agent.plot_click_distribution(output_dir)
コード例 #5
0
ファイル: test40.py プロジェクト: vivi489/gp_ucb_python_lib
def singleTest(ACQUISITION_FUNC, trialCount):
    print("%s: trial %d" % (ACQUISITION_FUNC, trialCount))
    OUTPUT_DIR = os.path.join(os.getcwd(), 'output_%s' % ACQUISITION_FUNC)

    ########################

    ### temporary ###
    if os.path.exists(OUTPUT_DIR):
        shutil.rmtree(OUTPUT_DIR)
    ##################
    RESULT_FILENAME = os.path.join(
        OUTPUT_DIR, "gaussian_result_4dim_%s_trialCount_%d.csv" %
        (ACQUISITION_FUNC, trialCount))

    print('GAMMA: ', GAMMA)
    print('GAMMA_Y: ', GAMMA_Y)
    print('GAMMA0:', GAMMA0)

    mkdir_if_not_exist(OUTPUT_DIR)

    param_names = sorted(
        [x.replace('.csv', '') for x in os.listdir(PARAMETER_DIR)])

    bo_param2model_param_dic = {}

    bo_param_list = []

    for param_name in param_names:
        param_df = pd.read_csv(os.path.join(PARAMETER_DIR,
                                            param_name + '.csv'),
                               dtype=str)
        bo_param_list.append(param_df[param_name].values)

        param_df.set_index(param_name, inplace=True)

        bo_param2model_param_dic[param_name] = param_df.to_dict()['gp_' +
                                                                  param_name]

    env = FourDimGaussianEnvironment(
        bo_param2model_param_dic=bo_param2model_param_dic,
        result_filename=RESULT_FILENAME,
        output_dir=OUTPUT_DIR,
        reload=False)

    agent = GMRF_BO(bo_param_list,
                    env,
                    GAMMA=GAMMA,
                    GAMMA0=GAMMA0,
                    GAMMA_Y=GAMMA_Y,
                    ALPHA=ALPHA,
                    is_edge_normalized=IS_EDGE_NORMALIZED,
                    gt_available=True,
                    n_early_stopping=N_EARLY_STOPPING,
                    burnin=BURNIN,
                    normalize_output=NORMALIZE_OUTPUT,
                    update_hyperparam_func=UPDATE_HYPERPARAM_FUNC,
                    initial_k=INITIAL_K,
                    initial_theta=INITIAL_THETA,
                    acquisition_func=ACQUISITION_FUNC,
                    acquisition_param_dic=ACQUISITION_PARAM_DIC)

    nIter = 500
    for i in range(nIter):
        flg = agent.learn(drop=True if i < nIter - 1 else False)
        if not flg:
            print("Early Stopping!!!")
            print(agent.bestX)
            print(agent.bestT)
            break
    os.system("mv %s/*.csv ./eval/" % OUTPUT_DIR)
コード例 #6
0
def singleTest(ACQUISITION_FUNC, trialCount):
    print("%s: trial %d" % (ACQUISITION_FUNC, trialCount))
    OUTPUT_DIR = os.path.join(os.getcwd(), 'output_%s' % ACQUISITION_FUNC)
    # ### temporary ###
    if os.path.exists(OUTPUT_DIR):
        shutil.rmtree(OUTPUT_DIR)
    ##################
    RESULT_FILENAME = os.path.join(
        OUTPUT_DIR, "gaussian_result_1dim_%s_trialCount_%d.csv" %
        (ACQUISITION_FUNC, trialCount))
    np.random.seed(int(time.time()))
    print('GAMMA: ', GAMMA)
    print('GAMMA_Y: ', GAMMA_Y)
    print('GAMMA0:', GAMMA0)

    mkdir_if_not_exist(OUTPUT_DIR)
    param_names = sorted(
        [x.replace('.csv', '') for x in os.listdir(PARAMETER_DIR)])

    bo_param2model_param_dic = {}
    bo_param_list = []
    for param_name in param_names:  # param_name is a param file's name
        param_df = pd.read_csv(
            os.path.join(PARAMETER_DIR, param_name + '.csv'),
            dtype=str)  #makes index column type str instead of float

        # always read the column of the same name as the file name -- param_name
        bo_param_list.append(param_df[param_name].values)
        # param_df has a column of its csv file name, e.g. "x"
        # and this column is set as the index column
        param_df.set_index(param_name, inplace=True)
        # dict: param_file name -> column dict (the column with the name "bo_"+param_file name)
        # column dict: index column element -> cell value #index column is type str
        bo_param2model_param_dic[param_name] = param_df.to_dict()['bo_' +
                                                                  param_name]

        # bo_param_list is a list of every "bo_" column in all the param files of param_names
        # print("bo_param_list", bo_param_list)


#    env = SinEnvironment(bo_param2model_param_dic=bo_param2model_param_dic,
#                         result_filename=RESULT_FILENAME,
#                         output_dir=OUTPUT_DIR,
#                         reload=RELOAD)

    env = OneDimGaussianEnvironment(
        bo_param2model_param_dic=bo_param2model_param_dic,
        result_filename=RESULT_FILENAME,
        output_dir=OUTPUT_DIR,
        reload=RELOAD)

    agent = GMRF_BO(bo_param_list,
                    env,
                    GAMMA=GAMMA,
                    GAMMA0=GAMMA0,
                    GAMMA_Y=GAMMA_Y,
                    ALPHA=ALPHA,
                    is_edge_normalized=IS_EDGE_NORMALIZED,
                    gt_available=True,
                    n_early_stopping=N_EARLY_STOPPING,
                    burnin=BURNIN,
                    normalize_output=NORMALIZE_OUTPUT,
                    update_hyperparam_func=UPDATE_HYPERPARAM_FUNC,
                    initial_k=INITIAL_K,
                    initial_theta=INITIAL_THETA,
                    acquisition_func=ACQUISITION_FUNC,
                    acquisition_param_dic=ACQUISITION_PARAM_DIC)

    #    agent = GP_BO(bo_param_list, env,
    #                   gt_available=True,
    #                   my_kernel=kernel,
    #                   burnin=BURNIN,
    #                   normalize_output=NORMALIZE_OUTPUT,
    #                   acquisition_func=ACQUISITION_FUNC,
    #                   acquisition_param_dic=ACQUISITION_PARAM_DIC)

    nIter = 1000
    for i in range(nIter):
        flg = agent.learn()
        #agent.plot(output_dir=OUTPUT_DIR) #plotting causes deadlock among processes
        #agent.save_mu_sigma_csv() #this line causes deadlock among processes (I/O contention)
        if flg == False:
            print("Early Stopping!!!")
            print("bestX =", agent.bestX)
            print("bestT =", agent.bestT)
            break
    #plot_1dim(agent.point_info_manager.T_seq, 'reward.png')
    #subprocess.call(["./convert_pngs2gif.sh ./output/res*.png demo_%s_iterCount_%d.gif"%(ACQUISITION_FUNC, iterCount)])
    #os.system("mv %s/*.gif ./eval/"%OUTPUT_DIR)
    os.system("mv %s/*.csv ./eval/" % OUTPUT_DIR)