TensorFlow摘要将标量作为示例以张量形式写入事件日志

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

TensorFlow版本= 2.0.0

我在https://www.tensorflow.org/api_docs/python/tf/summary处遵循如何使用TensorFlow摘要模块的示例;页面上的第一个,为了完整起见,我将在下面粘贴:

writer = tf.summary.create_file_writer("/tmp/mylogs")
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()

运行此程序很好,我得到了可以在TensorBoard中查看的事件日志。大!但是,当我使用以下命令查看事件日志时:

tensorboard --inspect --logdir=tmp/mylogs

它告诉我,由于某种原因,我的摘要变量已作为张量而不是标量写入了日志:

Event statistics for tmp/mylogs:
audio -
graph -
histograms -
images -
scalars -
sessionlog:checkpoint -
sessionlog:start -
sessionlog:stop -
tensor
   first_step           0
   last_step            99
   max_step             99
   min_step             0
   num_steps            100
   outoforder_steps     [(99, 0)]

我想这可能不是问题,除了当我尝试按照以下方法从事件日志中读取时: https://stackoverflow.com/a/45899735/1447953

from tensorboard.backend.event_processing.event_accumulator import EventAccumulator
x = EventAccumulator(path="tmp/mylogs")
x.Reload()
print(x.Tags())

然后再次告诉我my_metric是张量:

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

并且当我尝试查看数据时显得很乱

w_times, step_nums, vals = zip(*x.Tensors('my_metric'))
print("vals:", vals)

vals: (dtype: DT_FLOAT
tensor_shape {
}
tensor_content: "\000\000\000?"
, dtype: DT_FLOAT
tensor_shape {
}
tensor_content: "\000\000\000?"
, dtype: DT_FLOAT
tensor_shape {
}
...
etc.            

我在这里做错什么了吗?这个例子看起来很简单,所以我不确定可能是什么问题。我只是复制/粘贴了它。还是他们决定始终将数据粘贴在“ Tensor”标签下,并且有某种方法可以将值转换回标准绘图工具中可用的值?

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

我仍然不知道为什么编写标量摘要数据会导致我张量事件,但是我至少知道如何解码它们。以下内容基于https://stackoverflow.com/a/55788491/1447953的答案,针对TensorFlow 2进行了稍微更新:

import tensorflow as tf
def decode(val):
    tensor_bytes = val.tensor_content
    tensor_dtype = val.dtype
    tensor_shape = [x.size for x in val.tensor_shape.dim]
    tensor_array = tf.io.decode_raw(tensor_bytes, tensor_dtype)
    tensor_array = tf.reshape(tensor_array, tensor_shape)
    return tensor_array

print([decode(v) for v in vals])
print([decode(v).numpy() for v in vals])

输出:

[<tf.Tensor: id=3, shape=(), dtype=float32, numpy=0.5>, <tf.Tensor: id=7, shape=(), dtype=float32, numpy=0.5>, <tf.Tensor: id=11, shape=(), dtype=float32, numpy=0.5>, <tf.Tensor: id=15, shape=(), dtype=float32, numpy=0.5>, <tf.Tensor: id=19, shape=(), dtype=float32, numpy=0.5>, <tf.Tensor: id=23, shape=(), dtype=float32, numpy=0.5>, <tf.Tensor: id=27, shape=(), dtype=float32, numpy=0.5>, <tf.Tensor: id=31, shape=(), dtype=float32, numpy=0.5>, <tf.Tensor: id=35, shape=(), dtype=float32, numpy=0.5>, <tf.Tensor: id=39, shape=(), dtype=float32, numpy=0.5>]
[0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5]

不过,这还不是完整的故事,因为这只会给我10个事件,而我希望应该记录100个事件。但是我想这是原始记录如何发生的问题,因为我得到的step_nums是:

(3, 20, 24, 32, 53, 41, 58, 70, 78, 99)

所以我猜只有那些迭代被写入磁盘。但为什么?我没有在文档中看到任何有关选择性书写自动发生的信息。

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