AttributeError: 'module' object has no attribute 'Graph'

AttributeError: 'module' object has no attribute 'Graph'

在 MacOS 中,我使用 Pip 按照 tensorflow.org 网站上的标准说明安装了 tensorflow。

当我启动 Spyder 时,我可以无误地执行以下指令:

import tensorflow as tf

但是当我尝试运行以下指令时:g = tf.Graph(),我得到以下错误:

AttributeError: 'module' object has no attribute 'Graph'

当我在 "python" 或 "ipython" 而不是 Spyder 中执行相同操作时,我没有收到此错误。但我想使用 Spyder。

我尝试使用 Conda 而不是 Pip 安装,但出现了相同的症状。

这些步骤对我有用:(linux, pip, tf1.4-gpu)

  • 卸载tensorflow pip uninstall tensorflow-gpu
  • 删除 pip 缓存 rm -rf ~/.cache/pip/*
  • 重新安装tensorflow pip install -U tensorflow-gpu

对于最近来的任何人,在使用 Keras 时可能会出现此错误。通过更改所有

解决了它
from keras.something import something

from tensorflow.keras.something import something

可以看到github issue about this.