def _define_step(self, name, batch_size, score, summary): """Combine operations of a phase. Keeps track of the mean score and when to report it. Args: name: Name of the phase used for the score summary. batch_size: Increment size of the global step. score: Tensor holding the current scores. summary: Tensor holding summary string to write if not an empty string. Returns: Tuple of summary tensor, mean score, and new global step. The mean score is zero for non reporting steps. """ with tf.compat.v1.variable_scope('phase_{}'.format(name)): score_mean = tools.StreamingMean((), tf.float32, 'score_mean') score.set_shape((None, )) with tf.control_dependencies([score, summary]): submit_score = score_mean.submit(score) with tf.control_dependencies([submit_score]): mean_score = tf.cond(self._report, score_mean.clear, float) summary = tf.cond( self._report, lambda: tf.compat.v1.summary.merge([ summary, tf.compat.v1.summary.scalar( name + '/score', mean_score, family='trainer') ]), lambda: summary) next_step = self._global_step.assign_add(batch_size) with tf.control_dependencies([summary, mean_score, next_step]): return (tf.identity(summary), tf.identity(mean_score), tf.identity(next_step))
def print_metrics(metrics, step, every): means, updates = [], [] for key, value in metrics: mean = tools.StreamingMean((), tf.float32, 'mean_{}'.format(key)) means.append(mean) updates.append(mean.submit(value)) with tf.control_dependencies(updates): message = 'step/' + '/'.join(key for key, _ in metrics) + ' = ' gs = tf.train.get_or_create_global_step() print_metrics = tf.cond( tf.equal(step % every, 0), lambda: tf.print(message, [gs] + [mean.clear() for mean in means]), tf.no_op) return print_metrics
def print_metrics(metrics, step, every): means, updates = [], [] for key, value in metrics: mean = tools.StreamingMean((), tf.float32, 'mean_{}'.format(key)) means.append(mean) updates.append(mean.submit(value)) with tf.control_dependencies(updates): # step/score/loss/zs_entropy/zs_divergence = [25000, -nan, 11413.0146, 37.0638504, 4.20143223] message = 'step/' + '/'.join(key for key, _ in metrics) + ' = ' gs = tf.train.get_or_create_global_step() print_metrics = tf.cond( tf.equal(step % every, 0), lambda: tf.print(message, [gs] + [mean.clear() for mean in means]), tf.no_op) return print_metrics
def print_metrics(metrics, step, every, name='metrics'): means, updates = [], [] for key, value in metrics.items(): key = 'metrics_{}_{}'.format(name, key) mean = tools.StreamingMean((), tf.float32, key) means.append(mean) updates.append(mean.submit(value)) with tf.control_dependencies(updates): # message = 'step/' + '/'.join(metrics.keys()) + ' = ' message = '{}: step/{} ='.format(name, '/'.join(metrics.keys())) gs = tf.train.get_or_create_global_step() print_metrics = tf.cond( tf.equal(step % every, 0), lambda: tf.print(message, [gs] + [mean.clear() for mean in means]), tf.no_op) return print_metrics