def test_plots_work(): """Basic smoke tests to make sure plotting doesn't crash.""" SPACE = [ Integer(1, 20, name='max_depth'), Integer(2, 100, name='min_samples_split'), Integer(5, 30, name='min_samples_leaf'), Integer(1, 30, name='max_features'), Categorical(['gini', 'entropy'], name='criterion'), Categorical(list('abcdefghij'), name='dummy'), ] def objective(params): clf = DecisionTreeClassifier(random_state=3, **{ dim.name: val for dim, val in zip(SPACE, params) if dim.name != 'dummy' }) return -np.mean(cross_val_score(clf, *load_breast_cancer(True))) res = gp_minimize(objective, SPACE, n_calls=10, random_state=3) plots.plot_convergence(res) plots.plot_evaluations(res) plots.plot_objective(res) plots.plot_regret(res)
def test_plots_work_without_cat(): """Basic smoke tests to make sure plotting doesn't crash.""" SPACE = [ Integer(1, 20, name='max_depth'), Integer(2, 100, name='min_samples_split'), Integer(5, 30, name='min_samples_leaf'), Integer(1, 30, name='max_features'), ] def objective(params): clf = DecisionTreeClassifier(random_state=3, **{ dim.name: val for dim, val in zip(SPACE, params) if dim.name != 'dummy' }) return -np.mean(cross_val_score(clf, *load_breast_cancer(True))) res = gp_minimize(objective, SPACE, n_calls=10, random_state=3) plots.plot_convergence(res) plots.plot_evaluations(res) plots.plot_objective(res) plots.plot_objective(res, minimum='expected_minimum') plots.plot_objective(res, sample_source='expected_minimum', n_minimum_search=10) plots.plot_objective(res, sample_source='result') plots.plot_regret(res)
def plot_evaluations(self, filename=None, n_points=25, n_samples=250): # if `savefig(filename)`, don't `show()` if filename is not None: import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt import skopt from skopt.plots import plot_evaluations spaces = list(self.spaces.values()) pnames = list(self.spaces.keys()) opt = skopt.Optimizer(spaces, "ET", acq_optimizer="sampling") hyperpars, objective = self.trials_and_results # skopt minimizes. Therefore use: acc = 1-acc objective = 1-objective hyperpars = [list(f) for f in hyperpars] objective = list(objective) opt.tell(hyperpars, objective) opt_result = opt.run(lambda x: 0, n_iter=0) plot_evaluations(opt_result, bins=20, dimensions=pnames) plt.tight_layout() if filename is None: plt.show() else: plt.savefig(filename)
def test_hyper_search_example_trainer(): list_data = generate_toy_data(100000) data_split_hash = ExampleTrainer(list_data, seed=42).data_split_hash @use_named_args(space) def run_example_trainer(**kwargs): cfg = AttrDict(kwargs) cfg.batch_size = int(round(cfg.batch_size)) cfg.act_name = 'leaky_relu' cfg.act_params = (cfg.leaky_param,) del cfg['leaky_param'] cfg.max_epochs = 1024 trainer = ExampleTrainer(list_data, seed=42, nbr_readouts=0, **cfg) assert data_split_hash == trainer.data_split_hash makedirs('data/hyper', exist_ok=True) path = cfg.get_hashed_path('data/hyper') try: trainer.restore_best_state(path) except EnvironmentError: trainer.train(path) return trainer.best_validation_score res_gp = skopt.gp_minimize(run_example_trainer, space, n_calls=1, random_state=0, n_random_starts=1) print([d.name for d in res_gp.space.dimensions]) ts = get_time_stamp() plt.close() plot_objective(res_gp) plt.savefig('data/hyper/%s-objective.png' % ts) plt.close() plot_evaluations(res_gp) plt.savefig('data/hyper/%s-evaluations.png' % ts) skopt.dump(res_gp, 'data/hyper/%s-result.gz' % ts, store_objective=False)
def plot_evaluations_(OptimizeResult, dimensions, fig_savepath, format='PNG', dpi=300): """ Visualize the order in which points where sampled. The scatter plot matrix shows at which points in the search space and in which order samples were evaluated. Pairwise scatter plots are shown on the off-diagonal for each dimension of the search space. The order in which samples were evaluated is encoded in each point's color. The diagonal shows a histogram of sampled values for each dimension. A red point indicates the found minimum. Note: search spaces that contain Categorical dimensions are currently not supported by this function. Parameters result [OptimizeResult] The result for which to create the scatter plot matrix. bins [int, bins=20]: Number of bins to use for histograms on the diagonal. dimensions [list of str, default=None] Labels of the dimension variables. None defaults to space.dimensions[i].name, or if also None to ['X_0', 'X_1', ..]. fig_savepath [string]: The path to save a convergence figure Returns ax: [Axes]: The matplotlib axes. """ fig = plt.figure(num=1, figsize=(12, 12)) ax0 = plt.gca() plot_evaluations(OptimizeResult, dimensions=dimensions) plt.tight_layout() # plt.subplots_adjust(left=0.08, bottom=0.12, right=0.98, top=0.98, hspace=0.1, wspace=0.2) plt.savefig(fig_savepath, format=format, dpi=dpi)
def _plot(self, names=['scores', 'evals', 'partial']): if 'scores' in names: plt.plot(self.result.func_vals) if 'evals' in names: plot_evaluations(self.result) if 'partial' in names: plot_objective(self.result)
def get_maximum(self, n_calls=10, n_random_starts=5, noise=0.01, verbose=True, plot_results=False): """Performs Bayesian optimization by iteratively evaluating the given function on points that are likely to be a global maximum. After the optimization, the evaluated values are stored in self.x_values and self.y_values and appended to the data file if provided. Args: n_calls (int): Number of iterations n_random_starts (int): Initial random evaluations if no previpus values are provided noise (float): Estimated noise in the data verbose (bool): Whether to print optimization details at each evaluation plot_results (bool): Whether to plot an analysis of the solution Returns: A tuple (x, y) with the argmax and max found of the evaluated function. """ x_values = [x for x in self.x_values] if len(self.x_values) > 0 else None # Negate y_values because skopt performs minimization instead of maximization y_values = [-y for y in self.y_values] if len(self.y_values) > 0 else None rand_starts = 2 if len(self.x_values) == 0 and n_random_starts == 0 else n_random_starts res = gp_minimize(func=GaussianProcessSearch.evaluate, dimensions=self.search_space, n_calls=n_calls, n_random_starts=rand_starts, acq_func='EI', acq_optimizer='lbfgs', x0=x_values, y0=y_values, noise=noise, n_jobs=-1, callback=self.__save_res, verbose=verbose) if plot_results: ax = plot_objective(res) plt.show() ax = plot_evaluations(res) plt.show() self.x_values = [[float(val) for val in point] for point in res.x_iters] self.y_values = [-val for val in res.func_vals] if self.output_file is not None: self.save_values() try: ax = plot_objective(res) plt.savefig( self.output_file + "_objective_plot.png") except Exception as e: print(e) try: ax = plot_evaluations(res) plt.savefig( self.output_file + "_evaluations_plot.png") except Exception as e: print(e) return res.x, -res.fun
def save_skopt_plots(dirpath,search_result,prior_names): if not os.path.exists(dirpath): os.makedirs(dirpath) # ---- Evalution plot_evaluations(search_result, bins=20) plt.savefig( os.path.join(dirpath,'evaluation_plot.png') ) # ---- Convergence (previously looked better enquire what is going on) plot_convergence(search_result) plt.savefig( os.path.join(dirpath,'convergence_plot.png') ) # ---- Partial Dependence plots are only approximations of the modelled fitness function # - which in turn is only an approximation of the true fitness function in fitness plot_objective(result=search_result) plt.savefig( os.path.join(dirpath,'objective_plot.png') )
def run(self): result = self.input()["collection"].targets[0]["opt"].load().run( None, 0) output = self.output() plot_convergence(result) output["convergence"].dump(plt.gcf(), bbox_inches="tight") plt.close() plot_evaluations(result, bins=10) output["evaluations"].dump(plt.gcf(), bbox_inches="tight") plt.close() if self.plot_objective: plot_objective(result) output["objective"].dump(plt.gcf(), bbox_inches="tight") plt.close()
def make_plots(results, dir): import matplotlib import matplotlib.pyplot as plt # https://matplotlib.org/faq/usage_faq.html#non-interactive-example # https://matplotlib.org/api/_as_gen/matplotlib.pyplot.savefig.html if not dir.exists(): dir.mkdir(parents=True, exist_ok=True) # WIP: there is currently no support for plotting categorical variables... # You have to manually checkout this pull request: # git pr 675 # install https://github.com/tj/git-extras # git pull origin master # https://github.com/scikit-optimize/scikit-optimize/pull/675 from skopt.plots import plot_convergence _ = plot_convergence(results) plt.savefig(dir / 'plot_convergence.png') from skopt.plots import plot_objective _ = plot_objective(results) plt.savefig(dir / 'plot_objective.png') from skopt.plots import plot_regret _ = plot_regret(results) plt.savefig(dir / 'plot_regret.png') from skopt.plots import plot_evaluations _ = plot_evaluations(results) plt.savefig(dir / 'plot_evaluations.png') # compare convergence... # https://github.com/scikit-optimize/scikit-optimize/blob/master/examples/strategy-comparison.ipynb # for all runs... # from skopt.plots import plot_convergence # plot = plot_convergence(("dummy_minimize", dummy_res), # ("gp_minimize", gp_res), # ("forest_minimize('rf')", rf_res), # ("forest_minimize('et)", et_res), # true_minimum=0.397887, yscale="log") # plot.legend(loc="best", prop={'size': 6}, numpoints=1); # parallel optimization # in the yaml: # parallel: X (if you have a small dataset.self.) # https://github.com/scikit-optimize/scikit-optimize/blob/master/examples/parallel-optimization.ipynb # from sklearn.externals.joblib import Parallel, delayed # x = optimizer.ask(n_points=4) # x is a list of n_points points # y = Parallel()(delayed(branin)(v) for v in x) # evaluate points in parallel # optimizer.tell(x, y) # checkpoints? # https://github.com/scikit-optimize/scikit-optimize/blob/master/examples/interruptible-optimization.ipynb # https://github.com/scikit-optimize/scikit-optimize/blob/master/examples/store-and-load-results.ipynb # poor man's solution: # import pickle # with open('my-optimizer.pkl', 'wb') as f: # pickle.dump(opt, f) # with open('my-optimizer.pkl', 'rb') as f: # opt_restored = pickle.load(f)
def show_optimized(optimized, output=None): """ Takes the dump of an optimizer and outputs relevant infos Both with visualisations, in the terminal and in the optional output file where it puts the best parameters in a JSON format """ print(optimized) if output != None: x = optimized.x data = dict() data["jnd_length"] = x[0] data["jnd_angle"] = x[1] data["jnd_direction"] = x[2] data["angle_baseline_a"] = x[3] data["angle_baseline_d"] = x[4] if len(x) > 5: data["coef_l"] = x[5] data["coef_a"] = x[6] data["coef_d"] = x[7] else: data["coef_l"] = 1. data["coef_a"] = 1. data["coef_d"] = 1. json.dump(data, open(output, "w"), indent=4) p1 = plot_objective(optimized) p2 = plot_evaluations(optimized) plt.set_cmap("viridis") plt.grid() plt.legend() plt.show()
def plot_evaluations(self): results = SkoptResults() results.space = self.optimizer.space results.x_iters = self.optimizer.Xi results = self._convert_categorical_hyperparameters(results) results.x = results.x_iters[np.argmin(self.optimizer.yi)] plt.figure(figsize=(10, 10)) return plot_evaluations(results)
def plot_skopt_evaluations(opt_res, path): """Plots the evaluations plot from the skopt package. Args: opt_res (scipy.optimize.OptimizeResult): Optimization result object. path (str): Directory at which to save plot. """ if not os.path.exists(path): os.makedirs(path) skplot.plot_evaluations(result=opt_res, bins=32, dimensions=None, plot_dims=None) fig = plt.gcf() fig.tight_layout() fig.savefig(path + "evaluations") fig.show()
def plotting(results): fig1 = plt.figure(1) ax1 = plt.axes() plots.plot_convergence(results) plt.savefig(logdir + 'convergence.png', dpi=500) plt.close() fig2 = plt.figure(1) ax2 = plt.axes() plots.plot_evaluations(results) plt.savefig(logdir + 'eval.png', dpi=500) plt.close() # plot_objective can only be called if the parameters are being calculated instead of selected randomly if len(results.models) > 1: fig3 = plt.figure(3) ax3 = plt.axes() plots.plot_objective(results) plt.savefig(logdir + 'objective.png', dpi=500) plt.close() filename = logdir + 'gp_min.sav' pickle.dump(results, open(filename, 'wb'))
def run(self): from skopt.plots import plot_objective, plot_evaluations, plot_convergence import matplotlib.pyplot as plt result = self.input().load().run(None, 0) output = self.output() with output.targets["convergence"].localize("w") as tmp: plot_convergence(result) tmp.dump(plt.gcf(), bbox_inches="tight") plt.close() with output.targets["evaluations"].localize("w") as tmp: plot_evaluations(result, bins=10) tmp.dump(plt.gcf(), bbox_inches="tight") plt.close() if self.has_fitted_model(): plot_objective(result) with output.targets["objective"].localize("w") as tmp: tmp.dump(plt.gcf(), bbox_inches="tight") plt.close()
def plotting(results): fig1 = plt.figure(1) ax1 = plt.axes() plots.plot_convergence(results) plt.savefig(logdir + 'convergence.png', dpi=500) plt.close() fig2 = plt.figure(1) ax2 = plt.axes() plots.plot_evaluations(results) plt.savefig(logdir + 'eval.png', dpi=500) plt.close() #plot_objective kann nur aufgerufen werden, sobald die Parameter berechnet und nicht mehr zufällig gewählt sind if len(results.models) > 1: fig3 = plt.figure(3) ax3 = plt.axes() plots.plot_objective(results) plt.savefig(logdir + 'objective.png', dpi=500) plt.close() filename = logdir + 'gp_min.sav' pickle.dump(results, open(filename, 'wb'))
def save_evaluations(search_result, folder_path): """ :param search_result: :param folder_path: :return: """ ax = plot_evaluations(result=search_result) folder_path.mkdir(exist_ok=True, parents=True) plt.savefig(folder_path / 'evaluations.png') plt.close() print(f'Evaluation saved at {folder_path / "evaluations.png"}')
def main(): from functools import partial from skopt.plots import plot_evaluations, plot_objective from skopt import gp_minimize, forest_minimize, dummy_minimize bounds = [ (0., 1.), ] * 8 n_calls = 200 forest_res = gp_minimize(Main_Loop, bounds) _ = plot_evaluations(forest_res) _ = plot_objective(forest_res)
def skopt_plots(search_result): from skopt.plots import plot_evaluations, plot_objective, plot_convergence _ = plot_evaluations(search_result) plt.savefig('evaluations', dpi=400, bbox_inches='tight') plt.show() _ = plot_objective(search_result) plt.savefig('objective', dpi=400, bbox_inches='tight') plt.show() _ = plot_convergence(search_result) plt.savefig('convergence', dpi=400, bbox_inches='tight') plt.show()
def plot_evaluations(self, *args, **kwargs): """Calls skopt.plots.plot_evaluations.""" def _coconut_mock_11(self, *args, **kwargs): return self, args, kwargs while True: from skopt.plots import plot_evaluations try: _coconut_tre_check_1 = plot_evaluations is _coconut_recursive_func_30 except _coconut.NameError: _coconut_tre_check_1 = False if _coconut_tre_check_1: self, args, kwargs = _coconut_mock_11(self.get_skopt_result(), *args, **kwargs) continue else: return plot_evaluations(self.get_skopt_result(), *args, **kwargs) return None
def skopt_plots(search_result, pref=os.getcwd()): from skopt.plots import plot_evaluations, plot_objective, plot_convergence plt.close('all') _ = plot_evaluations(search_result) plt.savefig(os.path.join(pref, 'evaluations'), dpi=400, bbox_inches='tight') plt.close('all') _ = plot_objective(search_result) plt.savefig(os.path.join(pref, 'objective'), dpi=400, bbox_inches='tight') plt.close('all') _ = plot_convergence(search_result) plt.savefig(os.path.join(pref, 'convergence'), dpi=400, bbox_inches='tight')
def plot_evaluations(self, *args, **kwargs): """Calls skopt.plots.plot_evaluations.""" def _coconut_mock_func(self, *args, **kwargs): return self, args, kwargs while True: if not self._examples: raise ValueError("no existing data available to be plotted") skopt_backend = self._get_skopt_backend() try: _coconut_is_recursive = plot_evaluations is _coconut_recursive_func_27 except _coconut.NameError: _coconut_is_recursive = False if _coconut_is_recursive: self, args, kwargs = _coconut_mock_func(skopt_backend.result, *args, **kwargs) continue else: return plot_evaluations(skopt_backend.result, *args, **kwargs) return None
def plot_results(base_dir, results): """ Plot convergence, evaluations, and surrogate objectives to files. """ ax = plot_convergence(results) plt.savefig(os.path.join(base_dir, 'convergence.pdf'), bbox_inches='tight') plt.savefig(os.path.join(base_dir, 'convergence.png'), bbox_inches='tight') plt.close() ax = plot_evaluations(results) reformat_plot(ax) plt.savefig(os.path.join(base_dir, 'evaluations.pdf'), bbox_inches='tight') plt.savefig(os.path.join(base_dir, 'evaluations.png'), bbox_inches='tight') plt.close() ax = plot_objective(results, sample_source='result') reformat_plot(ax) plt.savefig(os.path.join(base_dir, 'surrogate_objective.pdf'), bbox_inches='tight') plt.savefig(os.path.join(base_dir, 'surrogate_objective.png'), bbox_inches='tight') plt.close()
def send_plot_evaluations(results, experiment=None, channel_name='evaluations'): """Logs skopt plot_evaluations figure to neptune. Image channel `evaluations` is created and the output of the plot_evaluations function is first covented to `neptune.Image` and then sent to neptune. Args: results('scipy.optimize.OptimizeResult'): Results object that is typically an output of the function like `skopt.forest_minimize(...)` experiment(`neptune.experiments.Experiment`): Neptune experiment. Default is None. Examples: Run skopt training:: ... results = skopt.forest_minimize(objective, space, base_estimator='ET', n_calls=100, n_random_starts=10) Send skopt plot_evaluations figure to neptune:: import neptune import neptunecontrib.monitoring.skopt as sk_utils neptune.init(project_qualified_name='USER_NAME/PROJECT_NAME') sk_monitor.send_plot_evaluations(results) """ _exp = experiment if experiment else neptune fig = plt.figure(figsize=(16, 12)) fig = axes2fig(sk_plots.plot_evaluations(results, bins=10), fig=fig) with tempfile.NamedTemporaryFile(suffix='.png') as f: fig.savefig(f.name) _exp.send_image(channel_name, f.name)
def skopt_plots(search_result, pref=os.getcwd()): plt.close('all') _ = plot_evaluations(search_result) plt.savefig(os.path.join(pref, 'evaluations'), dpi=400, bbox_inches='tight') if search_result.space.n_dims == 1: pass else: plt.close('all') _ = plot_objective(search_result) plt.savefig(os.path.join(pref, 'objective'), dpi=400, bbox_inches='tight') plt.close('all') _ = plot_convergence(search_result) plt.savefig(os.path.join(pref, 'convergence'), dpi=400, bbox_inches='tight')
def plot_results(self, *args): '''Produce and save result plots. ''' # self.ensure_result() ensure_dir(self.extend_path('plots')) if _plot_histogram_error: logging.warn(_plot_histogram_error) else: for dim_name in self.optimizer_keys: fig, ax = plot_histogram(result=self.result) #, dimension_name=dim_name) fig.savefig(self.extend_path('plots/histogram_%s.pdf' % dim_name)) # ax = plot_objective(result=self.result,) # dimension_names=self.non_categorical_dimensions) # fig = plt.gcf() # fig.savefig(self.extend_path('plots/objectives.pdf')) ax = plot_evaluations( result=self.result,) # dimension_names=self.non_categorical_dimensions) fig = plt.gcf() fig.savefig(self.extend_path('plots/evaluations.pdf'))
def _log_plot_evaluations(results, experiment, name='diagnostics'): expect_not_a_run(experiment) fig = plt.figure(figsize=(16, 12)) fig = axes2fig(sk_plots.plot_evaluations(results, bins=10), fig=fig) experiment.log_image(name, fig)
kappa [float, default=1.96]: Controls how much of the variance in the predicted values should be taken into account. Used when the acquisition is "LCB" (lower confidence bound). If set high, then we are favouring exploration over exploitation and vice versa. xi [float, default=0.01]: Controls how much improvement one wants over the previous best values. Used when the acquisition is either "EI" or "PI". to use this, i think i need a way to scale xi by the variance of the signal, which howver will depend on the participant and will have to be adapted online... """ # run the experiment for i in range(50): next_x = opt.ask() f_val = -1 * objective(next_x) # hart6(next_x) ## branin(next_x) result = opt.tell(next_x, f_val) #print('iteration:', i, next_x, f_val) with open('my_optimizer.pkl', 'wb') as f: pickle.dump(opt, f) # visualize evaluations and partial dependence plots ( Friedman (2001) (doi:10.1214/aos/1013203451 section 8.2)) # good explanation: https://www.kaggle.com/dansbecker/partial-dependence-plots #visulize how the value of xj influences the average predicted values y (marginalize over of all other variables). # marginalize: find the probability distribution of a random variable REGARDLESS of the value taken # by all other random variables. concretly, summ joint values of the variable we are interested in # and the one we want to marginalize over all possible values of the variable we want to marginalize plot_evaluations(result, bins=10) plot_objective(result) print(result.x) #save_fig('kappa_def') #gp_minimize(a=1)
# Next we use an extra trees based minimizer to find one of the minima of the # :class:`benchmarks.branin` function. Then we visualize at which points the objective is being # evaluated using :class:`plots.plot_evaluations`. from functools import partial from skopt.plots import plot_evaluations from skopt import gp_minimize, forest_minimize, dummy_minimize bounds = [(-5.0, 10.0), (0.0, 15.0)] n_calls = 160 forest_res = forest_minimize(branin, bounds, n_calls=n_calls, base_estimator="ET", random_state=4) _ = plot_evaluations(forest_res, bins=10) ############################################################################# # :class:`plots.plot_evaluations` creates a grid of size `n_dims` by `n_dims`. # The diagonal shows histograms for each of the dimensions. In the lower # triangle (just one plot in this case) a two dimensional scatter plot of all # points is shown. The order in which points were evaluated is encoded in the # color of each point. Darker/purple colors correspond to earlier samples and # lighter/yellow colors correspond to later samples. A red point shows the # location of the minimum found by the optimization process. # # You should be able to see that points start clustering around the location # of the true miminum. The histograms show that the objective is evaluated # more often at locations near to one of the three minima. # # Using :class:`plots.plot_objective` we can visualise the one dimensional partial
# optimization res = gp_minimize( f, # the function to minimize dimensions=dimensions, # the bounds on each dimension of x acq_func="EI", # the acquisition function n_calls=1000, # the number of evaluations of f n_random_starts=10, # the number of random initialization points #x0=[5,8,0.1], # initial points noise=0.2**2, # the noise level (variance)(optional) random_state=1234) # the random seed # print print("Best fitness:", res.fun) print("Best parameters:", res.x) # write to pickle file time_ = time.strftime("%Y-%m-%d_%H-%M-%S", time.gmtime()) with open( '/home/wuch/tracking_ws/src/Tracking/tracking_2d_lidar/res/' + time_ + '.pkl', 'wb') as file_: pickle.dump(res, file_) # plot fig, ax = plt.subplots() ax = plot_convergence(res) ax = plot_evaluations(res) ax = plot_objective(res) plt.show()
# test run # _fitness(x=default_parameters) # run model search_result = gp_minimize(func=_fitness, dimensions=dimensions, acq_func='EI', # Expected Improvement. n_calls=50, x0=default_parameters) # print results print("############RESULTS############") space = search_result.space print("RESULTS: ", space.point_to_dict(search_result.x)) print("Fitness value: ", search_result.fun) print("Nodes searched: ") print(sorted(zip(search_result.func_vals, search_result.x_iters))) print("##############################") # plot convergence plot_convergence(search_result) plt.show() # plot dependence # dim_names = ['learning_rate', 'batch_size', 'train_steps'] dim_names = ['learning_rate', 'batch_size'] fig, ax = plot_objective(result=search_result, dimension_names=dim_names) plt.show() fig, ax = plot_evaluations(result=search_result, dimension_names=dim_names) plt.show()