AttributeError: module tensorflow has no attribute ‘io‘

PyCharm,Pytorch1.2,使用tensorboard的SummaryWriter时,运行报错

根据报错信息进入event_file_writer.py文件,按Ctrl键点击makedirs时出现其声明信息,发现'io'位于tensorboard.compat.tensorflow_stub中,而import中为 from tensorboard.compat import tf,同样通过Ctrl之后点击tf可以查看原函数定义:

@_lazy.lazy_load("tensorboard.compat.tf")
def tf():
    """Provide the root module of a TF-like API for use within TensorBoard.

    By default this is equivalent to `import tensorflow as tf`, but it can be used
    in combination with //tensorboard/compat:tensorflow (to fall back to a stub TF
    API implementation if the real one is not available) or with
    //tensorboard/compat:no_tensorflow (to force unconditional use of the stub).

    Returns:
      The root module of a TF-like API, if available.

    Raises:
      ImportError: if a TF-like API is not available.
    """
    try:
        from tensorboard.compat import notf  # noqa: F401
    except ImportError:
        try:
            import tensorflow

            return tensorflow
        except ImportError:
            pass
    from tensorboard.compat import tensorflow_stub

    return tensorflow_stub

tf函数按理应该能返回tensorflow_stub,但前面的运行报错表明操作失败,于是尝试修改event_file_writer.py中的import语句from tensorboard.compat import tf 为:

from tensorboard.compat import tensorflow_stub as tf

再运行就可以正常使用了。

具体原因不明,目前来说,直接声明能解决问题。

后续查看SummaryWriter生成的文件时,还会在tensorboard不同的文件中报类似的Attribute错误,要想不报错貌似只能见一个改一个声明了......

  • 8
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
AttributeError: module 'tensorflow' has no attribute 'gfile'错误通常是由于TensorFlow版本不兼容引起的。在TensorFlow 2.x版本中,tf.gfile被移除了,所以在使用TensorFlow 2.x版本时,如果你的代码中有使用到tf.gfile,那么就会出现这个错误。 解决办法(一)是升级你的TensorFlow版本到最新的2.x版本。在最新的TensorFlow版本中,tf.gfile已被tf.io.gfile取代。所以你只需要将代码中的tf.gfile替换为tf.io.gfile即可。 例如,如果你的代码中有这样一行代码: tf.gfile.GFile(file_path, 'w') 你可以将其修改为: tf.io.gfile.GFile(file_path, 'w') 这样就可以解决AttributeError: module 'tensorflow' has no attribute 'gfile'错误。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [AttributeError: module 'tensorflow.compat.v1' has no attribute '](https://download.csdn.net/download/qq_38766019/86272235)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [程序报错:AttributeError: module 'tensorflow' has no attribute 'xxx' 解决办法](https://blog.csdn.net/qq_41320433/article/details/104198059)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值