当前位置: 移动技术网 > IT编程>脚本编程>Python > 【Tensorflow】Linux下Tensorflow报错:AttributeError: module ‘tensorflow‘ has no attribute ‘xxxx‘

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

2020年07月14日  | 移动技术网IT编程  | 我要评论

问题描述

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 "<ipython-input-2-25b92e4d5dec>", 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’

本文地址:https://blog.csdn.net/kernelxiao/article/details/107316841

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网