Check whether Tensorflow 2.xx version and 1.xx version are installed successfully under Linux system

version problem

After querying the data, it is found that most of the methods for checking whether Tensorflow is successfully installed are in version 1.xx. If you use the test code of version 1.xx directly, an error will be reported:

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

The reason for this problem is that tf.Session() is the code in tensorflow1.X, so you have to modify the code.

Test code for 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运算成一个字符串的值,并返回。

Test code for 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))

Actual test results for Tensorflow version 2.6

Since I installed tensorflow version 2.6 myself, here is the actual demonstration of the experimental results of version 2.xx, as follows:
insert image description here

Summarize

The above is to check whether the Tensorflow 2.xx version and 1.xx version are installed successfully under the Linux system. Scholars test according to their own versions. Thank you for your support!

Guess you like

Origin blog.csdn.net/qq_40280673/article/details/132404775