Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TensorFlow 1.5.0-rc0: error using `tf.app.flags`

The following flags were defined in a misc_fun.py file to include machine and directories info:

import tensorflow as tf
flags = tf.app.flags
FLAGS = flags.FLAGS
# definitions
flags.DEFINE_string(
    'DEFAULT_IN',
    '~/PycharmProjects/myNN/Data/',
    """Default input folder.""")
...

It worked fine in TensorFlow 1.0 - 1.4 versions (with Pycharm). After updating to TensorFlow 1.5.-rc0, the following error occurred:

Usage:

from misc_fun import FLAGS
FLAGS.DEFAULT_IN = FLAGS.DEFAULT_DOWNLOAD  # change default input folder

Error:

UnparsedFlagAccessError: Trying to access flag --DEFAULT_DOWNLOAD before flags were parsed.

However print(FLAGS) worked fine, which gives:

misc_fun:
  --DEFAULT_DOWNLOAD: default download folder for large datasets.
    (default: '/home/username/Downloads/Data/')
  --DEFAULT_IN: default input folder.
    (default: '~/PycharmProjects/myNN/Data/')
...

I tried FLAGS = flags.FLAGS(sys.argv), resulting in the following error:

UnrecognizedFlagError: Unknown command line flag 'f'

Although there is a workaround using the class object, I wonder what could be the problem here.

like image 574
Richard_wth Avatar asked Dec 07 '22 16:12

Richard_wth


2 Answers

I have tried adding the following line below.

tf.app.flags.DEFINE_string('f', '', 'kernel')

This solution is different from others in that it is simple and easy to try. You just need to add this into your code, and it doesn't change your system. Please let me know if this solution helps solve other people's problems.

The reference for this solution is from a Chinese website: https://blog.csdn.net/qq_39956625/article/details/80500291

like image 95
aysljc Avatar answered Dec 10 '22 18:12

aysljc


With 1.5.0-rc0 the Tensorflow maintainers have switched tf.app.flags to the flags module from abseil. Unfortunately, it is not 100% API compatible to the previous implementation. I worked around your problem with something like

remaining_args = FLAGS([sys.argv[0]] + [flag for flag in sys.argv if flag.startswith("--")])
assert(remaining_args == [sys.argv[0]])

before accessing the FLAGS object the first time.

like image 43
rerx Avatar answered Dec 10 '22 18:12

rerx