警告 在将 Tensorflow 1 代码升级到 Tensorflow 2 代码期间

Warning During upgrading a Tensorflow 1 code to Tensorflow 2 code

我正尝试在 google colab 上将现有的 TF1 代码转换为 TF2 代码。我在 运行 命令后收到 14 条警告消息: !tf_upgrade_v2 --infile medgan.py --outfile medgan_upgraded.py

警告:tf.get_variable 需要手动检查。 tf.get_variable returns ResourceVariables 在 2.0 中默认使用,它具有明确定义的语义并且对形状更严格。您可以通过传递 use_resource=False 或调用 tf.compat.v1.disable_resource_variables().

来禁用此行为

我是 tensorflow 的新手,不确定这个命令是什么意思。在保存升级后的 .py 文件之前我的下一步应该是什么?我应该担心这些警告吗? TF 通过这个警告告诉我做什么?谢谢。

使用自动脚本将代码从 Tensorflow 1.x 迁移到 Tensorflow 2.x 只会执行初始操作。但是在这个过程中,需要注意的是,有很多东西不会落入Tensorflow 2.x实现,比如placeholderssessionscollections、t[=16] =],以及其他 1.x 功能,包括 variables.

的行为变化

先说一下tf.get_variabletf.Variable的区别。

tf.get_variable 从图中获取一个具有指定参数的现有变量,如果不存在则创建一个新变量,而 tf.Variable 将始终创建一个新变量,即使传递相同的名称,Tensorflow 将分配带有后缀 variable_name_1.

的新名称

Tensorflow 2.x 中,使用 tf.Variable 默认创建资源变量,默认情况下启用即时执行。

除非您在变量使用中遇到问题,否则您不必担心此警告。 如果你想禁用资源变量 tf.compat.v1.disable_resource_variables() is depreciated,你可以在 tf.get_variable() 中使用 use_resource= False,当在 Tensorflow 2.x 中默认启用急切执行时,它将被强制为真。

您可以从 Tensorflow 的 this 文档中查看在迁移代码期间要进行的所有其他更改和观察。