def f(hyperparam): """function to optimize by hyperopt""" # "temporary" directory to use full_path = os.path.join( path, "-".join([str(v) for v in list(hyperparam.values())])) # execute experiment rt.run(setting, location=full_path, ids=list(range(1, trials_per_point + 1)), parallelization=parallelization, force_rerun=False, block=True, **hyperparam) # all jobs should be done res = tres.load_results(full_path) if objective == "max_steps": m, s, n = tres.avg_quantity(res, "steps") val = -m std = s[-1] elif objective == "min_steps": m, s, n = tres.avg_quantity(res, "steps") val = m std = s[-1] elif objective == "max_reward": m, s, n = tres.avg_quantity(res, "return") val = -m std = s[-1] else: print("unknown objective") weights = (np.arange(len(val)) + 1)**2 loss = old_div((val * weights).sum(), weights.sum()) print(time.ctime()) print("Parameters", hyperparam) print("Loss", loss) # use #steps/eps at the moment return { "loss": loss, "num_trials": n[-1], "status": hyperopt.STATUS_OK, "std_last_mean": std }
def f(hyperparam): """function to optimize by hyperopt""" # "temporary" directory to use full_path = os.path.join( path, "-".join([str(v) for v in hyperparam.values()])) # execute experiment rt.run(setting, location=full_path, ids=range(1, trials_per_point + 1), parallelization=parallelization, force_rerun=False, block=True, **hyperparam) # all jobs should be done res = tres.load_results(full_path) if objective == "max_steps": m, s, n = tres.avg_quantity(res, "steps") val = -m std = s[-1] elif objective == "min_steps": m, s, n = tres.avg_quantity(res, "steps") val = m std = s[-1] elif objective == "max_reward": m, s, n = tres.avg_quantity(res, "return") val = -m std = s[-1] else: print "unknown objective" weights = (np.arange(len(val)) + 1) ** 2 loss = (val * weights).sum() / weights.sum() print time.ctime() print "Parameters", hyperparam print "Loss", loss # use #steps/eps at the moment return {"loss": loss, "num_trials": n[-1], "status": hyperopt.STATUS_OK, "std_last_mean": std}
def get_results(self, path): # all jobs should be done res = tres.load_results(path) mapping = {'max_steps': (-1., 'steps'), 'min_steps': (1., 'steps'), 'max_reward': (-1., 'return')} neg, quan = mapping[self.objective] avg, std, n_trials = tres.avg_quantity(res, quan) avg *= neg weights = (np.arange(len(avg)) + 1) ** 2 loss = (avg * weights).sum() / weights.sum() print time.ctime() print "Loss: {:.4g}".format(loss) # use #steps/eps at the moment return {"loss": loss, "num_trials": n_trials[-1], "status": hyperopt.STATUS_OK, "std_last_mean": std[-1]}
def get_results(self, path): # all jobs should be done res = tres.load_results(path) mapping = {'max_steps': (-1., 'steps'), 'min_steps': (1., 'steps'), 'max_reward': (-1., 'return')} neg, quan = mapping[self.objective] avg, std, n_trials = tres.avg_quantity(res, quan) avg *= neg weights = (np.arange(len(avg)) + 1) ** 2 loss = (avg * weights).sum() / weights.sum() print time.ctime() print "Loss: {:.4g}".format(loss) # use #steps/eps at the moment return {"loss": loss, "num_trials": n_trials[-1], "status": hyperopt.STATUS_OK, "std_last_mean": std[-1]}
def plot_avg_sem( data, pad_x=False, pad_y=False, xbars=False, ybars=True, colors=None, markers=None, xerror_every=1, xscale=None, legend=True, **kwargs): """ plots quantity y over x (means and standard error of the mean). The quantities are specified by their id strings, i.e. "return" or "learning steps" :param data: Label->Results ``pad_x, pad_y``: if not enough observations are present for some results, should they be filled with the value of the last available obervation?\n ``xbars, ybars``: show standard error of the mean for the respective quantity colors: dictionary which maps experiment keys to colors.\n ``markers``: dictionary which maps experiment keys to markers. ``xerror_exery``: show horizontal error bars only every .. observation.\n ``legend``: (Boolean) show legend below plot.\n Returns the figure handle of the created plot """ x = "Values" y = "AUCS" style = { "linewidth": 2, "alpha": .7, "linestyle": "-", "markersize": 7, } if colors is None: colors = dict([(l, default_colors[i % len(default_colors)]) for i, l in enumerate(data.keys())]) if markers is None: markers = dict([(l, default_markers[i % len(default_markers)]) for i, l in enumerate(data.keys())]) style.update(kwargs) min_ = np.inf max_ = - np.inf fig = plt.figure() for label, results in data.items(): style["color"] = colors[label] style["marker"] = markers[label] y_mean, y_std, y_num = avg_quantity(results, y, pad_y) y_sem = y_std / np.sqrt(y_num) x_mean, x_std, x_num = avg_quantity(results, x, pad_x) x_sem = x_std / np.sqrt(x_num) if xbars: plt.errorbar(x_mean, y_mean, xerr=x_sem, label=label, ecolor="k", errorevery=xerror_every, **style) else: plt.plot(x_mean, y_mean, label=label, **style) if ybars: plt.fill_between(x_mean, y_mean - y_sem, y_mean + y_sem, alpha=.3, color=style["color"]) max_ = max(np.max(y_mean + y_sem), max_) min_ = min(np.min(y_mean - y_sem), min_) else: max_ = max(y_mean.max(), max_) min_ = min(y_mean.min(), min_) # adjust visible space y_lim = [min_ - .1 * abs(max_ - min_), max_ + .1 * abs(max_ - min_)] if min_ != max_: plt.ylim(y_lim) # axis labels xlabel = x ylabel = y plt.xlabel(xlabel, fontsize=16) plt.ylabel(ylabel, fontsize=16) if xscale: plt.xscale(xscale) if legend: box = plt.gca().get_position() plt.gca().set_position([box.x0, box.y0 + box.height * 0.2, box.width, box.height * 0.8]) legend_handle = plt.legend(loc='upper center', bbox_to_anchor=(0.5, -0.15), fancybox=True, shadow=True, ncol=2) return fig