3 from __future__
import absolute_import
4 from __future__
import division
5 from __future__
import print_function
6 from __future__
import unicode_literals
15 from collections
import OrderedDict
16 from future.utils
import viewkeys, viewvalues
19 Utilities for logging experiment run stats, such as accuracy 20 and loss over time for different runs. Runtime arguments are stored 23 Optionally, ModelTrainerLog calls out to a logger to log to 24 an external log destination. 29 six.add_metaclass(abc.ABCMeta)
34 Set runtime arguments for the logger. 35 runtime_args: dict of runtime arguments. 37 raise NotImplementedError(
38 'Must define set_runtime_args function to use this base class' 42 def log(self, log_dict):
44 log a dict of key/values to an external destination 47 raise NotImplementedError(
48 'Must define log function to use this base class' 54 def __init__(self, expname, runtime_args, external_loggers=None):
55 now = datetime.datetime.fromtimestamp(time.time())
57 "{}_{}".format(expname, now.strftime(
'%Y%m%d_%H%M%S'))
59 self.
logstr(
"# %s" % str(runtime_args))
66 if external_loggers
is not None:
68 if not isinstance(runtime_args, dict):
69 runtime_args = dict(vars(runtime_args))
71 runtime_args[
'hostname'] = socket.gethostname()
73 logger.set_runtime_args(runtime_args)
77 def logstr(self, str):
81 logging.getLogger(
"experiment_logger").info(str)
83 def log(self, input_count, batch_count, additional_values):
84 logdict = OrderedDict()
90 logdict[
'time_spent'] = delta_t
91 logdict[
'cumulative_time_spent'] = time.time() - self.
start_time 92 logdict[
'input_count'] = delta_count
93 logdict[
'cumulative_input_count'] = input_count
94 logdict[
'cumulative_batch_count'] = batch_count
96 logdict[
'inputs_per_sec'] = delta_count / delta_t
98 logdict[
'inputs_per_sec'] = 0.0
100 for k
in sorted(viewkeys(additional_values)):
101 logdict[k] = additional_values[k]
105 self.
headers = list(viewkeys(logdict))
108 self.
logstr(
",".join(str(v)
for v
in viewvalues(logdict)))
113 except Exception
as e:
115 "Failed to call ExternalLogger: {}".format(e), e)
def set_runtime_args(self, runtime_args)