gpt4 book ai didi

python - 如何在同一张图中绘制 Tensorboard 中的多个标量而不向实验列表发送垃圾邮件?

转载 作者:行者123 更新时间:2023-12-05 04:24:54 26 4
gpt4 key购买 nike

This is not an answer, just a workaround .

This too, is not an answer ,(从那里拍摄的图像)。

我正在 pytorch-lightning 中寻找代码的答案。

也可以表述为:如何使用 Pytorch 闪电在 Tensorboard 中的同一张图表上绘制训练和验证损失,而不向 Tensorboard 发送垃圾邮件?


我想创建这样的图表

enter image description here

但不会造成这样的垃圾邮件

enter image description here

我能找到的是 this answer ,它只解释了如何绘制这样一个带有垃圾邮件的多标量图,或者在拆分图表时避免垃圾邮件。

我怎样才能将多个标量放入单个图中?

代码(使用pytorch lightning):

tb = self.logger.experiment  # This is a SummaryWriter

label_ind_by_names = {
"A": 0,
"B": 1,
"C": 2,
"D": 3,
"E": 4,
"F": 5,
"G": 6,
"H": 7,
"I": 8,
"J": 9,
}

computed_confusion = np.random.randint(low=0, high=100, size=(10, 10))
per_class_accuracy = computed_confusion.diagonal()/(computed_confusion.sum(1) + np.finfo(float).eps)

drs = {names: per_class_accuracy[label_ind_by_names[names]] for names in label_ind_by_names.keys() if any("is_damaged_True" in n for n in names)}
fas = {names: 1.0 - per_class_accuracy[label_ind_by_names[names]] for names in label_ind_by_names.keys() if any("is_damaged_False" in n for n in names)}

单独图表的代码:

for names, dr in drs.items():
tb.add_scalar(f"dr/{str(names)}", dr, self.current_epoch)
for names, fa in fas.items():
tb.add_scalar(f"fa/{str(names)}", fa, self.current_epoch)

以及用于打乱绘图列表的联合图

tb.add_scalars(
"DR", {
str(k): v for k, v in drs.items()
},
self.current_epoch
)
tb.add_scalars(
"FA", {
str(k): v for k, v in fas.items()
},
self.current_epoch
)

最佳答案

我终于找到了足够的答案here .这是 doc .

这是对 pytorch-lightning 的改编:

def on_fit_start(self):
tb = self.logger.experiment # noqa

layout_scalar_names_losses = [r"train_loss_epoch", "val_loss_epoch"]
layout = {
"metrics": {
f"loss_epoch": ["Multiline", layout_scalar_names_losses],
}
}

tb.add_custom_scalars(layout)

def _common_log(self, loss, stage: str):
assert stage in ("train", "val", "test")
self.log(f"{stage}_loss", loss, on_step=True, on_epoch=True)

def training_step(self, batch, batch_nb):
stage = "train"
augmented_image, outputs, labels, loss = self._common_step(batch, batch_nb, stage=stage)
self._common_log(loss, stage=stage)
return {"loss": loss, "outputs": outputs, "labels": labels}

def validation_step(self, batch, batch_nb):
stage = "val"
augmented_images, outputs, labels, loss = self._common_step(batch, batch_nb, stage=stage)
self._common_log(loss, stage=stage)

return {"loss": loss, "outputs": outputs, "labels": labels}

def _common_step(self, batch, batch_nb, stage: str):
assert stage in ("train", "val", "test")
augmented_image, labels = batch
outputs, aux_outputs = self(augmented_image)
loss = self._criterion(outputs, labels)

return augmented_image, outputs, labels, loss

这在 Tensorboard 的“自定义标量”选项卡下显示了以下内容。

enter image description here

enter image description here

除了损失之外,我仍然无法对标量执行此操作,但我会更新此答案,因为我确信这是要走的路。

关于python - 如何在同一张图中绘制 Tensorboard 中的多个标量而不向实验列表发送垃圾邮件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73399268/

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