
该材料提供了手册的翻译,以使用更新脚本
tf_upgrade_v2
将代码从TensorFlow 1.x自动更新到Tensorflow 2。
TensorFlow 2.0包含许多API更改,例如更改参数的顺序,重命名字符以及更改参数的默认值。 手动纠正所有这些修改很繁琐且容易出错。 为了简化更改并尽可能平滑地过渡到TF 2.0,TensorFlow团队创建了
tf_upgrade_v2
实用程序,以帮助从旧版代码迁移到新API。
注意: tf_upgrade_v2
为TensorFlow 1.13和更高版本(包括所有TF 2.0版本)自动安装的。
脚本的典型用法如下所示:
tf_upgrade_v2 \ --intree my_project/ \ --outtree my_project_v2/ \ --reportfile report.txt
该代码通过将现有的TensorFlow 1.x Python脚本转换为TensorFlow 2.0来加速升级过程。
转换脚本尽可能使流程自动化,但是仍然存在无法通过脚本修复的语法和风格更改。
相容性模组
某些API字符不能简单地使用字符串替换来更新。 为确保您的代码在TensorFlow 2.0中正常工作,更新脚本包括
compat.v1
模块。 此模块用等效链接
tf.compat.v1.foo
替换TF 1.x字符,例如
tf.compat.v1.foo
。 尽管兼容性模块很好,但是我们建议您手动减去替换项,然后将其迁移到
tf. *
名称空间中的新API
tf. *
tf. *
尽快替换
tf.compat.v1
命名空间。
由于TensorFlow 2.x模块的贬值(例如
tf.flags
和
tf.contrib
),切换到
compat.v1
无法绕过某些更改。 更新此类代码可能需要使用其他库(例如
absl.flags
或切换到
tenorflow/addons
的软件包)。
推荐的更新过程
本手册的这一部分演示了更新脚本的用法。 尽管更新脚本易于使用,但我们强烈建议您将脚本用作以下过程的一部分:
- 单元测试 :确保更新后的代码具有一组覆盖合理的单元测试。 这是Python代码,因此该语言不会保护您免受许多错误类的侵害。 还要确保所有依赖项都已更新为与TensorFlow 2.0兼容。
- 安装TensorFlow 1.14 : 将TensorFlow升级到最新版本的TensorFlow 1.x,至少为1.14。 它包含位于
tf.compat.v2
的最终TensorFlow 2.0 API。 - 1.14版的测试代码 :确保您的单元测试在这一点上通过。 您将在升级过程中重新启动它们,因此从绿色开始很重要。
- 运行更新脚本 :在包括测试的整个源代码树上运行
tf_upgrade_v2
。 这会将您的代码更新为仅使用TensorFlow 2.0中可用字符的格式。 过时的字符可从tf.compat.v1
。 他们随后将需要手动处理。\ N“, - 使用TensorFlow 1.14运行转换后的测试 :您的代码仍应在TensorFlow 1.14中正确运行。 再次运行单元测试。 在此阶段测试中的任何错误都意味着更新脚本中存在错误。
- 检查更新报告中是否存在警告和错误 :脚本编写了一个报告文件,解释了您需要仔细检查的所有转换或需要手动执行的所有操作。 例如:任何剩余的contrib实例都需要手动删除。
- 安装TensorFlow 2.0 :此时,切换到TensorFlow 2.0应该是安全的。
- 使用
v1.disable_v2_behavior
测试代码 :使用主测试函数中的v1.disable_v2_behavior()
重新启动测试,结果应与在1.14下运行时的结果相同。 - 启用V2行为 :现在,您的测试正在使用v2 API运行,您可以开始观察是否包含
v2 behavior
。 根据代码的编写方式,这可能需要进行一些更改。
使用更新脚本
安装方式
from __future__ import absolute_import, division, print_function, unicode_literals
try: import tensorflow.compat.v2 as tf except Exception: pass tf.enable_v2_behavior() print(tf.__version__)
克隆
tensorflow / models存储库,以便您有一些实验代码:
!git clone --branch r1.13.0 --depth 1 https://github.com/tensorflow/models
阅读帮助
该脚本必须与TensorFlow一起安装。 内置帮助如下所示:
!tf_upgrade_v2 -h
TF1代码示例
这是一个不在TensorFlow 2.0上运行的简单TensorFlow 1.0脚本
!head -n 65 models/samples/cookbook/regression/custom_regression.py | tail -n 10
给出以下错误:
Traceback (most recent call last): File "custom_regression.py", line 162, in <module> tf.logging.set_verbosity(tf.logging.INFO) AttributeError: module 'tensorflow' has no attribute 'logging'
单文件
更新脚本可以在单独的Python文件上运行:
!tf_upgrade_v2 \ --infile models/samples/cookbook/regression/custom_regression.py \ --outfile /tmp/custom_regression_v2.py
如果脚本找不到该代码的更正,它将显示错误。
INFO line 38:8: Renamed 'tf.feature_column.input_layer' to 'tf.compat.v1.feature_column.input_layer' INFO line 43:10: Renamed 'tf.layers.dense' to 'tf.compat.v1.layers.dense' INFO line 46:17: Renamed 'tf.layers.dense' to 'tf.compat.v1.layers.dense' INFO line 57:17: tf.losses.mean_squared_error requires manual check. tf.losses have been replaced with object oriented versions in TF 2.0 and after. The loss function calls have been converted to compat.v1 for backward compatibility. Please update these calls to the TF 2.0 versions. INFO line 57:17: Renamed 'tf.losses.mean_squared_error' to 'tf.compat.v1.losses.mean_squared_error' INFO line 61:15: Added keywords to args of function 'tf.shape' INFO line 62:15: Changed tf.to_float call to tf.cast(..., dtype=tf.float32). INFO line 65:40: Renamed 'tf.train.AdamOptimizer' to 'tf.compat.v1.train.AdamOptimizer' INFO line 68:39: Renamed 'tf.train.get_global_step' to 'tf.compat.v1.train.get_global_step' INFO line 83:9: tf.metrics.root_mean_squared_error requires manual check. tf.metrics have been replaced with object oriented versions in TF 2.0 and after. The metric function calls have been converted to compat.v1 for backward compatibility. Please update these calls to the TF 2.0 versions. INFO line 83:9: Renamed 'tf.metrics.root_mean_squared_error' to 'tf.compat.v1.metrics.root_mean_squared_error' INFO line 142:23: Renamed 'tf.train.AdamOptimizer' to 'tf.compat.v1.train.AdamOptimizer' INFO line 162:2: Renamed 'tf.logging.set_verbosity' to 'tf.compat.v1.logging.set_verbosity' INFO line 162:27: Renamed 'tf.logging.INFO' to 'tf.compat.v1.logging.INFO' INFO line 163:2: Renamed 'tf.app.run' to 'tf.compat.v1.app.run' TensorFlow 2.0 Upgrade Script ----------------------------- Converted 1 files Detected 0 issues that require attention -------------------------------------------------------------------------------- Make sure to read the detailed log 'report.txt'
目录树
包括此简单示例在内的典型项目都使用多个文件。 通常,您要更新整个软件包,因此脚本也可以在目录树上运行:
注意关于功能的一句话
dataset.make_one_shot_iterator
。
现在该脚本已经可以在TensorFlow 2.0中使用了。
请注意,由于有`tf.compat.v1`模块,转换后的脚本也将在TensorFlow 1.14中运行。
详细报告
该脚本还发布更改的详细列表。 在此示例中,他发现了一个可能不安全的转换,并在文件顶部添加了警告:
!head -n 20 tree_report.txt
TensorFlow 2.0 Upgrade Script ----------------------------- Converted 7 files Detected 1 issues that require attention -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- File: models/samples/cookbook/regression/automobile_data.py -------------------------------------------------------------------------------- models/samples/cookbook/regression/automobile_data.py:125:15: WARNING: Changing dataset.make_one_shot_iterator() to tf.compat.v1.data.make_one_shot_iterator(dataset). Please check this transformation. ================================================================================ Detailed log follows: ================================================================================ ================================================================================ Input tree: 'models/samples/cookbook/regression/' ================================================================================ -------------------------------------------------------------------------------- Processing file 'models/samples/cookbook/regression/custom_regression.py' outputting to 'regression_v2/custom_regression.py'
再次注意有关
Dataset.make_one_shot_iterator function
的
Dataset.make_one_shot_iterator function
。
安全模式
转换脚本还具有侵入性较小的“安全”模式,该模式仅更改导入即可使用
tensorflow.compat.v1
模块。
!tf_upgrade_v2 --mode SAFETY --infile dropout.py --outfile dropout_v2_safe.py > /dev/null
例如代码:
import tensorflow as tf d = tf.nn.dropout(tf.range(10), 0.2) z = tf.zeros_like(d, optimize=False)
在此模式下转换为以下内容:
import tensorflow.compat.v1 as tf tf.disable_v2_behavior() d = tf.nn.dropout(tf.range(10), 0.2) z = tf.zeros_like(d, optimize=False)
如您所见,您的代码尚未更新,但现在TensorFlow 1代码在TensorFlow 2中运行。
警告事项
- 运行脚本之前,请勿手动更新部分代码。 特别是,带有重新排序参数的函数(例如
tf.argmax
或tf.batch_to_space
)将强制脚本错误地添加关键字参数,这会使现有代码混乱。 - 该脚本假设使用
import tensorflow as tf
- 该脚本不会重新排列参数。 而是,脚本将自变量的关键字添加到其中自变量反转的函数中。
经过验证后,翻译也将出现在Tensorflow.org上。 如果您想参与将Tensorflow.org网站的文档翻译成俄语,请以个人身份或评论联系。 任何更正或评论表示赞赏。