张量流中“ FLAGS”的目的是什么

Jim*_*eno 4 python mnist deep-learning tensorflow

我正在研究tensorflow中的mnist示例

我对FLAGS模块感到困惑

# Basic model parameters as external flags.
FLAGS = None
Run Code Online (Sandbox Code Playgroud)

在“ run_training”功能中:

def run_training():
"""Train MNIST for a number of steps."""
# Tell TensorFlow that the model will be built into the default Graph.
with tf.Graph().as_default():
# Input images and labels.
images, labels = inputs(train=True, batch_size=FLAGS.batch_size,
                        num_epochs=FLAGS.num_epochs)
Run Code Online (Sandbox Code Playgroud)

在这里使用“ FLAGS.batch_size”和“ FLAGS.num_epochs”的目的是什么?我可以将它替换为一个常数,例如128吗?

我在此站点中找到了类似的答案,但我仍然不明白。

Ago*_*iro 7

这些标志通常用于解析命令行参数并保留输入参数。您可以将它们替换为常数,但是在标记的帮助下组织输入参数是一种很好的做法。


小智 7

对于mnist full_connect_reader的示例,实际上他们根本没有使用过tensorflow FLAGS。这里的FLAGS只是用作“全局变量”,将由源代码页面按钮中的“ FLAGS,unparsed = parser.parse_known_args()”分配,并用于不同的功能。

使用tf.app.flags.FLAGS的方法应为:

import tensorflow as tf

FLAGS = tf.app.flags.FLAGS

tf.app.flags.DEFINE_integer('max_steps', 100,
                            """Number of batches to run.""")
tf.app.flags.DEFINE_integer('num_gpus', 1,
                            """How many GPUs to use.""")


def main(argv=None):
    print(FLAGS.max_steps)
    print(FLAGS.num_gpus)

if __name__ == '__main__':
  # the first param for argv is the program name
  tf.app.run(main=main, argv=['tensorflow_read_data', '--max_steps', '50', '--num_gpus', '20'])
Run Code Online (Sandbox Code Playgroud)