Esempio n. 1
0
async def delete_gen(request, gen):
    if request.args.get('really', 'no') == 'yes':
        gen_trials = MongoTrials('mongo://localhost:1234/covid/jobs',
                                 f'covid-{gen}')
        gen_trials.refresh()
        gen_trials.delete_all()
        return redirect(f"/status/?refresh=true")
    return html(create_delete_prompt(f"GENERATION 'covid-{gen}'"))
Esempio n. 2
0
def _refresh_trials():
    global TRIALS, TRIALS_REFRESHED

    if TRIALS is None:
        TRIALS = MongoTrials('mongo://localhost:1234/covid/jobs')

    TRIALS_REFRESHED = datetime.now()
    TRIALS.refresh()
Esempio n. 3
0
def run_optimization(level=1):
    print(f"Optimizing at level {level}")

    set_random_seeds(4)

    next_lvl_trials = MongoTrials('mongo://localhost:1234/covid/jobs',
                                  exp_key=f'covid-{level+1}')
    if len(next_lvl_trials.trials) > 0:
        print(f"Already completed level {level} -- skipping")
        return

    exp_key = f'covid-{level}'

    trials = MongoTrials('mongo://localhost:1234/covid/jobs', exp_key=exp_key)

    suggestion_box = hyperopt.tpe.suggest

    if level == 1:
        max_evals = LEVEL_DEFS[0][1]
        depth = 1

    elif level > 1:
        depth, new_budget, extend_budget = LEVEL_DEFS[level - 1]
        last_depth, _, _ = LEVEL_DEFS[level - 2]

        # Minimum one per node for the expensive ones -- no point wasting compute time
        num_new = int(np.ceil((new_budget / depth) / NUM_NODES) * NUM_NODES)

        if len(trials.trials) == 0:
            print("Generating estimates from previous level")
            result_docs = configure_next_level(level, depth, extend_budget)
            num_to_extend = len(result_docs)

            suggestion_box = create_suggestion_box(result_docs)

        last_level_trials = MongoTrials('mongo://localhost:1234/covid/jobs',
                                        exp_key=f'covid-{level-1}')
        prev_level_count = len(
            [x for x in last_level_trials.losses() if x is not None])

        max_evals = prev_level_count + num_new
        trials.refresh()

    objective = functools.partial(test_parameterization, num_epochs=depth)

    if len([x for x in trials.statuses() if x == 'ok']) >= max_evals:
        print(f"Already completed level {level} -- skipping")
    else:
        best = hyperopt.fmin(objective,
                             space=SEARCH_SPACE,
                             algo=suggestion_box,
                             max_evals=max_evals,
                             trials=trials)

        print(best)
Esempio n. 4
0
def validate_from_tids(dbname):
    trials = MongoTrials('mongo://localhost:44556/%s/jobs' % dbname,
                         refresh=False)
    trials.refresh()
    tdict = dict([(t['tid'], t) for t in trials])
    print "TIDS", tdict.keys()

    for tid, t in tdict.items():
        assert t['misc']['tid'] == tid
        if 'from_tid' in t['misc']:
            if t['misc']['from_tid'] not in tdict:
                print 'WTF gave us', tid, t['misc']['from_tid']
Esempio n. 5
0
def main():
    #logging.basicConfig(level=logging.INFO)

    data_root = abspath2("../reference-data")
    input_root = abspath2("../bayesian-input")
    output_root = abspath2("../bayesian-output")

    os.makedirs(input_root, exist_ok=True)
    os.makedirs(output_root, exist_ok=True)

    seed = 123

    np.random.seed(seed)

    # Global variable
    global ITERATION

    ITERATION = 0
    MAX_EVALS = 2
    from hyperopt import fmin
    from hyperopt import tpe

    portion = 0.5

    budgetfile = "budget.csv"
    if not os.path.exists('/home/ubuntu/BeamCompetitions/budget.csv'):
        data = pd.DataFrame(columns=['iter'])
        print(data)
        data.to_csv('/home/ubuntu/BeamCompetitions/budget.csv')

    #initialize the 1st mongotrail
    bayes_trials = MongoTrials('mongo://localhost:27017/wh_db/jobs',
                               exp_key=str(datetime.datetime.now()))
    len_trial = 21
    while len_trial >= 20:
        #bayes_trials = MongoTrials('mongo://localhost:27017/wh_db/jobs', exp_key=str(datetime.datetime.now()))
        #print("After getting trials, I can print")

        best = fmin(fn=objective,
                    space=space,
                    algo=tpe.suggest,
                    max_evals=MAX_EVALS,
                    trials=bayes_trials,
                    rstate=np.random.RandomState(50))

        budget = pd.read_csv("/home/ubuntu/BeamCompetitions/budget.csv")
        if (len(budget) == 0): n_sim_iters = 1
        else:
            n_sim_iters = budget.loc[len(budget) - 1, 'iter'] + budget_update
            n_sim_iters = int(n_sim_iters)

        #budget.loc[len(budget),'iter']=n_sim_iters
        budget = budget.append({'iter': n_sim_iters}, ignore_index=True)
        budget.to_csv("/home/ubuntu/BeamCompetitions/budget.csv", index=False)

        sorted_trial = sorted(list(bayes_trials.trials),
                              key=lambda x: x['result']['loss'])
        list_trial = list(sorted_trial)
        cutted_trial = list_trial[0:math.floor(len(list_trial) * portion)]
        MAX_EVALS = math.floor(len(list_trial) * portion)
        #suppose the cutted_trial is correct
        new_exp = str(datetime.datetime.now())
        test_trials = MongoTrials('mongo://localhost:27017/wh_db/jobs',
                                  exp_key=new_exp)
        for trial in cutted_trial:
            hyperopt_trial = hyperopt.Trials(new_exp).new_trial_docs(
                tids=[trial['tid']],
                specs=[trial['spec']],
                results=[trial['result']],
                miscs=[trial['misc']])
            hyperopt_trial[0]['state'] = hyperopt.JOB_STATE_DONE
            test_trials.insert_trial_docs(hyperopt_trial)
            test_trials.refresh()
        bayes_trials = test_trials
        len_trial = len(list(bayes_trials.trials))

    if not os.path.exists('../bayesian-output/bayes_trials.csv'):
        data = pd.DataFrame(
            columns=['loss', 'params', 'iteration', 'train_time'])
        data.to_csv('../bayesian-output/bayes_trials.csv')
    outputfile = pd.read_csv("../bayesian-output/bayes_trials.csv")
    for result in list(bayes_trials.results):
        #of_connection.write(",".join(result))
        outputfile = outputfile.append(
            {
                'loss': result['loss'],
                'params': list(result['params']),
                'iteration': result['iter'],
                'train_time': result['train_time']
            },
            ignore_index=True)

        # trials = self.convertResultsToTrials(hyperparameterSpace, filteredResults),
        # iter=iter+1
    outputfile.to_csv('../bayesian-output/bayes_trials.csv')