进入CMD 环境中,python -m pip install tensorflow(多次尝试)(pip3 install tensorflow)
安装成功!
此次安装没有安装CUDA,tensorflow的计算在单机CPU下进行。
或者用conda install tensorflow-gpu
安装
使用conda install tensorflow-gpu安装tensorflow时未指定版本,导致安装了tensorflow2.1版本。而tf.Session是1.X版本tensorflow中的代码。
解决方法
import tensorflow as tf
>>>hello = tf.constant('Hello, TensorFlow!')
>>>sess = tf.Session()
>>>print(sess.run(hello))
但是出现module ‘tensorflow’ has no attribute ‘Session’. Did you mean: ‘version’?报错
报错AttributeError: module ‘tensorflow’ has no attribute ‘Session’。这其实不是安装错误,是因为在新的Tensorflow 2.0版本中已经移除了Session这一模块,改换运行代码
tf.compat.v1.Session()
就可以获得与原先相同的输出信息。如果觉得不方便,也可以改换低版本的Tensorflow,直接用pip即可安装
sudo pip install tensorflow==1.14
查看tensorflow版本
由于tensorflow版本不同,可能一些函数的调用也有变换,这时候可能需要查看tensorflow版本,可以在终端输入查询命令如下:
import tensorflow as tf
tf.__version__
报错:RuntimeError: The Session graph is empty. Add operations to the graph before calling run().
问题产生的原因:无法执行sess.run()的原因是tensorflow版本不同导致的,tensorflow版本2.0无法兼容版本1.0.