error in use of tf.app.flags

白昼怎懂夜的黑 提交于 2020-01-16 05:37:11

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!