Solution to Python TensorFlow 2.x incompatibility with version 1.x

I installed TensorFlow version 2.8, but the code I use is 1.x. I have found several methods, and I am currently using

import tensorflow.compat.v1 as tf
tf.disable_eager_execution()
#关闭eager运算,用于版本转换
##上面的用在开头处


tf.reset_default_graph()
##用于清除默认图形堆栈并重置全局默认图形。
##不清除的话,会报错说哪里哪里已创建什么的,代码运行一次要清除一次


 with tf.variable_scope('x',reuse=tf.AUTO_REUSE):
#在用tf.variable_scope时,不加reuse=tf.AUTO_REUSE,也会报错

       The first paragraph is used when importing the library, directly imported according to the 1.x version, but there still seems to be some problems; the second paragraph can be run together with the import library (but when testing, you need to import the library multiple times, It is recommended to reopen a line to run); the third paragraph is the solution when I encounter an error when using the tf.variable_scope() function.

        In addition to my method, there is another way to add .compat.v1 after each piece of code, such as tf.compat.v1.variable_scope(), which I think is too much trouble.

        If you still encounter problems related to the version of TensorFlow in the future, you will update it here. If you have encountered related problems, please leave a corresponding solution in the comment area. I can integrate it after the attempt is successful.

Guess you like

Origin blog.csdn.net/a1004550653/article/details/124632804