MetricLogger

class tfsnippet.MetricLogger(summary_writer=None, summary_metric_prefix='', summary_skip_pattern=None, summary_commit_freqs=None, formatter=None)

Bases: object

Logger for the training metrics.

This class provides convenient methods for logging training metrics, and for writing metrics onto disk via TensorFlow summary writer. The statistics of the metrics could be formatted into human readable strings via format_logs().

An example of using this logger is:

logger = MetricLogger(tf.summary.FileWriter(log_dir))
global_step = 1

for epoch in range(1, max_epoch+1):
    for batch in DataFlow.arrays(...):
        loss, _ = session.run([loss, train_op], ...)
        logger.collect_metrics({'loss': loss}, global_step)
        global_step += 1

    valid_loss = session.run([loss], ...)
    logger.collect_metrics({'valid_loss': valid_loss}, global_step)
    print('Epoch {}, step {}: {}'.format(
        epoch, global_step, logger.format_logs()))
    logger.clear()

Attributes Summary

metrics Get the dict of metric collectors.

Methods Summary

clear() Clear all the metric statistics.
collect_metrics(metrics[, global_step]) Collect the statistics of metrics.
format_logs() Format the metric statistics as human readable strings.

Attributes Documentation

metrics

Get the dict of metric collectors.

Returns:The metric collectors.
Return type:dict[str, StatisticsCollector]

Methods Documentation

clear()

Clear all the metric statistics.

collect_metrics(metrics, global_step=None)

Collect the statistics of metrics.

Parameters:
  • metrics (dict[str, float or np.ndarray or ScheduledVariable]) – Dict from metrics names to their values. For format_logs(), there is no difference between calling collect_metrics() only once, with an array of metric values; or calling collect_metrics() multiple times, with one value at each time. However, for the TensorFlow summary writer, only the mean of the metric values would be recorded, if calling collect_metrics() with an array.
  • global_step (int or tf.Variable or tf.Tensor) – The global step counter. (optional)
format_logs()

Format the metric statistics as human readable strings.

Returns:The formatted metric statistics.
Return type:str