gpt4 book ai didi

tensorflow - 将 TensorBoard 2 中的 2 个图与 TensorFlow 2 合并

转载 作者:行者123 更新时间:2023-12-02 03:09:03 26 4
gpt4 key购买 nike

我想使用 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 日志/标量

enter image description here

根据这个答案,改编为 tf2:https://stackoverflow.com/a/38718948/5597718

关于tensorflow - 将 TensorBoard 2 中的 2 个图与 TensorFlow 2 合并,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58181527/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com