abseil/abseil-py

How to get flags working with Tensorflow 2.x

demongolem-biz opened this issue · 4 comments

I have seen some suggest that flags here can be a substitute for the deprecation of flags with the change from TF1.x to TF2.x, but I don't see a guide or code how to do it. What throws me is app.run(main). Can we have some sample code of TF2.x accessing flags from this library?

yilei commented

If you are coming from tensorflow, use absl.flags instead of tf.flags, they are (99%) identical: https://github.com/tensorflow/tensorflow/blob/daa561a0614a96bda706d19e9db80697391eb4da/tensorflow/python/platform/flags.py#L22

(tf.app.run is also almost the same as absl.app.run, except later doesn't allow unknown flags: https://github.com/tensorflow/tensorflow/blob/daa561a0614a96bda706d19e9db80697391eb4da/tensorflow/python/platform/app.py#L25-L36).

Does this help?

Well see in trying to convert my 1.14 code to 2.5, I note that there is no main function and there is nowhere where tf.app.run or app.run is called. Instead, the code has a long series of calls like

tf.app.flags.DEFINE_string

and then after all the flags are defined

FLAGS = tf.app.flags.FLAGS

config = FLAGS

And all of the things that were just defined are in config at the end.

How are the flag values set? It looks like configparser is used to do where we have a configuration file with all the various properties which is read in by configparse .

yilei commented

For the tf.flags v.s. absl.flags question: you should be able to simply replace tf.flags (or tf.app.flags) with absl.flags and it should work, since tf.flags is mostly an alias to absl.flags.

I'm not sure why your code doesn't have tf.app.run as it should be the way how tf.flags are parsed too. configparser is not related to absl.flags.

If you just want to know how to use absl.flags, you can see example here: https://abseil.io/docs/python/guides/flags#example-usage

yilei commented

Closing due to inactivity. Feel free to reopen if still needed.