欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 房产 > 家装 > PyTorch使用tensorboard的SummaryWriter报错

PyTorch使用tensorboard的SummaryWriter报错

2024/12/1 0:32:30 来源:https://blog.csdn.net/john_bh/article/details/139402514  浏览:    关键词:PyTorch使用tensorboard的SummaryWriter报错

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

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

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

python -X faulthandler your_script.py

错误代码:

import xxx
from torch.utils.tensorboard import SummaryWriter
import yyylog_dir = './run_logs'
self.writer = SummaryWriter(log_dir)

报错如下:

 File "/opt/conda/lib/python3.7/site-packages/tensorboard/compat/__init__.py", line 45 in tfFile "/opt/conda/lib/python3.7/site-packages/tensorboard/lazy.py", line 50 in load_onceFile "/opt/conda/lib/python3.7/site-packages/tensorboard/lazy.py", line 97 in wrapperFile "/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_writerFile "/opt/conda/lib/python3.7/site-packages/torch/utils/tensorboard/writer.py", line 225 in __init__

经过查询,发现极有可能是导包(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

改为

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

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com