使用 python 从 tensorflow 日志中手动读取最后一个值

Manually read last values from tensorflow logs with python

在 python 中,我正在记录张量流标量值:

import tensorflow as tf
...
self.writer = tf.summary.FileWriter(log_dir)
...
summary = tf.Summary(
  value=[
    tf.Summary.Value(
      tag=tag,
      simple_value=value
    )
  ]
)
self.writer.add_summary(summary, step)
self.writer.flush()

我希望能够从上次中断的地方继续我的记录。

我现在如何导入日志文件并读出最后一个值及其索引?

类似的东西可能会起作用:

def get_latest_value(log_file, tag_name):
    latest_summ = None
    latest_value = None

    for summary in tf.train.summary_iterator(log_file):
        if latest_summ is None or summary.step > latest_summ.step:
            latest_summ = summary
    for v in latest_summ.summary.value:
        if v.tag == tag_name:
            return v.simple_value

get_latest_value("./log/events.out.tfevents.1554114440.me", "train_error")