AttributeError: module tensorflow has no attribute ‘io‘

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

AttributeError: module tensorflow has no attribute ‘io‘_第1张图片

根据报错信息进入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错误,要想不报错貌似只能见一个改一个声明了......

你可能感兴趣的:(pytorch)