模块“tensorflow”没有属性“constant”

Mik*_*oho 2 python tensorflow

我以前安装了 tensorflow 并安装了 gpu 支持并正常工作。我尝试安装 keras 之后什么都不起作用了。我已经卸载了 keras 和 tensorflow。

我尝试按照 tensorflow 网页https://www.tensorflow.org/install/install_linux(顺便说一下,我正在运行 ubuntu 18 )上的说明重新安装支持 gpu 的 tensorfow(现在是 1.9 版)。我尝试运行代码

import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))
Run Code Online (Sandbox Code Playgroud)

我得到了错误

import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))
Traceback (most recent call last):

  File "<ipython-input-2-25b92e4d5dec>", line 2, in <module>
    hello = tf.constant('Hello, TensorFlow!')

AttributeError: module 'tensorflow' has no attribute 'constant'
Run Code Online (Sandbox Code Playgroud)

我尝试查看其他线程,有人建议将 C 大写为“常量”,但这也不起作用。

任何建议都会很棒!

谢谢

Jon*_*IAR 5

您的安装已损坏,执行以下操作应该能让您重回正轨。

您可以尝试以下操作:

pip install virtualenv
virtualenv venv

## Linux:
source venv/bin/activate

## Windows:
venv\Scripts\activate.bat

pip install tensorflow-gpu==1.9.0

python temp.py
Run Code Online (Sandbox Code Playgroud)

如果这没有任何问题。您的 python 安装已损坏,我建议您手动删除 python 库中的所有内容。

也许这会有所帮助:

  1. 打开一个新终端
  2. pip uninstall tensorflow -y
  3. pip uninstall tensorflow-gpu -y
  4. pip uninstall tensorboard -y
  5. pip install --upgrade tensorflow-gpu==1.9.0 -y

如果您已经使用了 virtualenv,只需删除该文件夹并重新创建它


Chi*_*rag 5

我有同样的问题,我错误地将我的文件命名为tensorflow.py. 如果文件夹名称是 tensorflow,也会出现同样的问题。

我只是用别的东西重命名了文件,它解决了这个问题。