Linux系统下检验Tensorflow 2.xx版本和1.xx版本是否安装成功

25 篇文章 7 订阅
3 篇文章 1 订阅

版本问题

查询资料发现,多数检验Tensorflow是否安装成功的方法,多数方法都是1.xx版本的,直接使用1.xx版本的测是代码还会报一下错误:

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

之所以出现该问题是因为tf.Session()是tensorflow1.X中的代码,所以得修改一下代码。

Tensorflow 1.xx的测试代码

import tensorflow as tf                     # 导入TensorFlow模块,并缩写为tf
hello = tf.constant(‘Hello, TensorFlow!’)   #使用TensorFlow的constant方法创建一个字符串Tensor,其值为'Hello, YNNU'
sess = tf.Session()                         # 创建一个TensorFlow Session对象,该对象用于启动图,调用图中的op
print(sess.run(hello))                      # 调用Session的run方法运行hello op。run方法需要传入要运行的op或者Tensor,这里传入hello Tensor。
                                            # sess.run会把hello Tensor运算成一个字符串的值,并返回。

Tensorflow 2.xx的测试代码1

import tensorflow as tf
tf.compat.v1.disable_eager_execution()
hello = tf.constant("Hello,YNNU")
sess = tf.compat.v1.Session()
print(sess.run(hello))

Tensorflow 2.6版本实际的测验结果

由于我自己安装的是tensorflow 2.6版本,这里主要实际演示2.xx版本的实验结果,如下:
在这里插入图片描述

Tensorflow 2.xx的测试代码2

import tensorflow as tf

print(tf.test.is_built_with_cuda())       # 检查TensorFlow是否构建了GPU支持
print(tf.test.is_gpu_available())         # 检查是否可以在GPU上运行TensorFlow

在这里插入图片描述

总结

以上就是Linux系统下检验Tensorflow 2.xx版本和1.xx版本是否安装成功,学者根据自己的版本对应测试,多多支持,谢谢!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

视觉研坊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值