Python TensorFlow 中 tf.app.flags 的目的是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33932901/
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
What's the purpose of tf.app.flags in TensorFlow?
提问by flyaway1217
I am reading some example codes in Tensorflow, I found following code
我正在阅读 Tensorflow 中的一些示例代码,我发现以下代码
flags = tf.app.flags
FLAGS = flags.FLAGS
flags.DEFINE_float('learning_rate', 0.01, 'Initial learning rate.')
flags.DEFINE_integer('max_steps', 2000, 'Number of steps to run trainer.')
flags.DEFINE_integer('hidden1', 128, 'Number of units in hidden layer 1.')
flags.DEFINE_integer('hidden2', 32, 'Number of units in hidden layer 2.')
flags.DEFINE_integer('batch_size', 100, 'Batch size. '
'Must divide evenly into the dataset sizes.')
flags.DEFINE_string('train_dir', 'data', 'Directory to put the training data.')
flags.DEFINE_boolean('fake_data', False, 'If true, uses fake data '
'for unit testing.')
in tensorflow/tensorflow/g3doc/tutorials/mnist/fully_connected_feed.py
在 tensorflow/tensorflow/g3doc/tutorials/mnist/fully_connected_feed.py
But I can't find any docs about this usage of tf.app.flags
.
但我找不到任何关于tf.app.flags
.
And I found the implementation of this flags is in the
tensorflow/tensorflow/python/platform/default/_flags.py
我发现这个标志的实现是在
tensorflow/tensorflow/python/platform/default/_flags.py
Obviously, this tf.app.flags
is somehow used to configure a network, so why is it not in the API docs? Can anyone explain what is going on here?
显然,这tf.app.flags
以某种方式用于配置网络,那么为什么它不在 API 文档中呢?谁能解释这里发生了什么?
回答by mrry
The tf.app.flags
module is presently a thin wrapper around python-gflags, so the documentation for that projectis the best resource for how to use itargparse
, which implements a subset of the functionality in python-gflags
.
该tf.app.flags
模块目前是围绕python-gflags 的一个瘦包装器,因此该项目的文档是有关如何使用它的最佳资源argparse
,它实现了python-gflags
.
Note that this module is currently packaged as a convenience for writing demo apps, and is not technically part of the public API, so it may change in future.
请注意,此模块目前打包为方便编写演示应用程序,并且在技术上不是公共 API 的一部分,因此将来可能会更改。
We recommend that you implement your own flag parsing using argparse
or whatever library you prefer.
我们建议您使用argparse
或您喜欢的任何库来实现自己的标志解析。
EDIT:The tf.app.flags
module is not in fact implemented using python-gflags
, but it uses a similar API.
编辑:该tf.app.flags
模块实际上并未使用 实现python-gflags
,但它使用了类似的 API。
回答by Cro
回答by Nandani
After trying many times I found this to print all FLAGS key as well as actual value -
多次尝试后,我发现这可以打印所有 FLAGS 键以及实际值 -
for key in tf.app.flags.FLAGS.flag_values_dict():
print(key, FLAGS[key].value)
回答by Vedang Waradpande
The tf.app.flags
module is a functionality provided by Tensorflow to implement command line flags for your Tensorflow program. As an example, the code you came across would do the following:
该tf.app.flags
模块是 Tensorflow 提供的一项功能,用于为您的 Tensorflow 程序实现命令行标志。例如,您遇到的代码将执行以下操作:
flags.DEFINE_float('learning_rate', 0.01, 'Initial learning rate.')
The first parameter defines the name of the flag while the second defines the default value in case the flag is not specified while executing the file.
第一个参数定义标志的名称,而第二个参数定义默认值,以防在执行文件时未指定标志。
So if you run the following:
因此,如果您运行以下命令:
$ python fully_connected_feed.py --learning_rate 1.00
then the learning rate is set to 1.00 and will remain 0.01 if the flag is not specified.
那么学习率设置为 1.00,如果未指定标志,则学习率将保持为 0.01。
As mentioned in this article, the docs are probably not present because this might be something that Google requires internally for its developers to use.
如本文所述,文档可能不存在,因为这可能是 Google 内部要求其开发人员使用的内容。
Also, as mentioned in the post, there are several advantages of using Tensorflow flags over flag functionality provided by other Python packages such as argparse
especially when dealing with Tensorflow models, the most important being that you can supply Tensorflow specific information to the code such as information about which GPU to use.
此外,如帖子中所述,与其他 Python 包提供的标志功能相比,使用 Tensorflow 标志有几个优点,argparse
尤其是在处理 Tensorflow 模型时,最重要的是您可以向代码提供 Tensorflow 特定信息,例如信息关于使用哪个 GPU。
回答by Ahmed
At Google, they use flag systems to set default values for arguments. It's similar to argparse. They use their own flag system instead of argparse or sys.argv.
在 Google,他们使用标志系统来设置参数的默认值。它类似于 argparse。他们使用自己的标志系统而不是 argparse 或 sys.argv。
Source: I worked there before.
资料来源:我以前在那里工作过。
回答by mikey
For those who are still wondering, take a look at absl-py https://github.com/abseil/abseil-py.
对于那些仍然想知道的人,请查看 absl-py https://github.com/abseil/abseil-py。