Python 如何在 TensorFlow 中使用“FLAGS”(命令行开关)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35241827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use "FLAGS" (command line switches) in TensorFlow?
提问by Dims
I am trying to set custom batch size in my application.
我正在尝试在我的应用程序中设置自定义批量大小。
If I put the following code into my app
如果我将以下代码放入我的应用程序中
tf.app.flags.DEFINE_integer('batch_size', 128,
"""Number of images to process in a batch.""")
it says the following error
它说以下错误
argparse.ArgumentError: argument --batch_size: conflicting option string(s): --batch_size
and if I remove this statement, it swears:
如果我删除此声明,它会发誓:
usage: <myscript> [-h] [--batch_size BATCH_SIZE] [--data_dir DATA_DIR]
[--checkpoint_dir CHECKPOINT_DIR]
at the line where FLAGS.batch_size
used.
在使用的行FLAGS.batch_size
。
myscript
is the name of my script and I didn't write this message anywhere and don't expect these command line switches at all. Looks like TF
uses some Python switch parsing library and expecting these switches somehow. How to avoid this and expect custom switches?
myscript
是我的脚本的名称,我没有在任何地方写下这条消息,也根本不指望这些命令行开关。看起来TF
使用了一些 Python 开关解析库并以某种方式期待这些开关。如何避免这种情况并期望自定义开关?
How to hardcode custom batch_size?
如何硬编码自定义batch_size?
UPDATE
更新
My command line is follows:
我的命令行如下:
myscript image1.png image2.png image3.png
PNGs are images from CIFAR database I wish to recognize from command line. This is command line I wish it to be, I don't wish it contain options listed in "usage" output.
PNG 是来自 CIFAR 数据库的图像,我希望从命令行识别。这是我希望的命令行,我不希望它包含“使用”输出中列出的选项。
采纳答案by mrry
From your update, it sounds like you don't want to use the FLAGS
module at all. If you look at a program like cifar10_train.py
, you'll see the following near the bottom of the script:
从您的更新来看,您似乎根本不想使用该FLAGS
模块。如果您查看类似 的程序cifar10_train.py
,您将在脚本底部附近看到以下内容:
def main(argv=None): # pylint: disable=unused-argument
# ...
if __name__ == '__main__':
tf.app.run()
The tf.app.run()
invocation is a bit of boilerplate that ensures that any flags are parsed, and then invokes the main()
function in the same module. Notice that main()
has an argv
argument. This will be filled with the remaining arguments to your program: in your example, it will be a list ["image1.png", "image2.png", "image3.png"]
. Therefore you can simply write your main()
function to be something like:
该tf.app.run()
调用是一个比特的样板,其确保任何标志被解析,然后调用main()
在同一模块中的功能。请注意,main()
有一个argv
参数。这将填充您程序的其余参数:在您的示例中,它将是一个 list ["image1.png", "image2.png", "image3.png"]
。因此,您可以简单地将main()
函数编写为:
def main(argv=None):
if argv:
for filename in argv:
run_inference_on_file(filename)
回答by keveman
I suspect you are importing cifar10.pythat already has the batch_size
flag defined, and the error is due to you trying to re-define a flag with the same name. If you are importing cifar10
, you can simply use --batch_size
from the command line, and FLAGS.batch_size
in your code.
我怀疑您正在导入已经定义了标志的cifar10.pybatch_size
,并且错误是由于您试图重新定义具有相同名称的标志。如果您正在导入cifar10
,您可以简单地--batch_size
从命令行使用,并FLAGS.batch_size
在您的代码中使用。