【Tensorflow】Linux下Tensorflow报错:AttributeError: module ‘tensorflow‘ has no attribute ‘xxxx‘

文章目录

  • 问题描述
  • 解决办法
  • 参考链接

问题描述

Linux 下使用 tensorflow-gpu 出现如下报错:

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

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

AttributeError: module 'tensorflow' has no attribute 'constant'

原因:tensorflow 变成了一个没有任何内容的模块(导致 tensorflow 变为空的原因未知)。

解决办法

卸载重装 tensorflow-gpu 。

(tensorflow) [root@master zz]# pip uninstall -y tensorflow
...
Proceed (y/n)? y
  Successfully uninstalled tensorflow-gpu-2.0.0b1
(tensorflow) [root@master zz]# pip3 install tensorflow-gpu
Collecting tensorflow-gpu
  Downloading tensorflow_gpu-2.2.0-cp35-cp35m-manylinux2010_x86_64.whl (516.2 MB)
     |████████████████████████████████| 516.2 MB 30 kB/s 
...

验证安装是否正确。

(tensorflow) [root@master zz]# python3
Python 3.5.6 |Anaconda, Inc.| (default, Aug 26 2018, 21:41:56) 
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
... # 冗余信息
>>> tf.add(1, 2).numpy()
...
3
>>> hello = tf.constant('Hello, TensorFlow!')
>>> hello.numpy()
b'Hello, TensorFlow!'
>>> tf.__version__
'2.2.0'

参考链接

tensorflow 报错: AttributeError: module ‘tensorflow’ has no attribute ‘xxxx’
module ‘tensorflow’ has no attribute ‘constant’

你可能感兴趣的:(Linux,Python)