JimReno
JimReno

Reputation: 43

What's the purpose of "FLAGS" in tensorflow

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.

Upvotes: 4

Views: 6452

Answers (2)

BryanF
BryanF

Reputation: 131

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'])

Upvotes: 7

Agost Biro
Agost Biro

Reputation: 2839

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.

Upvotes: 8

Related Questions