当前位置:   article > 正文

PyTorch使用tensorboard的SummaryWriter报错

PyTorch使用tensorboard的SummaryWriter报错

PyTorch使用tensorboard可以显示网络运行情况,但偶尔使用SummaryWriter时遇到Segmentation fault错误。
利用python3的faulthandler,可定位到出错的代码行,具体操作有两种方式如下:
(1) 在代码中写入faulthandler

import faulthandler
# 在import之后直接添加以下启用代码即可
faulthandler.enable()
# 后边正常写你的代码

  • 1
  • 2
  • 3
  • 4
  • 5

(2)直接通过命令行来启用,运行时添加-X faulthandler参数即可:

python -X faulthandler your_script.py
  • 1

错误代码:

import xxx
from torch.utils.tensorboard import SummaryWriter
import yyy

log_dir = './run_logs'
self.writer = SummaryWriter(log_dir)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

报错如下:

 File "/opt/conda/lib/python3.7/site-packages/tensorboard/compat/__init__.py", line 45 in tf
  File "/opt/conda/lib/python3.7/site-packages/tensorboard/lazy.py", line 50 in load_once
  File "/opt/conda/lib/python3.7/site-packages/tensorboard/lazy.py", line 97 in wrapper
  File "/opt/conda/lib/python3.7/site-packages/tensorboard/lazy.py", line 65 in __getattr__
  File "/opt/conda/lib/python3.7/site-packages/tensorboard/summary/writer/event_file_writer.py", line 72 in __init__
  File "/opt/conda/lib/python3.7/site-packages/torch/utils/tensorboard/writer.py",line 66 in __init__
  File "/opt/conda/lib/python3.7/site-packages/torch/utils/tensorboard/writer.py",line 256 in _get_file_writer
  File "/opt/conda/lib/python3.7/site-packages/torch/utils/tensorboard/writer.py", line 225 in __init__


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

经过查询,发现极有可能是导包(from torch.utils.tensorboard import SummaryWriter)顺序导致的问题。参考链接:[TensorBoard] The different order of import SummaryWriter may cause Segmentation fault

将from torch.utils.tensorboard import SummaryWriter顺序调前,可以顺利导包和实例化了。

import xxx
from torch.utils.tensorboard import SummaryWriter
import yyy

  • 1
  • 2
  • 3
  • 4

改为

from torch.utils.tensorboard import SummaryWriter
import xxx
import yyy

  • 1
  • 2
  • 3
  • 4
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/671064
推荐阅读
相关标签
  

闽ICP备14008679号