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

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

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

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中)?

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

python tensorflow machine-learning edit tensorboard
1个回答
0
投票

How to load selected range of samples in Tensorboard中所述,TensorBoard事件实际上是存储的记录文件,因此您可以读取它们并对其进行处理。这是一个与此处发布的脚本相似的脚本,但用于重命名事件,并已更新为可在TF 2.x中使用。

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# rename_events.py

import sys
from pathlib import Path
import os
# Use this if you want to avoid using the GPU
os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
import tensorflow as tf
from tensorflow.core.util.event_pb2 import Event

def rename_events(input_path, output_path, old_tags, new_tag):
    # Make a record writer
    with tf.io.TFRecordWriter(str(output_path)) as writer:
        # Iterate event records
        for rec in tf.data.TFRecordDataset([str(input_path)]):
            # Read event
            ev = Event()
            ev.MergeFromString(rec.numpy())
            # Check if it is a summary
            if ev.summary:
                # Iterate summary values
                for v in ev.summary.value:
                    # Check if the tag should be renamed
                    if v.tag in old_tags:
                        # Rename with new tag name
                        v.tag = new_tag
        writer.write(ev.SerializeToString())

def rename_events_dir(input_dir, output_dir, old_tags, new_tag):
    input_dir = Path(input_dir)
    output_dir = Path(output_dir)
    # Make output directory
    output_dir.mkdir(parents=True, exist_ok=True)
    # Iterate event files
    for ev_file in input_dir.glob('**/*.tfevents*'):
        # Make directory for output event file
        out_file = Path(output_dir, ev_file.relative_to(input_dir))
        out_file.parent.mkdir(parents=True, exist_ok=True)
        # Write renamed events
        rename_events(ev_file, out_file, old_tags, new_tag)

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')

您将这样使用它:

> python rename_events.py my_log_dir renamed_log_dir "Training Loss;Validation Loss" loss
© www.soinside.com 2019 - 2024. All rights reserved.