如何从tensorflow 2摘要写入器中读取数据?

问题描述 投票:1回答:1

我在读取tensorflow摘要写入器中的数据时遇到了问题。

我使用的是tensorflow网站上的例子中的写入器。https:/www.tensorflow.orgtensorboardmigrate

import tensorflow as tf
from tensorboard.backend.event_processing.event_accumulator import EventAccumulator

writer = tf.summary.create_file_writer("/tmp/mylogs/eager")

# write to summary writer
with writer.as_default():
  for step in range(100):
    # other model code would go here
    tf.summary.scalar("my_metric", 0.5, step=step)
    writer.flush()

# read from summary writer
event_acc = EventAccumulator("/tmp/mylogs/eager")
event_acc.Reload()
event_acc.Tags()

产生。

 'distributions': [],
 'graph': False,
 'histograms': [],
 'images': [],
 'meta_graph': False,
 'run_metadata': [],
 'scalars': [],
 'tensors': ['my_metric']}```

如果我试图抓取张量数据,

import pandas as pd
pd.DataFrame(event_acc.Tensors('my_metric'))

我没有得到正确的值。

wall_time   step    tensor_proto
0   1.590743e+09    3   dtype: DT_FLOAT\ntensor_shape {\n}\ntensor_con...
1   1.590743e+09    20  dtype: DT_FLOAT\ntensor_shape {\n}\ntensor_con...
2   1.590743e+09    24  dtype: DT_FLOAT\ntensor_shape {\n}\ntensor_con...
3   1.590743e+09    32  dtype: DT_FLOAT\ntensor_shape {\n}\ntensor_con...
...

我如何抓取实际的摘要数据(应该是0.5,每100步)?

这里是一个colab笔记本,上面有代码。https:/colab.research.google.comdrive1RlgZrGD_vY-YcOBLF_sEPelmtVuygkqz?usp=分享。

python tensorflow tensorflow2.0 tensorboard
1个回答
1
投票

你需要转换事件累加器中的张量值,存储为 TensorProto 消息,将其转化为数组,你可以使用 tf.make_ndarray:

pd.DataFrame([(w, s, tf.make_ndarray(t)) for w, s, t in event_acc.Tensors('my_metric')],
             columns=['wall_time', 'step', 'tensor'])

1
投票

为了避免分步骤,我建议:

event_acc = EventAccumulator("/tmp/mylogs/eager", size_guidance={'tensors': 0})

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.