猿问

你如何编辑现有的 Tensorboard Training Loss 摘要?

我已经训练了我的网络并生成了一些训练/验证损失,我通过以下代码示例保存了这些损失(仅训练损失示例,验证完全等效):


valid_summary_writer = tf.summary.create_file_writer("/path/to/logs/")

with train_summary_writer.as_default():

    tf.summary.scalar('Training Loss', data=epoch_loss, step=current_step)

训练后,我想使用 Tensorboard 查看损失曲线。但是,因为我将损失曲线保存在名称“Training Loss”和“Validation Loss”下,所以这些曲线绘制在单独的图表上。我知道我应该将名称更改为简单的“损失”以解决此问题,以便将来写入日志目录。但是如何编辑我现有的训练/验证损失日志文件来解决这个问题?


我试图修改以下帖子的解决方案:https://stackoverflow.com/a/55061404编辑日志文件的步骤并重新写入文件;我的版本涉及更改文件中的标签。但我在这方面没有成功。它还需要通过 'tf.compat.v1' 导入较旧的 Tensorflow 代码。有没有办法实现这一点(可能在 TF 2.X 中)?


我曾想过简单地从包含损失的每个日志目录中获取损失和步长值,并通过我以前的工作方法将它们写入新的日志文件,但我只设法获得了步长,而不是损失值本身。有人在这里取得过成功吗?


---=== 编辑 ===---


我设法使用来自@jhedesa 的代码解决了这个问题


我不得不稍微改变调用函数“rename_events_dir”的方式,因为我在 Google Colab Notebook 中协同使用 Tensorflow。为此,我更改了代码的最后一部分:


if __name__ == '__main__':

    if len(sys.argv) != 5:

        print(f'{sys.argv[0]} <input dir> <output dir> <old tags> <new tag>',

              file=sys.stderr)

        sys.exit(1)

    input_dir, output_dir, old_tags, new_tag = sys.argv[1:]

    old_tags = old_tags.split(';')

    rename_events_dir(input_dir, output_dir, old_tags, new_tag)

    print('Done')

要阅读此内容:


rootpath = '/path/to/model/'

dirlist = [dirname for dirname in os.listdir(rootpath) if dirname not in ['train', 'valid']]

for dirname in dirlist:

  rename_events_dir(rootpath + dirname + '/train', rootpath + '/train', 'Training Loss', 'loss')

  rename_events_dir(rootpath + dirname + '/valid', rootpath + '/valid', 'Validation Loss', 'loss')

请注意,我调用了两次“rename_events_dir”,一次用于编辑训练损失的标签,一次用于验证损失标签。我可以通过设置“old_tags = 'Training Loss;Validation Loss'”并使用“old_tags = old_tags.split(';')”来拆分标签来使用之前调用代码的方法。我使用我的方法只是为了理解代码以及它如何处理数据。


哈士奇WWW
浏览 141回答 1
1回答

忽然笑

正如如何在 Tensorboard 中加载选定范围的样本中所述,TensorBoard 事件实际上是存储的记录文件,因此您可以读取它们并按原样处理它们。这是一个与那里发布的脚本类似的脚本,但用于重命名事件,并更新为在 TF 2.x 中工作。#!/usr/bin/env python3# -*- coding: utf-8 -*-# rename_events.pyimport sysfrom pathlib import Pathimport os# Use this if you want to avoid using the GPUos.environ['CUDA_VISIBLE_DEVICES'] = '-1'import tensorflow as tffrom tensorflow.core.util.event_pb2 import Eventdef rename_events(input_path, output_path, old_tags, new_tag):&nbsp; &nbsp; # Make a record writer&nbsp; &nbsp; with tf.io.TFRecordWriter(str(output_path)) as writer:&nbsp; &nbsp; &nbsp; &nbsp; # Iterate event records&nbsp; &nbsp; &nbsp; &nbsp; for rec in tf.data.TFRecordDataset([str(input_path)]):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # Read event&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ev = Event()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ev.MergeFromString(rec.numpy())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # Check if it is a summary&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ev.summary:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # Iterate summary values&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for v in ev.summary.value:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # Check if the tag should be renamed&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if v.tag in old_tags:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # Rename with new tag name&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; v.tag = new_tag&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.write(ev.SerializeToString())def rename_events_dir(input_dir, output_dir, old_tags, new_tag):&nbsp; &nbsp; input_dir = Path(input_dir)&nbsp; &nbsp; output_dir = Path(output_dir)&nbsp; &nbsp; # Make output directory&nbsp; &nbsp; output_dir.mkdir(parents=True, exist_ok=True)&nbsp; &nbsp; # Iterate event files&nbsp; &nbsp; for ev_file in input_dir.glob('**/*.tfevents*'):&nbsp; &nbsp; &nbsp; &nbsp; # Make directory for output event file&nbsp; &nbsp; &nbsp; &nbsp; out_file = Path(output_dir, ev_file.relative_to(input_dir))&nbsp; &nbsp; &nbsp; &nbsp; out_file.parent.mkdir(parents=True, exist_ok=True)&nbsp; &nbsp; &nbsp; &nbsp; # Write renamed events&nbsp; &nbsp; &nbsp; &nbsp; rename_events(ev_file, out_file, old_tags, new_tag)if __name__ == '__main__':&nbsp; &nbsp; if len(sys.argv) != 5:&nbsp; &nbsp; &nbsp; &nbsp; print(f'{sys.argv[0]} <input dir> <output dir> <old tags> <new tag>',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; file=sys.stderr)&nbsp; &nbsp; &nbsp; &nbsp; sys.exit(1)&nbsp; &nbsp; input_dir, output_dir, old_tags, new_tag = sys.argv[1:]&nbsp; &nbsp; old_tags = old_tags.split(';')&nbsp; &nbsp; rename_events_dir(input_dir, output_dir, old_tags, new_tag)&nbsp; &nbsp; print('Done')你会像这样使用它:> python rename_events.py my_log_dir renamed_log_dir "Training Loss;Validation Loss" loss
随时随地看视频慕课网APP

相关分类

Python
我要回答