使用 WandB 而不是单独的图来获取每个时期的对齐 val_loss 和 train_loss 图

发布于 2025-01-13 18:14:54 字数 801 浏览 2 评论 0原文

我有以下代码,用于使用 WandB API 记录每个时期的训练和验证损失。但我不确定为什么我没有在同一时期得到 val 损失和训练损失。知道如何解决这个问题吗?

wandb.log({"train loss": train_epoch_loss,
           "val loss": val_epoch_loss,
           "epoch": epoch})

wandb.log({"train acc": train_epoch_acc,
           "val acc": val_epoch_acc,
           "epoch": epoch})

wandb.log({"best val acc": best_acc, "epoch": epoch})

和 train loss vs epochs 是两个完全独立的实体,而我希望将它们都放在 WandB 的一个图中。 输入图片此处描述

在此处输入图像描述

I have the following code for logging the train and val loss in each epoch using WandB API. I am not sure though why I am not getting val loss and train loss in the same epoch. Any idea how that could be fixed?

wandb.log({"train loss": train_epoch_loss,
           "val loss": val_epoch_loss,
           "epoch": epoch})

wandb.log({"train acc": train_epoch_acc,
           "val acc": val_epoch_acc,
           "epoch": epoch})

wandb.log({"best val acc": best_acc, "epoch": epoch})

enter image description here

As you see, val loss vs epochs and train loss vs epochs are two completely separate entities while I would like to have both of them in one plot in WandB.
enter image description here

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

新雨望断虹 2025-01-20 18:14:54

我在Weights & 工作。偏差,很乐意提供帮助:

同一图表上的 2 个指标

要在同一图表上绘制 2 个指标,您可以单击图表中的铅笔图标进行编辑,然后将其他指标添加到 y-轴,如下图所示。

输入图像描述这里

更改默认 x 轴

您还可以更改 X 轴以针对“epoch”而不是默认的 wandb step 进行绘图。如果您希望默认情况下实现此行为,您可以在开始训练之前调用 wandb.define_metric 一次,并将 x 轴设置为 epoch。请参阅define_metric文档了解更多

记录步骤

需要注意的一件事是,当您记录验证指标时,您希望它们与训练指标在同一步骤中记录。在这种情况下,您可以执行以下操作:

metrics = {}

for step, batch in my_data:
  ...
  
  train_loss = ....

  metrics['train_loss'] = train_loss

  if step % val_steps == 0:
    val_loss = ...
  
    metrics['val_loss'] = val_loss
  
    wandb.log(metrics)

  else:
    wandb.log(metrics)

或者,您可以使用 commit= wandb.log 中的 False 参数 用于存储指标而不增加 wandb 步骤。然后在需要时调用 wandb.log() 来增加步长。

I work at Weights & Biases, happy to help:

2 metrics on the same chart

To plot 2 metrics on the same chart you can click on the pencil icon in the chart to edit it, and then add additional metrics to the y-axis, as shown below.

enter image description here

Change default x-axis

You can also change the X axis to plot against "epoch" instead of the default wandb step. If you'd like this behaviour by default you can call wandb.define_metric once before you start training and set the x-axis to be epoch. See the define_metric docs for more

Logging step

One thing to be mindful of is that when you log your validation metrics, you'd like them to be logged at the same step as the train metrics. In this case you can do something like this:

metrics = {}

for step, batch in my_data:
  ...
  
  train_loss = ....

  metrics['train_loss'] = train_loss

  if step % val_steps == 0:
    val_loss = ...
  
    metrics['val_loss'] = val_loss
  
    wandb.log(metrics)

  else:
    wandb.log(metrics)

Alternatively, you can use the commit=False argument in wandb.log to store the metric without incrementing the wandb step. And then call wandb.log() to increment the step when you need to.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文