Search code examples
pythontensorflowgoogle-colaboratorytensorflow2.0

Warning During upgrading a Tensorflow 1 code to Tensorflow 2 code


I am trying to convert an existing TF1 code to TF2 code on google colab. I get 14 of this warning message after I run the command: !tf_upgrade_v2 --infile medgan.py --outfile medgan_upgraded.py

WARNING: tf.get_variable requires manual check. tf.get_variable returns ResourceVariables by default in 2.0, which have well-defined semantics and are stricter about shapes. You can disable this behavior by passing use_resource=False, or by calling tf.compat.v1.disable_resource_variables().

I am new to tensorflow and not sure what this command mean. What should be my next step before saving the upgraded .py file? Should I worry about these warnings? What is TF telling me to do by this warning? Thank you.


Solution

  • Migrating code from Tensorflow 1.x to Tensorflow 2.x using Automated script will just do an initial pass. But in the process, you need to note that there are many things which won't fall under Tensorflow 2.x implementations such as placeholders, sessions, collections,tf.contrib, and other 1.x functionalities including changes in the behavior of variables.

    First, let me tell you the difference between tf.get_variable and tf.Variable.

    tf.get_variable gets an existing variable with specifies parameters from the graph, and if it doesn't exist it creates a new one, whereas tf.Variable will always create a new variable, even if the same name is passed, Tensorflow will assign the new name with the suffix variable_name_1.

    In Tensorflow 2.x using tf.Variable creates a Resource variable as default with eager execution is enabled by default.

    You don't have to worry about this warning unless you are facing behavior in your variables usage. If you want to disable the resource variable tf.compat.v1.disable_resource_variables() is depreciated instead you can use use_resource= False in tf.get_variable() which will be forced to true when eager excecution is enabled by default in Tensorflow 2.x.

    You can see all the other changes and observations to be made during migrating your code from this documentation from Tensorflow.