我在TensorFlow中使用不同的图形运行了几个训练课程。我设置的摘要在培训和验证中显示了有趣的结果。现在,我想将我保存的数据保存在摘要日志中并执行一些统计分析,并进行总体绘图并以不同方式查看摘要数据。有没有现成的方法可以轻松访问这些数据?
更具体地说,有没有内置的方法将TFEvent记录读回Python?
如果没有简单的方法,TensorFlow states that all its file formats are protobuf files。从我对protobufs(有限)的理解,我想如果我有TFEvent协议规范,我将能够提取这些数据。有没有一种简单的方法来获得这个?非常感谢你。
作为Fabrizio says,TensorBoard是一个很好的工具,用于可视化摘要日志的内容。但是,如果要执行自定义分析,可以使用tf.train.summary_iterator()
函数循环遍历日志中的所有tf.Event
和tf.Summary
协议缓冲区:
for summary in tf.train.summary_iterator("/path/to/log/file"):
# Perform custom processing in here.
要读取TFEvent,您可以获得一个产生Event协议缓冲区的Python迭代器。
# This example supposes that the events file contains summaries with a
# summary value tag 'loss'. These could have been added by calling
# `add_summary()`, passing the output of a scalar summary op created with
# with: `tf.scalar_summary(['loss'], loss_tensor)`.
for e in tf.train.summary_iterator(path_to_events_file):
for v in e.summary.value:
if v.tag == 'loss' or v.tag == 'accuracy':
print(v.simple_value)
更多信息:summary_iterator
你可以简单地使用:
tensorboard --inspect --event_file=myevents.out
或者如果要过滤图表事件的特定子集:
tensorboard --inspect --event_file=myevents.out --tag=loss
如果你想创造更多自定义的东西,你可以深入研究
/tensorflow/python/summary/event_file_inspector.py
了解如何解析事件文件。
您可以使用脚本serialize_tensorboard,它将接受logdir并以json格式写出所有数据。
您还可以使用EventAccumulator获得方便的Python API(这与TensorBoard使用的API相同)。
以下是从标量中获取值的完整示例。您可以看到事件protobuf消息here的消息规范
import tensorflow as tf
for event in tf.train.summary_iterator('runs/easy_name/events.out.tfevents.1521590363.DESKTOP-43A62TM'):
for value in event.summary.value:
print(value.tag)
if value.HasField('simple_value'):
print(value.simple_value)
我一直在用这个。它假定您只想看到您多次登录其标记为浮动的标记,并将结果作为pd.DataFrame
返回。只需致电metrics_df = parse_events_file(path)
。
from collections import defaultdict
import pandas as pd
import tensorflow as tf
def is_interesting_tag(tag):
if 'val' in tag or 'train' in tag:
return True
else:
return False
def parse_events_file(path: str) -> pd.DataFrame:
metrics = defaultdict(list)
for e in tf.train.summary_iterator(path):
for v in e.summary.value:
if isinstance(v.simple_value, float) and is_interesting_tag(v.tag):
metrics[v.tag].append(v.simple_value)
if v.tag == 'loss' or v.tag == 'accuracy':
print(v.simple_value)
metrics_df = pd.DataFrame({k: v for k,v in metrics.items() if len(v) > 1})
return metrics_df