本文整理汇总了Python中hyperopt.Trials.losses方法的典型用法代码示例。如果您正苦于以下问题:Python Trials.losses方法的具体用法?Python Trials.losses怎么用?Python Trials.losses使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类hyperopt.Trials
的用法示例。
在下文中一共展示了Trials.losses方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: run_all_dl
# 需要导入模块: from hyperopt import Trials [as 别名]
# 或者: from hyperopt.Trials import losses [as 别名]
def run_all_dl(csvfile = saving_fp,
space = [hp.quniform('h1', 100, 550, 1),
hp.quniform('h2', 100, 550, 1),
hp.quniform('h3', 100, 550, 1),
#hp.choice('activation', ["RectifierWithDropout", "TanhWithDropout"]),
hp.uniform('hdr1', 0.001, 0.3),
hp.uniform('hdr2', 0.001, 0.3),
hp.uniform('hdr3', 0.001, 0.3),
hp.uniform('rho', 0.9, 0.999),
hp.uniform('epsilon', 1e-10, 1e-4)]):
# maxout works well with dropout (Goodfellow et al 2013), and rectifier has worked well with image recognition (LeCun et al 1998)
start_save(csvfile = csvfile)
trials = Trials()
print "Deep learning..."
best = fmin(objective,
space = space,
algo=tpe.suggest,
max_evals=evals,
trials=trials)
print best
print trials.losses()
with open('output/dlbest.pkl', 'w') as output:
pickle.dump(best, output, -1)
with open('output/dltrials.pkl', 'w') as output:
pickle.dump(trials, output, -1)
示例2: main
# 需要导入模块: from hyperopt import Trials [as 别名]
# 或者: from hyperopt.Trials import losses [as 别名]
def main():
usage = "%prog"
parser = OptionParser(usage=usage)
parser.add_option('-o', dest='output_dirname', default='bayes_opt_rnn_chars',
help='Output directory name')
parser.add_option('--reuse', dest='reuse', action="store_true", default=False,
help='Use reusable holdout; default=%default')
(options, args) = parser.parse_args()
global output_dirname, output_filename, reuse, search_alpha, space
reuse = options.reuse
output_dirname = options.output_dirname
if reuse:
output_dirname += '_reuse'
output_filename = fh.make_filename(defines.exp_dir, fh.get_basename_wo_ext(output_dirname), 'log')
with codecs.open(output_filename, 'w') as output_file:
output_file.write(output_dirname + '\n')
#output_file.write('reuse = ' + str(reuse) + '\n')
trials = Trials()
best = fmin(call_experiment,
space=space,
algo=tpe.suggest,
max_evals=100,
trials=trials)
print space_eval(space, best)
print trials.losses()
示例3: main
# 需要导入模块: from hyperopt import Trials [as 别名]
# 或者: from hyperopt.Trials import losses [as 别名]
def main():
usage = "%prog text.json labels.csv feature_dir output_dir"
parser = OptionParser(usage=usage)
parser.add_option('-m', dest='max_iter', default=4,
help='Maximum iterations of Bayesian optimization; default=%default')
(options, args) = parser.parse_args()
max_iter = int(options.max_iter)
global data_filename, label_filename, feature_dir, output_dir, log_filename
data_filename = args[0]
label_filename = args[1]
feature_dir = args[2]
output_dir = args[3]
if not os.path.exists(output_dir):
os.makedirs(output_dir)
log_filename = os.path.join(output_dir, 'log.txt')
with open(log_filename, 'w') as logfile:
logfile.write(','.join([data_filename, label_filename, feature_dir, output_dir]))
trials = Trials()
best = fmin(call_experiment,
space=space,
algo=tpe.suggest,
max_evals=max_iter,
trials=trials)
print space_eval(space, best)
print trials.losses()
示例4: work
# 需要导入模块: from hyperopt import Trials [as 别名]
# 或者: from hyperopt.Trials import losses [as 别名]
def work(self):
bandit = self.bandit
assert bandit.name is not None
algo = partial(
tree.suggest,
# XXX (begin)
n_trees=10,
logprior_strength=1.0,
# XXX (end)
)
LEN = self.LEN.get(bandit.name, 75)
trials = Trials()
fmin(fn=passthrough,
space=self.bandit.expr,
trials=trials,
algo=algo,
max_evals=LEN)
assert len(trials) == LEN
if 1:
rtrials = Trials()
fmin(fn=passthrough,
space=self.bandit.expr,
trials=rtrials,
algo=rand.suggest,
max_evals=LEN)
print 'RANDOM BEST 6:', list(sorted(rtrials.losses()))[:6]
if 0:
plt.subplot(2, 2, 1)
plt.scatter(range(LEN), trials.losses())
plt.title('TPE losses')
plt.subplot(2, 2, 2)
plt.scatter(range(LEN), ([s['x'] for s in trials.specs]))
plt.title('TPE x')
plt.subplot(2, 2, 3)
plt.title('RND losses')
plt.scatter(range(LEN), rtrials.losses())
plt.subplot(2, 2, 4)
plt.title('RND x')
plt.scatter(range(LEN), ([s['x'] for s in rtrials.specs]))
plt.show()
if 0:
plt.hist(
[t['x'] for t in self.experiment.trials],
bins=20)
#print trials.losses()
print 'OPT BEST 6:', list(sorted(trials.losses()))[:6]
#logx = np.log([s['x'] for s in trials.specs])
#print 'TPE MEAN', np.mean(logx)
#print 'TPE STD ', np.std(logx)
thresh = self.thresholds[bandit.name]
print 'Thresh', thresh
assert min(trials.losses()) < thresh
示例5: main
# 需要导入模块: from hyperopt import Trials [as 别名]
# 或者: from hyperopt.Trials import losses [as 别名]
def main():
set_globals()
trials = Trials()
best = fmin(call_experiment,
space=space,
algo=tpe.suggest,
max_evals=max_iter,
trials=trials)
print space_eval(space, best)
print "losses:", [-l for l in trials.losses()]
print('the best loss: ', max([-l for l in trials.losses()]))
print("number of trials: " + str(len(trials.trials)))
示例6: optimize
# 需要导入模块: from hyperopt import Trials [as 别名]
# 或者: from hyperopt.Trials import losses [as 别名]
def optimize(obj_function, inputs, key_file, space, max_eval):
trials = Trials()
f = partial(obj_function, inputs, key_file)
best = fmin(f, space=space, algo=tpe.suggest, max_evals=max_eval,
trials=trials)
LOGGER.info("{}\t{}".format(best, 1 - min(trials.losses())))
示例7: run
# 需要导入模块: from hyperopt import Trials [as 别名]
# 或者: from hyperopt.Trials import losses [as 别名]
def run(self):
start = time.time()
trials = Trials()
best = fmin(self._obj, self.model_param_space._build_space(), tpe.suggest, self.max_evals, trials)
best_params = space_eval(self.model_param_space._build_space(), best)
best_params = self.model_param_space._convert_int_param(best_params)
trial_rmses = np.asarray(trials.losses(), dtype=float)
best_ind = np.argmin(trial_rmses)
best_rmse_mean = trial_rmses[best_ind]
best_rmse_std = trials.trial_attachments(trials.trials[best_ind])["std"]
self.logger.info("-"*50)
self.logger.info("Best RMSE")
self.logger.info(" Mean: %.6f"%best_rmse_mean)
self.logger.info(" std: %.6f"%best_rmse_std)
self.logger.info("Best param")
self.task._print_param_dict(best_params)
end = time.time()
_sec = end - start
_min = int(_sec/60.)
self.logger.info("Time")
if _min > 0:
self.logger.info(" %d mins"%_min)
else:
self.logger.info(" %d secs"%_sec)
self.logger.info("-"*50)
示例8: optimize_model_pytorch
# 需要导入模块: from hyperopt import Trials [as 别名]
# 或者: from hyperopt.Trials import losses [as 别名]
def optimize_model_pytorch(device, args, train_GWAS, train_y, test_GWAS, test_y, out_folder ="", startupJobs = 40, maxevals = 200, noOut = False):
global numTrials_pytorch
numTrials_pytorch= 0
trials = Trials()
trial_wrapper = partial(trial_pytorch,device = device, args = args , train_GWAS = train_GWAS, train_y = train_y , test_GWAS = test_GWAS , test_y = test_y)
best_pars = fmin(trial_wrapper, parameter_space_pytorch(), algo=partial(tpe.suggest, n_startup_jobs=(startupJobs) ), max_evals=maxevals, trials=trials)
# Print the selected 'best' hyperparameters.
if noOut == False: print('\nBest hyperparameter settings: ',space_eval(parameter_space_pytorch(), best_pars),'\n')
# loops through the 1st entry in the dict that holds all the lookup keys
regression = True
for p in trials.trials[0]['misc']['idxs']: plot_optimization_pytorch(trials, p, regression, out_folder = out_folder)
best_pars = space_eval(parameter_space_pytorch(), best_pars) # this turns the indices into the actual params into the valid aprameter space
# override the epochs with the early start
lowestLossIndex = np.argmin(trials.losses())
trials.trial_attachments(trials.trials[lowestLossIndex])['highestAcc_epoch']
best_pars['earlyStopEpochs'] = trials.trial_attachments(trials.trials[lowestLossIndex])['highestAcc_epoch']
best_pars['earlyStopEpochs'] += 1 # as epochs are 0 based otherwise...
best_pars['epochs'] = best_pars['earlyStopEpochs']
if best_pars['epochs'] <= 0 : best_pars['epochs'] = 1 # we dont want a network without any training, as that will cause a problem for deep dreaming
return(best_pars)
示例9: work
# 需要导入模块: from hyperopt import Trials [as 别名]
# 或者: from hyperopt.Trials import losses [as 别名]
def work(self):
bandit = self.bandit
random_algo = Random(bandit)
# build an experiment of 10 trials
trials = Trials()
exp = Experiment(trials, random_algo)
#print random_algo.s_specs_idxs_vals
exp.run(10)
ids = trials.tids
assert len(ids) == 10
tpe_algo = TreeParzenEstimator(bandit)
#print pyll.as_apply(tpe_algo.post_idxs)
#print pyll.as_apply(tpe_algo.post_vals)
argmemo = {}
print trials.miscs
idxs, vals = miscs_to_idxs_vals(trials.miscs)
argmemo[tpe_algo.observed['idxs']] = idxs
argmemo[tpe_algo.observed['vals']] = vals
argmemo[tpe_algo.observed_loss['idxs']] = trials.tids
argmemo[tpe_algo.observed_loss['vals']] = trials.losses()
stuff = pyll.rec_eval([tpe_algo.post_below['idxs'],
tpe_algo.post_below['vals']],
memo=argmemo)
print stuff
示例10: notest_opt_qn_normal
# 需要导入模块: from hyperopt import Trials [as 别名]
# 或者: from hyperopt.Trials import losses [as 别名]
def notest_opt_qn_normal(f=hp_normal):
bandit = Bandit(
{'loss': scope.sum([f('v%i' % ii, 0, 1)
for ii in range(25)]) ** 2},
loss_target=0)
algo = TreeParzenEstimator(bandit,
prior_weight=.5,
n_startup_jobs=0,
n_EI_candidates=1,
gamma=0.15)
trials = Trials()
experiment = Experiment(trials, algo, async=False)
experiment.max_queue_len = 1
experiment.run(40)
print 'sorted losses:', list(sorted(trials.losses()))
idxs, vals = miscs_to_idxs_vals(trials.miscs)
if 1:
import hyperopt.plotting
hyperopt.plotting.main_plot_vars(trials, bandit, do_show=1)
else:
import matplotlib.pyplot as plt
begin = [v[:10] for k, v in vals.items()]
end = [v[-10:] for k, v in vals.items()]
plt.subplot(2, 1, 1)
plt.title('before')
plt.hist(np.asarray(begin).flatten())
plt.subplot(2, 1, 2)
plt.title('after')
plt.hist(np.asarray(end).flatten())
plt.show()
示例11: run_all_gbm
# 需要导入模块: from hyperopt import Trials [as 别名]
# 或者: from hyperopt.Trials import losses [as 别名]
def run_all_gbm(csvfile = saving_fp,
space = [hp.quniform('ntrees', 200, 750, 1), hp.quniform('max_depth', 5, 15, 1), hp.uniform('learn_rate', 0.03, 0.35)]):
# Search space is a stochastic argument-sampling program:
start_save(csvfile = csvfile)
trials = Trials()
best = fmin(objective,
space = space,
algo=tpe.suggest,
max_evals=evals,
trials=trials)
print best
# from hyperopt import space_eval
# print space_eval(space, best)
# trials.trials # list of dictionaries representing everything about the search
# trials.results # list of dictionaries returned by 'objective' during the search
print trials.losses() # list of losses (float for each 'ok' trial)
# trials.statuses() # list of status strings
with open('output/gbmbest.pkl', 'w') as output:
pickle.dump(best, output, -1)
with open('output/gbmtrials.pkl', 'w') as output:
pickle.dump(trials, output, -1)
示例12: work
# 需要导入模块: from hyperopt import Trials [as 别名]
# 或者: from hyperopt.Trials import losses [as 别名]
def work(self):
bandit = self.bandit
assert bandit.name is not None
algo = partial(anneal.suggest)
LEN = self.LEN.get(bandit.name, 50)
trials = Trials()
fmin(fn=passthrough, space=self.bandit.expr, trials=trials, algo=algo, max_evals=LEN)
assert len(trials) == LEN
if 1:
rtrials = Trials()
fmin(fn=passthrough, space=self.bandit.expr, trials=rtrials, algo=rand.suggest, max_evals=LEN)
print("RANDOM BEST 6:", list(sorted(rtrials.losses()))[:6])
if 0:
plt.subplot(2, 2, 1)
plt.scatter(list(range(LEN)), trials.losses())
plt.title("TPE losses")
plt.subplot(2, 2, 2)
plt.scatter(list(range(LEN)), ([s["x"] for s in trials.specs]))
plt.title("TPE x")
plt.subplot(2, 2, 3)
plt.title("RND losses")
plt.scatter(list(range(LEN)), rtrials.losses())
plt.subplot(2, 2, 4)
plt.title("RND x")
plt.scatter(list(range(LEN)), ([s["x"] for s in rtrials.specs]))
plt.show()
if 0:
plt.hist([t["x"] for t in self.experiment.trials], bins=20)
# print trials.losses()
print("ANNEAL BEST 6:", list(sorted(trials.losses()))[:6])
# logx = np.log([s['x'] for s in trials.specs])
# print 'TPE MEAN', np.mean(logx)
# print 'TPE STD ', np.std(logx)
thresh = self.thresholds[bandit.name]
print("Thresh", thresh)
assert min(trials.losses()) < thresh
示例13: hyperopt_search
# 需要导入模块: from hyperopt import Trials [as 别名]
# 或者: from hyperopt.Trials import losses [as 别名]
def hyperopt_search(self, parallel=False): # TODO: implement parallel search with MongoTrials
def objective(kwargs):
start = dt.now()
self.get_hyperparam_string(**kwargs)
self.fit_vw()
self.validate_vw()
loss = self.validation_metric_vw()
finish = dt.now()
elapsed = finish - start
self.logger.info("evaluation time for this step: %s" % str(elapsed))
# clean up
subprocess.call(shlex.split('rm %s %s' % (self.train_model, self.holdout_pred)))
to_return = {'status': STATUS_OK,
'loss': loss, # TODO: include also train loss tracking in order to prevent overfitting
'eval_time': elapsed,
'train_command': self.train_command
}
return to_return
trials = Trials()
if self.searcher == 'tpe':
algo = tpe.suggest
elif self.searcher == 'rand':
algo = rand.suggest
logging.debug("starting hypersearch...")
best_params = fmin(objective, space=self.space, trials=trials, algo=algo, max_evals=self.max_evals)
self.logger.debug("the best hyperopt parameters: %s" % str(best_params))
best_configuration = trials.results[np.argmin(trials.losses())]['train_command']
best_loss = trials.results[np.argmin(trials.losses())]['loss']
self.logger.info("\n\nA FULL TRAINING COMMAND WITH THE BEST HYPERPARAMETERS: \n%s" % best_configuration)
self.logger.info("\n\nTHE BEST LOSS VALUE: \n%s" % best_loss)
return best_configuration, best_loss
示例14: TunningParamter
# 需要导入模块: from hyperopt import Trials [as 别名]
# 或者: from hyperopt.Trials import losses [as 别名]
def TunningParamter(param,data,features,feature,source_name,real_value,int_boolean):
data = data[~pd.isnull(all_data[feature])]
print data.shape
ISOTIMEFORMAT='%Y-%m-%d %X'
start = time.strftime(ISOTIMEFORMAT, time.localtime())
trials = Trials()
objective = lambda p : trainModel(p, data, features, feature,source_name,real_value,int_boolean)
best_parameters = fmin(objective, param, algo =tpe.suggest,max_evals=param['max_evals'],trials= trials)
#now we need to get best_param
trials_loss = np.asanyarray(trials.losses(),dtype=float)
best_loss = min(trials_loss)
ind = np.where(trials_loss==best_loss)[0][0]
best_loss_std = trials.trial_attachments(trials.trials[ind])['std']
end = time.strftime(ISOTIMEFORMAT,time.localtime())
dumpMessage(best_parameters, best_loss, best_loss_std,param['task'],source_name,start,end)
示例15: work
# 需要导入模块: from hyperopt import Trials [as 别名]
# 或者: from hyperopt.Trials import losses [as 别名]
def work(self):
bandit = self.bandit
assert bandit.name is not None
algo = partial(tpe.suggest,
gamma=self.gammas.get(bandit.name,
tpe._default_gamma),
prior_weight=self.prior_weights.get(bandit.name,
tpe._default_prior_weight),
n_EI_candidates=self.n_EIs.get(bandit.name,
tpe._default_n_EI_candidates),
)
LEN = self.LEN.get(bandit.name, 50)
trials = Trials()
fmin(passthrough,
space=bandit.expr,
algo=algo,
trials=trials,
max_evals=LEN,
rstate=np.random.RandomState(123),
catch_eval_exceptions=False)
assert len(trials) == LEN
if 1:
rtrials = Trials()
fmin(passthrough,
space=bandit.expr,
algo=rand.suggest,
trials=rtrials,
max_evals=LEN)
print 'RANDOM MINS', list(sorted(rtrials.losses()))[:6]
#logx = np.log([s['x'] for s in rtrials.specs])
#print 'RND MEAN', np.mean(logx)
#print 'RND STD ', np.std(logx)
if 0:
plt.subplot(2, 2, 1)
plt.scatter(range(LEN), trials.losses())
plt.title('TPE losses')
plt.subplot(2, 2, 2)
plt.scatter(range(LEN), ([s['x'] for s in trials.specs]))
plt.title('TPE x')
plt.subplot(2, 2, 3)
plt.title('RND losses')
plt.scatter(range(LEN), rtrials.losses())
plt.subplot(2, 2, 4)
plt.title('RND x')
plt.scatter(range(LEN), ([s['x'] for s in rtrials.specs]))
plt.show()
if 0:
plt.hist(
[t['x'] for t in self.experiment.trials],
bins=20)
#print trials.losses()
print 'TPE MINS', list(sorted(trials.losses()))[:6]
#logx = np.log([s['x'] for s in trials.specs])
#print 'TPE MEAN', np.mean(logx)
#print 'TPE STD ', np.std(logx)
thresh = self.thresholds[bandit.name]
print 'Thresh', thresh
assert min(trials.losses()) < thresh