从 tf1.12 转换为 tf2详解

Google 提供了一个名为 tf_upgrade_v2的命令行脚本,该脚本会将 1.12 版文件(.py.ipynb文件)转换为 TensorFlow 2 兼容文件。

此转换的语法如下:

 tf_upgrade_v2   --infile  file_to_convert --outfile  converted_file

这里是更新脚本的实战演示,以及有关它的更多详细信息,请参见这里

重要的是要注意,在运行脚本之前,不应该手动更新代码部分。

该脚本不会解决所有问题,但是它生成的报告将标识那些必须手动解决的问题。

特别是,tf.contrib已从 TF2 中删除,因此必须跟踪并手动修复以前驻留在其中的函数。

这是脚本生成的报告的示例:

Processing file 'Chapter1_TF2_Snippets.ipynb'
 outputting to 'Chapter1_TF2_alpha'
 --------------------------------------------------------------------------------

 37:4: INFO: Added keywords to args of function 'tf.size'
 48:13: INFO: Added keywords to args of function 'tf.transpose'
 74:0: INFO: Added keywords to args of function 'tf.reduce_mean'
 75:0: INFO: Added keywords to args of function 'tf.reduce_mean'
 76:0: INFO: Added keywords to args of function 'tf.reduce_mean'
 77:0: INFO: Added keywords to args of function 'tf.reduce_mean'
 78:0: INFO: Added keywords to args of function 'tf.reduce_mean'
 110:4: INFO: Added keywords to args of function 'tf.argmax'
 114:4: INFO: Added keywords to args of function 'tf.argmin'
 121:4: INFO: Added keywords to args of function 'tf.argmax'
 123:4: INFO: Added keywords to args of function 'tf.argmin'
 127:4: INFO: Added keywords to args of function 'tf.argmax'
 129:4: INFO: Added keywords to args of function 'tf.argmin'
 136:0: ERROR: Using member tf.contrib.integrate.odeint in deprecated module tf.contrib. tf.contrib.integrate.odeint cannot be converted automatically. tf.contrib will not be distributed with TensorFlow 2.0, please consider an alternative in non-contrib TensorFlow, a community-maintained repository, or fork the required code.
 162:10: INFO: Added keywords to args of function 'tf.transpose'
 173:11: INFO: Added keywords to args of function 'tf.reduce_mean'

教程来源于Github,感谢apachecn大佬的无私奉献,致敬!

技术教程推荐

Service Mesh实践指南 -〔周晶〕

深入浅出区块链 -〔陈浩〕

Python核心技术与实战 -〔景霄〕

Swift核心技术与实战 -〔张杰〕

Service Mesh实战 -〔马若飞〕

Redis核心技术与实战 -〔蒋德钧〕

张汉东的Rust实战课 -〔张汉东〕

手把手带你写一门编程语言 -〔宫文学〕

Python实战 · 从0到1搭建直播视频平台 -〔Barry〕