1# TensorFlow Python API Upgrade Utility 2 3This tool allows you to upgrade your existing TensorFlow Python scripts, 4specifically: 5* `tf_upgrade_v2.py`: Upgrade code from TensorFlow 1.x to TensorFlow 2.0 preview. 6* `tf_upgrade.py`: Upgrade code to TensorFlow 1.0 from TensorFlow 0.11. 7 8## Running the script from pip package 9 10First, install TensorFlow pip package*. See 11https://www.tensorflow.org/install/pip. 12 13Upgrade script can be run on a single Python file: 14 15``` 16tf_upgrade_v2 --infile foo.py --outfile foo-upgraded.py 17``` 18 19It will print a list of errors it finds that it can't fix. You can also run 20it on a directory tree: 21 22``` 23# upgrade the .py files and copy all the other files to the outtree 24tf_upgrade_v2 --intree coolcode --outtree coolcode-upgraded 25 26# just upgrade the .py files 27tf_upgrade_v2 --intree coolcode --outtree coolcode-upgraded --copyotherfiles False 28``` 29 30*Note: `tf_upgrade_v2` is installed automatically as a script by the pip install 31 after TensorFlow 1.12. 32 33 34## Report 35 36The script will also dump out a report e.g. which will detail changes 37e.g.: 38 39``` 40'tensorflow/tools/compatibility/testdata/test_file_v1_12.py' Line 65 41-------------------------------------------------------------------------------- 42 43Added keyword 'input' to reordered function 'tf.argmax' 44Renamed keyword argument from 'dimension' to 'axis' 45 46 Old: tf.argmax([[1, 3, 2]], dimension=0) 47 ~~~~~~~~~~ 48 New: tf.argmax(input=[[1, 3, 2]], axis=0) 49 50``` 51 52## Caveats 53 54- Don't update parts of your code manually before running this script. In 55particular, functions that have had reordered arguments like `tf.argmax` 56or `tf.batch_to_space` will cause the script to incorrectly add keyword 57arguments that mismap arguments. 58 59- This script wouldn't actually reorder arguments. Instead, the script will add 60keyword arguments to functions that had their arguments reordered. 61 62- The script assumes that `tensorflow` is imported using `import tensorflow as tf`. 63 64- Note for upgrading to 2.0: Check out [tf2up.ml](http://tf2up.ml) for a 65 convenient tool to upgrade Jupyter notebooks and Python files in a GitHub 66 repository. 67 68- Note for upgrading to 1.0: There are some syntaxes that are not handleable with this script as this 69script was designed to use only standard python packages. 70If the script fails with "A necessary keyword argument failed to be inserted." or 71"Failed to find keyword lexicographically. Fix manually.", you can try 72[@machrisaa's fork of this script](https://github.com/machrisaa/tf0to1). 73[@machrisaa](https://github.com/machrisaa) has used the 74[RedBaron Python refactoring engine](https://redbaron.readthedocs.io/en/latest/) 75which is able to localize syntactic elements more reliably than the built-in 76`ast` module this script is based upon. Note that the alternative script is not 77available for TensorFlow 2.0 upgrade. 78