TensorFlow中的tf.app.flags的目的是什么?

问题:TensorFlow中的tf.app.flags的目的是什么?

我在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.')

tensorflow/tensorflow/g3doc/tutorials/mnist/fully_connected_feed.py

但我找不到有关的用法的任何文档tf.app.flags

我发现该标志的实现在 tensorflow/tensorflow/python/platform/default/_flags.py

显然,这tf.app.flags是以某种方式用于配置网络的,所以为什么在API文档中没有呢?谁能解释这是怎么回事?

I am reading some example codes in Tensorflow, I found following code

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

But I can’t find any docs about this usage of tf.app.flags.

And I found the implementation of this flags is in the 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?


回答 0

tf.app.flags模块目前是python-gflags的 一个瘦包装,因此该项目文档是如何使用它的最佳资源argparse,它实现了一部分功能python-gflags

请注意,该模块当前已打包为方便编写演示应用程序使用,从技术上讲,它不是公共API的一部分,因此将来可能会更改。

我们建议您使用argparse或任何您喜欢的库来实现自己的标志解析。

编辑:tf.app.flags模块实际上并未使用实现python-gflags,但它使用了类似的API。

The tf.app.flags module is presently a thin wrapper around python-gflags, so the documentation for that project is the best resource for how to use it argparse, which implements a subset of the functionality in 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.

We recommend that you implement your own flag parsing using argparse or whatever library you prefer.

EDIT: The tf.app.flags module is not in fact implemented using python-gflags, but it uses a similar API.


回答 1

tf.app.flags模块是Tensorflow提供的功能,用于为Tensorflow程序实现命令行标志。例如,您遇到的代码将执行以下操作:

flags.DEFINE_float('learning_rate', 0.01, 'Initial learning rate.')

第一个参数定义标志的名称,第二个参数定义默认值,以防执行文件时未指定标志。

因此,如果运行以下命令:

$ python fully_connected_feed.py --learning_rate 1.00

那么学习率将设置为1.00,如果未指定该标志,则将保持0.01。

本文所述,文档可能不存在,因为这可能是Google内部要求开发人员使用的文档。

此外,如文章中所述,使用Tensorflow标志比其他Python软件包提供的标志功能有多个优势,例如argparse在处理Tensorflow模型时尤其如此,最重要的是可以向代码提供Tensorflow特定信息,例如信息有关使用哪个GPU的信息。

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:

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.

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.

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.


回答 2

在Google,他们使用标记系统来设置参数的默认值。它类似于argparse。他们使用自己的标记系统,而不是argparse或sys.argv。

资料来源:我以前在那里工作过。

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.

Source: I worked there before.


回答 3

使用时tf.app.run(),可以使用方便地在线程之间传输变量tf.app.flags。请参阅此内容以进一步使用tf.app.flags

When you use tf.app.run(), you can transfer the variable very conveniently between threads using tf.app.flags. See this for further usage of tf.app.flags.


回答 4

经过多次尝试后,我发现它可以打印所有FLAGS键以及实际值-

for key in tf.app.flags.FLAGS.flag_values_dict():

  print(key, FLAGS[key].value)

After trying many times I found this to print all FLAGS key as well as actual value –

for key in tf.app.flags.FLAGS.flag_values_dict():

  print(key, FLAGS[key].value)