[opt] unified printing of status of optimization

This commit is contained in:
Max Zwiessele 2015-01-15 09:02:45 +00:00
parent 4deac0103c
commit 57e941140c
4 changed files with 17 additions and 8 deletions

View file

@ -256,8 +256,9 @@ class Model(Parameterized):
optimizer = optimization.get_optimizer(optimizer) optimizer = optimization.get_optimizer(optimizer)
opt = optimizer(start, model=self, max_iters=max_iters, **kwargs) opt = optimizer(start, model=self, max_iters=max_iters, **kwargs)
with VerboseOptimization(self, opt, maxiters=max_iters, verbose=messages, ipython_notebook=ipython_notebook): 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) opt.run(f_fp=self._objective_grads, f=self._objective, fp=self._grads)
vo.finish(opt)
self.optimization_runs.append(opt) self.optimization_runs.append(opt)

View file

@ -22,6 +22,7 @@ class VerboseOptimization(object):
self.len_maxiters = len(str(maxiters)) self.len_maxiters = len(str(maxiters))
self.opt_name = opt.opt_name self.opt_name = opt.opt_name
self.model.add_observer(self, self.print_status) self.model.add_observer(self, self.print_status)
self.status = 'running'
self.update() self.update()
@ -65,7 +66,7 @@ class VerboseOptimization(object):
else: else:
self.exps = exponents(self.fnow, self.current_gradient) self.exps = exponents(self.fnow, self.current_gradient)
print 'Running {} Code:'.format(self.opt_name) print 'Running {} Code:'.format(self.opt_name)
print ' {3:5s} {0:{mi}s} {1:11s} {2:11s}'.format("i", "f", "|g|", "secs", mi=self.len_maxiters) print ' {3:7s} {0:{mi}s} {1:11s} {2:11s}'.format("i", "f", "|g|", "secs", mi=self.len_maxiters)
def __enter__(self): def __enter__(self):
self.start = time.time() self.start = time.time()
@ -78,6 +79,7 @@ class VerboseOptimization(object):
['evaluation', "{:>0{l}}".format(self.iteration, l=self.len_maxiters)], ['evaluation', "{:>0{l}}".format(self.iteration, l=self.len_maxiters)],
['objective', "{: > 12.3E}".format(self.fnow)], ['objective', "{: > 12.3E}".format(self.fnow)],
['||gradient||', "{: >+12.3E}".format(float(self.current_gradient))], ['||gradient||', "{: >+12.3E}".format(float(self.current_gradient))],
['status', "{:s}".format(self.status)],
] ]
#message = "Lik:{:5.3E} Grad:{:5.3E} Lik:{:5.3E} Len:{!s}".format(float(m.log_likelihood()), np.einsum('i,i->', grads, grads), float(m.likelihood.variance), " ".join(["{:3.2E}".format(l) for l in m.kern.lengthscale.values])) #message = "Lik:{:5.3E} Grad:{:5.3E} Lik:{:5.3E} Len:{!s}".format(float(m.log_likelihood()), np.einsum('i,i->', grads, grads), float(m.likelihood.variance), " ".join(["{:3.2E}".format(l) for l in m.kern.lengthscale.values]))
html_begin = """<style type="text/css"> html_begin = """<style type="text/css">
@ -109,7 +111,7 @@ class VerboseOptimization(object):
if b: if b:
self.exps = n_exps self.exps = n_exps
print '\r', print '\r',
print '{3:> 6.2g} {0:>0{mi}g} {1:> 12e} {2:> 12e}'.format(self.iteration, float(self.fnow), float(self.current_gradient), time.time()-self.start, mi=self.len_maxiters), # print 'Iteration:', iteration, ' Objective:', fnow, ' Scale:', beta, '\r', print '{3:> 7.2g} {0:>0{mi}g} {1:> 12e} {2:> 12e}'.format(self.iteration, float(self.fnow), float(self.current_gradient), time.time()-self.start, mi=self.len_maxiters), # print 'Iteration:', iteration, ' Objective:', fnow, ' Scale:', beta, '\r',
sys.stdout.flush() sys.stdout.flush()
def print_status(self, me, which=None): def print_status(self, me, which=None):
@ -128,6 +130,9 @@ class VerboseOptimization(object):
else: else:
self.current_gradient = np.nan self.current_gradient = np.nan
def finish(self, opt):
self.status = opt.status
def __exit__(self, type, value, traceback): def __exit__(self, type, value, traceback):
if self.verbose: if self.verbose:
self.stop = time.time() self.stop = time.time()

View file

@ -37,7 +37,7 @@ class Optimizer():
self.x_opt = None self.x_opt = None
self.funct_eval = None self.funct_eval = None
self.status = None self.status = None
self.max_f_eval = int(max_f_eval) self.max_f_eval = int(max_iters)
self.max_iters = int(max_iters) self.max_iters = int(max_iters)
self.bfgs_factor = bfgs_factor self.bfgs_factor = bfgs_factor
self.trace = None self.trace = None

View file

@ -61,6 +61,7 @@ def SCG(f, gradf, x, optargs=(), maxiters=500, max_f_eval=np.inf, display=True,
function_eval = 1 function_eval = 1
fnow = fold fnow = fold
gradnew = gradf(x, *optargs) # Initial gradient. gradnew = gradf(x, *optargs) # Initial gradient.
function_eval += 1
#if any(np.isnan(gradnew)): #if any(np.isnan(gradnew)):
# raise UnexpectedInfOrNan, "Gradient contribution resulted in a NaN value" # raise UnexpectedInfOrNan, "Gradient contribution resulted in a NaN value"
current_grad = np.dot(gradnew, gradnew) current_grad = np.dot(gradnew, gradnew)
@ -96,6 +97,7 @@ def SCG(f, gradf, x, optargs=(), maxiters=500, max_f_eval=np.inf, display=True,
sigma = sigma0 / np.sqrt(kappa) sigma = sigma0 / np.sqrt(kappa)
xplus = x + sigma * d xplus = x + sigma * d
gplus = gradf(xplus, *optargs) gplus = gradf(xplus, *optargs)
function_eval += 1
theta = np.dot(d, (gplus - gradnew)) / sigma theta = np.dot(d, (gplus - gradnew)) / sigma
# Increase effective curvature and evaluate step size alpha. # Increase effective curvature and evaluate step size alpha.
@ -111,10 +113,10 @@ def SCG(f, gradf, x, optargs=(), maxiters=500, max_f_eval=np.inf, display=True,
fnew = f(xnew, *optargs) fnew = f(xnew, *optargs)
function_eval += 1 function_eval += 1
# if function_eval >= max_f_eval: if function_eval >= max_f_eval:
# status = "maximum number of function evaluations exceeded" status = "maximum number of function evaluations exceeded"
# break break
# return x, flog, function_eval, status return x, flog, function_eval, status
Delta = 2.*(fnew - fold) / (alpha * mu) Delta = 2.*(fnew - fold) / (alpha * mu)
if Delta >= 0.: if Delta >= 0.:
@ -156,6 +158,7 @@ def SCG(f, gradf, x, optargs=(), maxiters=500, max_f_eval=np.inf, display=True,
# Update variables for new position # Update variables for new position
gradold = gradnew gradold = gradnew
gradnew = gradf(x, *optargs) gradnew = gradf(x, *optargs)
function_eval += 1
current_grad = np.dot(gradnew, gradnew) current_grad = np.dot(gradnew, gradnew)
fold = fnew fold = fnew
# If the gradient is zero then we are done. # If the gradient is zero then we are done.