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

版本问题

查询资料发现,多数检验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的测试代码:

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版本的实验结果,如下:
在这里插入图片描述

总结

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

猜你喜欢

转载自blog.csdn.net/qq_40280673/article/details/132404775
今日推荐