深度学习(Tensorflow—GPU安装)

本人在进行深度学习的过程中对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
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值