Set and Parse Command Line Arguments with Flags in TensorFlow – TensorFlow Tutorial

By | June 18, 2019

In tensorflow application, we often need pass some arguments to application, such as arguments batch_size, learning_rate, dimension_size etc. We can use to set and parse these arguments. Here is an example.

Set and parse arguments with in tensorflow.

The example code:

import numpy as np
import tensorflow as tf
#set arguments
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_unit',128,'Number of units in hidden layer 1')
flags.DEFINE_integer('batch_size',100,'Batch size')
flags.DEFINE_string('train_dir','mnist-data/','Directory to put the training data')
flags.DEFINE_boolean('fake_data', False, 'If true, use fake data for unit testing')

#get arguments
print 'learning_rate = ' + str(FLAGS.learning_rate)
print 'max_steps = ' + str(FLAGS.max_steps)
print 'hidden1_unit = ' + str(FLAGS.hidden1_unit)
print 'batch_size = ' + str(FLAGS.batch_size)
print 'train_dir = ' + FLAGS.train_dir
print 'fake_data = ' + str(FLAGS.fake_data)

The common used arguments in tensorflow is: float, integer, string and boolean, example above illustrates how to set and parse these arguments, you can learn how to do by this code.

If you do not pass any arguments, the application will use default values.

tensorflow flags arguments

If you pass some arguments, the application will use the new values.

tensorflow set flags arguments

Leave a Reply

Your email address will not be published. Required fields are marked *