如何防止PyTorch Lightning中的TensorBoard记录当前epoch?

5
在PyTorch Lightning中创建新的TensorBoard记录器时,默认记录的两个内容是当前epoch和hp_metric。我可以通过设置default_hp_metric=False来禁用hp_metric日志记录,但我找不到任何方法来禁用epoch日志记录。我已经在lightning.py、trainer.py和tensorboard.py文件中搜索了这个模块、训练器和TensorBoard记录器的代码,但无法找到任何关于epoch的日志记录调用。
即使使用了PyTorch Lightning教程中的最简示例,这种行为仍然会发生。
有没有办法禁用这种epoch日志记录,以防止TensorBoard界面上出现混乱?

Tensorboard epoch logging

1个回答

2

简要概述

您可以通过覆盖TensorBoard记录器来禁用自动写入epoch变量。

from pytorch_lightning import loggers
from pytorch_lightning.utilities import rank_zero_only

class TBLogger(loggers.TensorBoardLogger):
    @rank_zero_only
    def log_metrics(self, metrics, step):
        metrics.pop('epoch', None)
        return super().log_metrics(metrics, step)

完整版本

  • Pytorch lightning automatically add epoch vs global_step graph to each logger. (you can see description in here)
  • There is no option to turn this behavior off. Because this is hard coded without any condition like below: (see full source code in here)
    if step is None:
        # added metrics for convenience
        scalar_metrics.setdefault("epoch", self.trainer.current_epoch)
        step = self.trainer.global_step
    
    # log actual metrics
    self.trainer.logger.agg_and_log_metrics(scalar_metrics, step=step)
    
  • To disable this option, you should pop epoch variable from metric dictionary in log_metrics(metrics, step) that is called in add_and_log_metrics(scalar_metrics, step=step). Code is shown in above. You can see full long version snippet in here.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接