4

I am studying the mnist example in tensorflow.

I am confused with the module FLAGS

# Basic model parameters as external flags.
FLAGS = None

In the "run_training" funcition :

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)

What's the purpose of using "FLAGS.batch_size" and "FLAGS.num_epochs" here? Can I just replace it with a constant number like 128?

I found a similar answer in this site but I still can't understand.

JimReno
  • 43
  • 1
  • 5
  • 1
    You haven't included the code where `FLAGS` is defined to something significant. Moreover, can you explain what you don't understand from that other question? – E_net4 Jul 18 '17 at 09:19

2 Answers2

8

The flags are generally used to parse command line arguments and hold input parameters. You could replace them with constant numbers, but it is good practice to organise your input parameters with the help of flags.

Agost Biro
  • 2,709
  • 1
  • 20
  • 33
7

For the example of mnist full_connect_reader, actually they haven't use the tensorflow FLAGS at all. The FLAGS here, just serves as a "global variable", which will be assigned by "FLAGS, unparsed = parser.parse_known_args()" in the button of the source code page, and used in different functions.

The way to use tf.app.flags.FLAGS should be:

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'])
BryanF
  • 111
  • 1
  • 5