[opt messages] show dd/hh:mm:ss.ms

This commit is contained in:
Max Zwiessele 2015-05-14 11:21:14 +01:00
parent 1629662678
commit 466b381443

View file

@ -5,9 +5,10 @@ from __future__ import print_function
import numpy as np import numpy as np
import sys import sys
import time import time
import datetime
def exponents(fnow, current_grad): def exponents(fnow, current_grad):
exps = [np.abs(np.float(fnow)), current_grad] exps = [np.abs(np.float(fnow)), 1 if current_grad is np.nan else current_grad]
return np.sign(exps) * np.log10(exps).astype(int) return np.sign(exps) * np.log10(exps).astype(int)
class VerboseOptimization(object): class VerboseOptimization(object):
@ -23,6 +24,7 @@ class VerboseOptimization(object):
self.model.add_observer(self, self.print_status) self.model.add_observer(self, self.print_status)
self.status = 'running' self.status = 'running'
self.clear = clear_after_finish self.clear = clear_after_finish
self.deltat = .2
self.update() self.update()
@ -74,16 +76,29 @@ 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:7s} {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|", "runtime", mi=self.len_maxiters))
def __enter__(self): def __enter__(self):
self.start = time.time() self.start = time.time()
return self return self
def print_out(self): def print_out(self, seconds):
if seconds<60:
self.timestring = "{:0>5.2f}".format(seconds)
else:
m, s = divmod(seconds, 60)
if m>59:
h, m = divmod(m, 60)
if h>23:
d, h = divmod(h, 24)
self.timestring = '{d:0>2d}/{h:0>2d}:{m:0>2d}'.format(m=int(m), h=int(h), d=int(d))
else:
self.timestring = '{h:0>2d}:{m:0>2d}:{s:0>2d}'.format(m=int(m), s=int(s), h=int(h))
else:
self.timestring = '{m:0>2d}:{s:0>5.2f}'.format(m=int(m), s=s)
if self.ipython_notebook: if self.ipython_notebook:
names_vals = [['optimizer', "{:s}".format(self.opt_name)], names_vals = [['optimizer', "{:s}".format(self.opt_name)],
['runtime [s]', "{:> g}".format(time.time()-self.start)], ['runtime', "{:>s}".format(self.timestring)],
['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))],
@ -120,14 +135,18 @@ class VerboseOptimization(object):
if b: if b:
self.exps = n_exps self.exps = n_exps
print('\r', end=' ') print('\r', end=' ')
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), end=' ') # print 'Iteration:', iteration, ' Objective:', fnow, ' Scale:', beta, '\r', print('{3:} {0:>0{mi}g} {1:> 12e} {2:> 12e}'.format(self.iteration, float(self.fnow), float(self.current_gradient), "{:>8s}".format(self.timestring), mi=self.len_maxiters), end=' ') # 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):
self.update() self.update()
seconds = time.time()-self.start
#sys.stdout.write(" "*len(self.message)) #sys.stdout.write(" "*len(self.message))
self.print_out() self.deltat += seconds
if self.deltat > .2:
self.print_out(seconds)
self.deltat = 0
self.iteration += 1 self.iteration += 1
@ -153,11 +172,11 @@ class VerboseOptimization(object):
if self.verbose: if self.verbose:
self.stop = time.time() self.stop = time.time()
self.model.remove_observer(self) self.model.remove_observer(self)
self.print_out() self.print_out(self.stop - self.start)
if not self.ipython_notebook: if not self.ipython_notebook:
print() print()
print('Optimization finished in {0:.5g} Seconds'.format(self.stop-self.start)) print('Runtime: {}'.format("{:>9s}".format(self.timestring)))
print('Optimization status: {0}'.format(self.status)) print('Optimization status: {0}'.format(self.status))
print() print()
elif self.clear: elif self.clear: