Docker容器运行Tensorboard报No module named 'tensorboard.context'错误
问题根源
你遇到的报错是tensorboard和tensorboard_plugin_profile版本不兼容导致的:
- 你使用的
tensorflow/tensorflow:2.2.0-gpu基础镜像自带的TensorBoard版本为2.2.x,该版本没有tensorboard.context模块 - 执行
pip install -U tensorboard_plugin_profile时会默认安装最新版的profile插件,最新版插件依赖高版本TensorBoard的tensorboard.context模块,和你环境内的低版本TensorBoard不匹配
解决方案
方案1:固定插件版本适配现有TensorFlow 2.2环境
不需要升级基础镜像,直接调整Dockerfile里的插件安装指令,安装和TensorBoard大版本匹配的2.2.0版本插件即可:
# 原Dockerfile里无需单独执行pip install tensorboard,tf2.2基础镜像已自带匹配版本的tensorboard RUN pip install tensorboard_plugin_profile==2.2.0
调整后重新构建镜像,启动TensorBoard即可正常加载Profile插件,使用Profiler功能。
方案2:升级TensorFlow基础镜像(推荐)
如果你的业务可以适配更高版本的TensorFlow,建议将基础镜像升级到2.4.0及以上版本,高版本TensorFlow自带的TensorBoard兼容性更好,同时也支持更多Profiler的新功能:
# 示例使用tf2.10-gpu基础镜像,可根据需求选择其他更高版本 FROM tensorflow/tensorflow:2.10.0-gpu ... # 直接安装最新版profile插件即可,和高版本tensorboard适配 RUN pip install -U tensorboard_plugin_profile
验证方式
镜像构建完成后进入容器执行以下命令,确认两个包的大版本号一致即可:
pip list | grep tensorboard # 正常输出示例(大版本一致即可): # tensorboard 2.2.0 # tensorboard-plugin-profile 2.2.0
内容的提问来源于stack exchange,提问作者user17579347
相关产品推荐
相关产品推荐

