问题
I used tf.app.flags in my tensorflow program like this:
flags = tf.app.flags
FLAGS = flags.FLAGS
flags.DEFINE_string('model_dir', './models','Save checkpoint')
.
.
.
if __name__ == "__main__":
# main()
tf.app.run()
But when run my code two time it makes this error:
ArgumentError: argument --model_dir: conflicting option string: --model_dir
I think tensorflow create a argument for --model_dir and when it run again it try to create again a argument for --model_dir, but conflicted by existence --model_dir.
are there any way two solve this problem or I used python parameters against tf.app.falgs?
回答1:
My guess is that you are working in an environment like a Jupyter/iPython notebook.
The reason you are having this issue is that the flags data seems to be maintained within the Python session. tf.app.flags.FLAGS.__getattr__('model_dir') is equal to ./models even if you reset your variable FLAGS.
If you are using a notebook, I suggest you put your flag definitions in a separate cell. The only way that I found to reset tf.app.flags.FLAGS is to restart the kernel/session.
回答2:
You can try like this:
#define flags
tf.flags.DEFINE_integer("age", 17, "age of user(default:20)")
tf.flags.DEFINE_boolean("drink_allow", False, "if can drink or not(default:False)")
tf.flags.DEFINE_float("weight", 55.55, "weight of user(default:55.55kg)")
FLAGS = tf.flags.FLAGS #init flags
FLAGS._parse_flags() # parse flags
for attr,value in FLAGS.__flags.items():
print("attr:%s\tvalue:%s" % (attr,str(value)))
来源:https://stackoverflow.com/questions/38267771/error-in-use-of-tf-app-flags