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
18 Utilities for logging experiment run stats, such as accuracy 19 and loss over time for different runs. Runtime arguments are stored 22 Optionally, ModelTrainerLog calls out to an logger to log to 23 an external log destination. 28 six.add_metaclass(abc.ABCMeta)
33 Set runtime arguments for the logger. 34 runtime_args: dict of runtime arguments. 36 raise NotImplementedError(
37 'Must define set_runtime_args function to use this base class' 41 def log(self, log_dict):
43 log a dict of key/values to an external destination 46 raise NotImplementedError(
47 'Must define log function to use this base class' 53 def __init__(self, expname, runtime_args, external_loggers=None):
54 now = datetime.datetime.fromtimestamp(time.time())
56 "{}_{}".format(expname, now.strftime(
'%Y%m%d_%H%M%S'))
58 self.
logstr(
"# %s" % str(runtime_args))
65 if external_loggers
is not None:
67 runtime_args = dict(vars(runtime_args))
69 runtime_args[
'hostname'] = socket.gethostname()
71 logger.set_runtime_args(runtime_args)
75 def logstr(self, str):
79 logging.getLogger(
"experiment_logger").info(str)
81 def log(self, input_count, batch_count, additional_values):
82 logdict = OrderedDict()
88 logdict[
'time_spent'] = delta_t
89 logdict[
'cumulative_time_spent'] = time.time() - self.
start_time 90 logdict[
'input_count'] = delta_count
91 logdict[
'cumulative_input_count'] = input_count
92 logdict[
'cumulative_batch_count'] = batch_count
94 logdict[
'inputs_per_sec'] = delta_count / delta_t
96 logdict[
'inputs_per_sec'] = 0.0
98 for k
in sorted(additional_values.keys()):
99 logdict[k] = additional_values[k]
103 self.
headers = logdict.keys()[:]
106 self.
logstr(
",".join([str(v)
for v
in logdict.values()]))
111 except Exception
as e:
113 "Failed to call ExternalLogger: {}".format(e), e)
def set_runtime_args(self, runtime_args)