Upgrade to tensorflow2.0

After tensorflow2.0 launched, to embrace keras, simplified API interface, making tensorflow pleasant in use on a lot.

Tensorflow1.x to upgrade from 2.x

Option One: still use tf 1.x script

For the original 1.x series code, when after tensorflow support background upgrade to 2.x, most of the code does not need to be rewritten, only need to modify the code to load tensorflow

import tensorflow.compat.v1 as tf

tf.disable_v2_behavior()

On it.

Option Two: Upgrading to 2.x project code

But this does not really play the new features bring new 2.x API. In order to project code 1.x to 2.x migration of real style, tensorflow project group has released tf_upgrade_v2 script to achieve this function. Use this script is as follows:

tf_upgrade_v2 \

  --intree my_project/ \

  --outtree my_project_v2/ \

  --reportfile report.txt

Other parameters can help documentation tf_upgrade_v2 script:tf_upgrade_v2 -h

However, some symbols can not just simply the API obtained by the replacement string. While calling most of the 1.x version tf function can be modified to call the function by tf.compat.v1.func_name()way of get, but there are some functions are abandoned, such as tf.flags and tf.contrib in version 2.x, and there are some function may need to call some other cry, e.g. absl.flags, or the function corresponding to the adjusted tensorflow.addons module.

Given the above, the upgrade is officially recommended:
1) unit test
2) mounted tensorflow 1.14, in tf1.14 version actually included tf 2.0 version of the code, encapsulated in tf.compat.v2 module.
3) with 1.14 tf test version of the code, this step is critical to ensure that all of the code in the 1.14 version of the test passed.
4) upgrade project code with tf_upgrade_v2
5) code after test with version 1.14 upgrade.
6) Check the upgrade report warning and error
. 7) mounted tf2.0
. 8) test code, the code is added in front of tf.disable_v2_behavior()the function, and then run the test
9) commented in the test code, the run with 2.x API.

If you have any questions you can check out the official Code Migration Guide: https: //tensorflow.google.cn/guide/migrate

Published 111 original articles · won praise 118 · views 280 000 +

Guess you like

Origin blog.csdn.net/happyhorizion/article/details/103833847