# 十、从 tf1.12 转换为 tf2

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

此转换的语法如下：

```py
 tf_upgrade_v2   --infile  file_to_convert --outfile  converted_file
```

[这里是更新脚本的实战演示](https://www.youtube.com/watch?v=JmSNUeBG-PQ&list=PLQY2H8rRoyvzoUYI26kHmKSJBedn3SQuB&index=32&t=71s)，以及有关它的更多详细信息，请参见[这里](https://github.com/tensorflow/docs/blob/master/site/en/r2/guide/upgrade.md) 。

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

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

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

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

```py
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'
```