Skip to content

Commit

Permalink
[verbose] changed verbose optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
mzwiessele committed Mar 8, 2016
1 parent c9ac669 commit c68dd43
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 17 deletions.
1 change: 1 addition & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ exclude_lines =
raise DeprecationWarning
except NotImplementedError
except NotImplemented
except ImportError
except AssertionError
except KeyError
raise ValueError
Expand Down
1 change: 0 additions & 1 deletion paramz/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ def optimize(self, optimizer=None, start=None, messages=False, max_iters=1000, i

with VerboseOptimization(self, opt, maxiters=max_iters, verbose=messages, ipython_notebook=ipython_notebook, clear_after_finish=clear_after_finish) as vo:
opt.run(start, f_fp=self._objective_grads, f=self._objective, fp=self._grads)
vo.finish(opt)

self.optimization_runs.append(opt)

Expand Down
6 changes: 5 additions & 1 deletion paramz/optimization/optimization.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ def opt(self, x_init, f_fp=None, f=None, fp=None):
def _check_for_climin():
try:
import climin
except ImportError:
except ImportError:
raise ImportError("Need climin to run this optimizer. See https://github.com/BRML/climin.")

class Opt_Adadelta(Optimizer):
Expand All @@ -272,6 +272,8 @@ def opt(self, x_init, f_fp=None, f=None, fp=None):
self.x_opt = opt.wrt
self.status = 'maximum number of function evaluations exceeded '
break
else: # pragma: no cover
pass

class RProp(Optimizer):
# We want the optimizer to know some things in the Optimizer implementation:
Expand Down Expand Up @@ -306,6 +308,8 @@ def opt(self, x_init, f_fp=None, f=None, fp=None):
self.x_opt = opt.wrt
self.status = 'maximum number of function evaluations exceeded'
break
else: # pragma: no cover
pass

def get_optimizer(f_min):

Expand Down
34 changes: 19 additions & 15 deletions paramz/optimization/verbose_optimization.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,14 @@ def __init__(self, model, opt, maxiters, verbose=False, current_iteration=0, ipy
self.maxiters = maxiters
self.len_maxiters = len(str(maxiters))
self.opt_name = opt.opt_name
self.opt = opt
self.model.add_observer(self, self.print_status)
self.status = 'running'
self.clear = clear_after_finish

self.update()

try:
try:# pragma: no cover
from IPython.display import display
from ipywidgets import IntProgress, HTML, Box, VBox, HBox, FlexBox
self.text = HTML(width='100%')
Expand All @@ -65,7 +66,7 @@ def __init__(self, model, opt, maxiters, verbose=False, current_iteration=0, ipy
# Not in Ipython notebook
self.ipython_notebook = False

if self.ipython_notebook:
if self.ipython_notebook:# pragma: no cover
left_col = VBox(children=[self.progress, self.text], padding=2, width='40%')
right_col = Box(children=[self.model_show], padding=2, width='60%')
self.hor_align = FlexBox(children = [left_col, right_col], width='100%', orientation='horizontal')
Expand Down Expand Up @@ -124,7 +125,7 @@ def print_out(self, seconds):
else:
ms = (seconds%1)*100
self.timestring = '{m:0>2d}m{s:0>2d}s{ms:0>2d}'.format(m=int(m), s=int(s), ms=int(ms))
if self.ipython_notebook:
if self.ipython_notebook: # pragma: no cover
names_vals = [['optimizer', "{:s}".format(self.opt_name)],
['runtime', "{:>s}".format(self.timestring)],
['evaluation', "{:>0{l}}".format(self.iteration, l=self.len_maxiters)],
Expand Down Expand Up @@ -187,25 +188,28 @@ def update(self):
self.current_gradient = np.nan

def finish(self, opt):
self.status = opt.status
if self.verbose and self.ipython_notebook:
if 'conv' in self.status.lower():
self.progress.bar_style = 'success'
elif self.iteration >= self.maxiters:
self.progress.bar_style = 'warning'
else:
self.progress.bar_style = 'danger'

import warnings
warnings.warn('Finish now automatic, deprecating', DeprecationWarning)

def __exit__(self, type, value, traceback):
if self.verbose:
self.status = self.opt.status

self.stop = time.time()
self.model.remove_observer(self)
self.print_out(self.stop - self.start)

self.print_out(self.stop - self.start)
if not self.ipython_notebook:
print()
print('Runtime: {}'.format("{:>9s}".format(self.timestring)))
print('Optimization status: {0}'.format(self.status))
print()
elif self.clear:
elif self.clear:# pragma: no cover
self.hor_align.close()
else:# pragma: no cover
if 'conv' in self.status.lower():
self.progress.bar_style = 'success'
elif self.iteration >= self.maxiters:
self.progress.bar_style = 'warning'
else:
self.progress.bar_style = 'danger'
1 change: 1 addition & 0 deletions paramz/tests/parameterized_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ def test_fixing_optimize(self):
val = float(self.testmodel.kern.lengthscale)
self.testmodel.randomize()
self.assertEqual(val, self.testmodel.kern.lengthscale)
self.testmodel.optimize(max_iters=2)

def test_regular_expression_misc(self):
self.assertTrue(self.testmodel[''].checkgrad())
Expand Down
64 changes: 64 additions & 0 deletions paramz/tests/verbose_optimize_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#===============================================================================
# Copyright (c) 2016, Max Zwiessele
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# * Neither the name of paramz.tests.verbose_optimize_tests nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#===============================================================================

import unittest
from paramz.optimization.verbose_optimization import VerboseOptimization
from paramz.optimization.optimization import opt_bfgs

class Test(unittest.TestCase):
def setUp(self):
class Stub(object):
obj_grads = [10,0]
def add_observer(self, m, f):
self.obs = m
self.obs_f = f
def objective_function(self):
return 10

self.vo = VerboseOptimization(Stub(), opt_bfgs(), 100, verbose=True)

def test_timestrings(self):
self.vo.print_out(0)
self.assertEqual(self.vo.timestring, '00s00')

self.vo.print_out(10.2455)
self.assertEqual(self.vo.timestring, '10s24')

self.vo.print_out(120)
self.assertEqual(self.vo.timestring, '02m00s00')

self.vo.print_out(60*60+120+12.2455)
self.assertEqual(self.vo.timestring, '01h02m12')

self.vo.print_out(2*3600*24+60*60+120+12.2455)
self.assertEqual(self.vo.timestring, '02d01h02')

def test_finish(self):
self.assertEqual(self.vo.status, 'running')

0 comments on commit c68dd43

Please sign in to comment.