gol*_*enk 39 python tensorflow tensorboard
我在TensorFlow中使用不同的图形运行了几个训练课程.我设置的摘要在培训和验证中显示了有趣的结果.现在,我想将我保存的数据保存在摘要日志中并执行一些统计分析,并进行总体绘图并以不同方式查看摘要数据.有没有现成的方法可以轻松访问这些数据?
更具体地说,有没有内置的方法将TFEvent记录读回Python?
如果没有简单的方法,TensorFlow声明其所有文件格式都是protobuf文件.从我对protobufs(有限)的理解,我想如果我有TFEvent协议规范,我将能够提取这些数据.有没有一种简单的方法来获得这个?非常感谢你.
mrr*_*rry 37
正如Fabrizio 所说,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.
Run Code Online (Sandbox Code Playgroud)
Tem*_*mak 28
要读取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)
Run Code Online (Sandbox Code Playgroud)
更多信息:summary_iterator
fab*_*ioM 17
你可以简单地使用:
tensorboard --inspect --event_file=myevents.out
Run Code Online (Sandbox Code Playgroud)
或者如果要过滤图表的特定事件子集:
tensorboard --inspect --event_file=myevents.out --tag=loss
Run Code Online (Sandbox Code Playgroud)
如果你想创造更多自定义的东西,你可以深入研究
/tensorflow/python/summary/event_file_inspector.py
Run Code Online (Sandbox Code Playgroud)
了解如何解析事件文件.
这是一个用于从标量获取值的完整示例。您可以在此处查看 Event protobuf消息的消息规范
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)
Run Code Online (Sandbox Code Playgroud)
从tensorflow版本开始以下工作2.0.0-beta1
:
import os
import tensorflow as tf
from tensorflow.python.framework import tensor_util
summary_dir = 'tmp/summaries'
summary_writer = tf.summary.create_file_writer('tmp/summaries')
with summary_writer.as_default():
tf.summary.scalar('loss', 0.1, step=42)
tf.summary.scalar('loss', 0.2, step=43)
tf.summary.scalar('loss', 0.3, step=44)
tf.summary.scalar('loss', 0.4, step=45)
from tensorflow.core.util import event_pb2
from tensorflow.python.lib.io import tf_record
def my_summary_iterator(path):
for r in tf_record.tf_record_iterator(path):
yield event_pb2.Event.FromString(r)
for filename in os.listdir(summary_dir):
path = os.path.join(summary_dir, filename)
for event in my_summary_iterator(path):
for value in event.summary.value:
t = tensor_util.MakeNdarray(value.tensor)
print(value.tag, event.step, t, type(t))
Run Code Online (Sandbox Code Playgroud)
my_summary_iterator
从tensorflow.python.summary.summary_iterator.py
那里复制代码-无法在运行时导入它。
您可以使用脚本serialize_tensorboard,它将接受logdir并以json格式写出所有数据.
您还可以将EventAccumulator用于方便的Python API(这与TensorBoard使用的API相同).
有两种本机方法可以读取本文中提到的事件文件:
事件累加器
>>> from tensorboard.backend.event_processing.event_accumulator import EventAccumulator
>>> event_acc = EventAccumulator(event_file)
>>> event_acc.Reload()
<tensorboard.backend.event_processing.event_accumulator.EventAccumulator object at ...>
>>> print(event_acc.Tags())
{'images': [], 'audio': [], 'histograms': [], 'scalars': ['y=2x'], 'distributions': [], 'tensors': [], 'graph': False, 'meta_graph': False, 'run_metadata': []}
>>> for e in event_acc.Scalars('y=2x'):
... print(e.step, e.value)
0 0.0
1 2.0
2 4.0
3 6.0
4 8.0
Run Code Online (Sandbox Code Playgroud)
摘要迭代器
>>> import tensorflow as tf
>>> from tensorflow.python.summary.summary_iterator import summary_iterator
>>> for e in summary_iterator(event_file):
... for v in e.summary.value:
... if v.tag == 'y=2x':
... print(e.step, v.simple_value)
0 0.0
1 2.0
2 4.0
3 6.0
4 8.0
Run Code Online (Sandbox Code Playgroud)
对于多个事件文件或其他事件类型(例如直方图),您可以使用tbparse将事件日志解析为 pandas DataFrame 并在本地处理。如果您在解析过程中遇到任何问题,可以提出问题。(我是tbparse的作者)
注意:仅当您将事件日志上传到TensorBoard.dev(源)时,TensorBoard 才能将事件日志解析为 DataFrame,并且当前无法离线/本地使用它。