diff --git a/GPy/core/model.py b/GPy/core/model.py index 35b046fd..c5d318e7 100644 --- a/GPy/core/model.py +++ b/GPy/core/model.py @@ -213,7 +213,7 @@ class Model(Parameterized): self.obj_grads = np.clip(self._transform_gradients(self.objective_function_gradients()), -1e10, 1e10) return obj_f, self.obj_grads - def optimize(self, optimizer=None, start=None, messages=False, max_iters=1000, ipython_notebook=False, **kwargs): + def optimize(self, optimizer=None, start=None, messages=False, max_iters=1000, ipython_notebook=True, **kwargs): """ Optimize the model using self.log_likelihood and self.log_likelihood_gradient, as well as self.priors. @@ -255,16 +255,7 @@ class Model(Parameterized): else: optimizer = optimization.get_optimizer(optimizer) opt = optimizer(start, model=self, max_iters=max_iters, **kwargs) - - try: - from IPython.display import display - from IPython.html import widgets - display(widgets.TextWidget()) - ipython_notebook = True - except: - # Not in Ipython notebook - ipython_notebook = False - + with VerboseOptimization(self, opt, maxiters=max_iters, verbose=messages, ipython_notebook=ipython_notebook) as vo: opt.run(f_fp=self._objective_grads, f=self._objective, fp=self._grads) vo.finish(opt) diff --git a/GPy/core/verbose_optimization.py b/GPy/core/verbose_optimization.py index 78b6127e..1a87b3da 100644 --- a/GPy/core/verbose_optimization.py +++ b/GPy/core/verbose_optimization.py @@ -11,9 +11,8 @@ def exponents(fnow, current_grad): return np.sign(exps) * np.log10(exps).astype(int) class VerboseOptimization(object): - def __init__(self, model, opt, maxiters, verbose=True, current_iteration=0, ipython_notebook=False): + def __init__(self, model, opt, maxiters, verbose=False, current_iteration=0, ipython_notebook=True): self.verbose = verbose - self.ipython_notebook = ipython_notebook if self.verbose: self.model = model self.iteration = current_iteration @@ -26,13 +25,18 @@ class VerboseOptimization(object): self.update() - if self.ipython_notebook: + try: from IPython.display import display from IPython.html.widgets import FloatProgressWidget, HTMLWidget, ContainerWidget self.text = HTMLWidget() self.progress = FloatProgressWidget() self.model_show = HTMLWidget() + self.ipython_notebook = ipython_notebook + except: + # Not in Ipython notebook + self.ipython_notebook = False + if self.ipython_notebook: self.text.set_css('width', '100%') #self.progress.set_css('width', '100%') @@ -140,6 +144,7 @@ class VerboseOptimization(object): self.print_out() if not self.ipython_notebook: - print + print '' print 'Optimization finished in {0:.5g} Seconds'.format(self.stop-self.start) + print 'Optimization status: {0:.5g}'.format(self.status) print