0
点赞
收藏
分享

微信扫一扫

深度学习(Tensorflow—GPU安装)

木匠0819 2022-04-28 阅读 76

本人在进行深度学习的过程中对tensorflow的安装尝试过很多次也错误过很多次,我认为究其原因还是现在很难在网上寻找到一篇很完备的文章去详细的说明tensorflow 的安装全部过程,当然也仅限于GPU版本,毕竟CPU版本要简单很多,简单所带来的牺牲就是一定运行效率的降低。
首先在进行tensorflow的安装时,我们要先进行安装cuda和cuDNN(当然如果准备安装CPU版本就不用这一步)
我寻找的一篇很详细的安装cuda和cuDNN的布置的博客,希望对大家有所帮助。
https://blog.csdn.net/m0_45447650/article/details/123704930
其中有一个十分值得注意的点,在注册账号的时候,不要强求,实在不行我们可以“科学上网”,没必要强求,真的QAQ。
然后就是tensorflow的安装
我的建议是使用anaconda为tensorflow配置一个新环境,
详细的安装过程参考如下连接
https://zhuanlan.zhihu.com/p/392693390
其中需要注意的是测试TensorFlow 是否安装成功
我提供如下方法
第一步

import tensorflow as tf
sess = tf.Session()
a = tf.constant(1)
b = tf.constant(2) 
print(sess.run(a+b))

第二步
如若运行上述代码出现以下错误:

    sess = tf.Session()
AttributeError: module 'tensorflow' has no attribute 'Session'

则添加下列代码:

sess = tf.compat.v1.Session()

若添加上述代码,结果为3,则证明TensorFlow运行成功
第三步
如若添加上述代码后仍然出现下列错误

raise RuntimeError('The Session graph is empty.  Add operations to the '
RuntimeError: The Session graph is empty.  Add operations to the graph before calling run().

问题产生的原因:无法执行sess.run()的原因是tensorflow版本不同导致的,tensorflow版本2.0无法兼容版本1.0.

添加以下代码:

tf.compat.v1.disable_eager_execution()

最终代码为

import tensorflow as tf
tf.compat.v1.disable_eager_execution()
sess = tf.compat.v1.Session()
a = tf.constant(1)
b = tf.constant(2)
print(sess.run(a+b))

运行结果为:3.则证明安装成功。

举报

相关推荐

0 条评论