我使用以下代码读取 TensorFlow 生成的事件文件的内容并显示标量数据。该代码能够正确列出所有标签,但不显示这些标签的内容。
import os
import tensorflow as tf
from tensorboard.backend.event_processing.event_accumulator import EventAccumulator
def process_event_file(event_file):
print(f"Processing event file: {event_file}")
event_acc = EventAccumulator(event_file)
event_acc.Reload()
print(event_acc.Tags())
scalar_tags = event_acc.Tags()['scalars']
for tag in scalar_tags:
scalar_events = event_acc.Scalars(tag)
print(f"\nData for tag '{tag}':")
for event in scalar_events:
print(f"Step {event.step}, Wall time {event.wall_time}: {event.value}")
def find_and_process_event_files(directory):
for root, _, files in os.walk(directory):
for file in files:
if file.startswith("events.out.tfevents"):
event_file_path = os.path.join(root, file)
process_event_file(event_file_path)
print("\n" + "=" * 80 + "\n")
find_and_process_event_files("/content/drive/MyDrive/logs")
但是,当我在浏览器中运行 TensorBoard 时,所有图表都正确绘制。为什么即使 TensorBoard 正确显示图形,代码也无法打印标量数据的内容?我缺少什么,或者如何解决这个问题?
更新:打印输出看起来
Processing event file: /content/drive/some-folder/logs/2023-04-13 13:52:36.450081/tensorboard/trainer/UNIQUE_ID_HERE/events.out.tfevents.xxxxxx.localhost.localdomain.xxxx.x.v2
{'images': [], 'audio': [], 'histograms': [], 'scalars': [], 'distributions': [], 'tensors': ........}
我也遇到了同样的问题,对于 TensorFlow 2.x 编写的摘要,标量不再位于“scalars”键下,而是位于“tensors”键下(您可以在 Reload( 之后,使用调试器检查 EventAccumulator 对象来看到这一点) )).
您可以通过 .Tensors() 方法检索数据,然后将其传递给 tf.make_ndarray() 以获取标量值。
请参阅 nfelt 的评论:https://github.com/tensorflow/tensorboard/issues/5395
我也遇到过类似的问题。下面的代码可能有效,你可以尝试一下
import os
from tensorboard.backend.event_processing.event_accumulator import EventAccumulator
def process_event_file(event_file):
print(f"Processing event file: {event_file}")
event_acc = EventAccumulator(event_file)
event_acc.Reload()
print(event_acc.tensors.Keys())
tensor_tags = event_acc.tensors.Keys()
for tag in tensor_tags:
events = event_acc.tensors.Items(tag)
print(f"\nData for tag '{tag}':")
for event in events:
print(f"Step {event.step}, Wall time {event.wall_time}: {event.tensor_proto}")
def find_and_process_event_files(directory):
for root, _, files in os.walk(directory):
for file in files:
if file.startswith("events.out.tfevents"):
event_file_path = os.path.join(root, file)
process_event_file(event_file_path)
print("\n" + "=" * 80 + "\n")
find_and_process_event_files("/content/drive/MyDrive/logs")