将TensorBoard 2中的2个地块与TensorFlow 2合并 [英] Merging 2 plots in TensorBoard 2 with TensorFlow 2

查看:378
本文介绍了将TensorBoard 2中的2个地块与TensorFlow 2合并的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Tensorflow和tensorboard V2在同一图上合并精度和召回率.我发现了许多先前版本的示例,但在我的情况下,这些示例都无法正常工作.

我创建了一个Keras回调来计算精度和调用率,然后调用一个tensorflow摘要以将它们记录在同一记录器中.我可以在Tensorboard中可视化它们,但是在2个单独的图中.

 Class ClassificationReport(Callback):
    def __init__(self, data_generator, steps, label_names, log_directory):
        """
        Instantiator
        :param data_generator: the data generator that produces the input data
        :param steps: int, batch size
        :param data_type, string, 'training', 'validation' or 'test', used a prefix in the logs
        :param log_directory: pathlib2 path to the TensorBoard log directory

        """

        self.data_generator = data_generator
        self.steps = steps
        self.data_type = data_type
        self.logger = tensorflow.summary.create_file_writer(str(log_directory / self.data_type))

        # names of the scalar to consider in the sklearn classification report
        self._scalar_names = ['precision', 'recall']

    def on_epoch_end(self, epoch, logs={}):
        """
        log the precision and recall

        :param epoch: int, number of epochs
        :param logs: the Keras dictionary where the metrics are stored
        """

        y_true = numpy.zeros(self.steps)
        y_predicted = numpy.zeros(self.steps)

       ...Here I fetch y_true and y_predicted with the data_generator

        # The current report is calculated by SciKit-Learn
        current_report = classification_report(y_true, y_predicted, output_dict=True)

        with self.logger.as_default():
            for scalar_name in self._scalar_names:
                tensorflow.summary.scalar(
                    name="{} / macro average / {}".format(self.data_type, scalar_name),
                    data=current_report['macro avg'][scalar_name],
                    step=epoch)

        return super().on_epoch_end(epoch, logs)
 

就我理解Tensorboard 2逻辑而言,似乎不可能在同一图上绘制2个标量汇总...在此阶段欢迎提出任何建议.

解决方案

使用两个具有相同标量摘要名称的不同编写器.

import numpy as np
import tensorflow as tf

logger1 = tf.summary.create_file_writer('logs/scalar/precision')
logger2 = tf.summary.create_file_writer('logs/scalar/recall')

precision = np.random.uniform(size=10)
recall = np.random.uniform(size=10)

for i in range(10):
    with logger1.as_default():
        tf.summary.scalar(name='precision-recall', data=precision[i], step=i)
    with logger2.as_default():
        tf.summary.scalar(name='precision-recall', data=recall[i], step=i)

tensorboard --logdir日志/标量

此答案适用于tf2: https://stackoverflow.com/a/38718948/5597718

I would like to merge on the same plot both the precision and recall using Tensorflow and tensorboard V2. I found many examples for the previous versions, but none of them is working in my case.

I have created a Keras callback that calculates the precision and recall, then I call a tensorflow summary to log them in the same logger. I can visualize them in Tensorboard, but in 2 separated plots.

Class ClassificationReport(Callback):
    def __init__(self, data_generator, steps, label_names, log_directory):
        """
        Instantiator
        :param data_generator: the data generator that produces the input data
        :param steps: int, batch size
        :param data_type, string, 'training', 'validation' or 'test', used a prefix in the logs
        :param log_directory: pathlib2 path to the TensorBoard log directory

        """

        self.data_generator = data_generator
        self.steps = steps
        self.data_type = data_type
        self.logger = tensorflow.summary.create_file_writer(str(log_directory / self.data_type))

        # names of the scalar to consider in the sklearn classification report
        self._scalar_names = ['precision', 'recall']

    def on_epoch_end(self, epoch, logs={}):
        """
        log the precision and recall

        :param epoch: int, number of epochs
        :param logs: the Keras dictionary where the metrics are stored
        """

        y_true = numpy.zeros(self.steps)
        y_predicted = numpy.zeros(self.steps)

       ...Here I fetch y_true and y_predicted with the data_generator

        # The current report is calculated by SciKit-Learn
        current_report = classification_report(y_true, y_predicted, output_dict=True)

        with self.logger.as_default():
            for scalar_name in self._scalar_names:
                tensorflow.summary.scalar(
                    name="{} / macro average / {}".format(self.data_type, scalar_name),
                    data=current_report['macro avg'][scalar_name],
                    step=epoch)

        return super().on_epoch_end(epoch, logs)

As far as I understant the Tensorboard 2 logic, it doesn't seem to be possible to plot 2 scalar summaries on the same plot... Any advice is welcomed at this stage.

解决方案

Use two different writers with the same scalar summary name.

import numpy as np
import tensorflow as tf

logger1 = tf.summary.create_file_writer('logs/scalar/precision')
logger2 = tf.summary.create_file_writer('logs/scalar/recall')

precision = np.random.uniform(size=10)
recall = np.random.uniform(size=10)

for i in range(10):
    with logger1.as_default():
        tf.summary.scalar(name='precision-recall', data=precision[i], step=i)
    with logger2.as_default():
        tf.summary.scalar(name='precision-recall', data=recall[i], step=i)

tensorboard --logdir logs/scalar

From this answer, adapted for tf2: https://stackoverflow.com/a/38718948/5597718

这篇关于将TensorBoard 2中的2个地块与TensorFlow 2合并的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆