18. Use the tf.app.flags interface to define command line parameters

1. Use the tf.app.flags interface to define command line parameters

  • As we all know, deep learning has a lot of Hyperparameterneeds to be tuned, TensorFlow uses the python-gflagsproject at the bottom, and then encapsulates it into an tf.app.flagsinterface
  • Using the tf.app.flagsinterface, it is very convenient to call the built- DEFINE_string, DEFINE_boolean, DEFINE_integer, DEFINE_floatin command-line parameters of different types and their default values. In actual projects, command-line parameters are generally defined in advance , as shown below:
# coding: utf-8
# filename: flags.py
import tensorflow as tf

# 定义一个全局对象来获取参数的值,在程序中使用(eg:FLAGS.iteration)来引用参数
FLAGS = tf.app.flags.FLAGS

# 定义命令行参数,第一个是:参数名称,第二个是:参数默认值,第三个是:参数描述
tf.app.flags.DEFINE_integer("iteration", 200000, "Iterations to train [2e5]")
tf.app.flags.DEFINE_integer("disp_freq", 1000, "Display the current results every display_freq iterations [1e3]")
tf.app.flags.DEFINE_integer("save_freq", 2000, "Save the checkpoints every save_freq iterations [2e3]")
tf.app.flags.DEFINE_float("learning_rate", 0.001, "Learning rate of for adam [0.001]")
tf.app.flags.DEFINE_integer("train_batch_size", 64, "The size of batch images [64]")
tf.app.flags.DEFINE_integer("val_batch_size", 100, "The size of batch images [100]")
tf.app.flags.DEFINE_integer("height", 48, "The height of image to use. [48]")
tf.app.flags.DEFINE_integer("width", 160, "The width of image to use. [160]")
tf.app.flags.DEFINE_integer("depth", 3, "Dimension of image color. [3]")
tf.app.flags.DEFINE_string("data_dir", "/path/to/data_sets/", "Directory of dataset in the form of TFRecords.")
tf.app.flags.DEFINE_string("checkpoint_dir", "/path/to/checkpoint_save_dir/", "Directory name to save the checkpoints [checkpoint]")
tf.app.flags.DEFINE_string("model_name", "40w_grtr", "Model name. [40w_grtr]")
tf.app.flags.DEFINE_string("gpu_id", "0", "Which GPU to be used. [0]")
tf.app.flags.DEFINE_boolean("continue_train", False, "True for continue training.[False]")
tf.app.flags.DEFINE_boolean("per_image_standardization", True, "True for per_image_standardization.[True]")


# 定义主函数
def main(argv=None):  
    print(FLAGS.iteration)
    print(FLAGS.learning_rate)
    print(FLAGS.data_dir)
    print(FLAGS.continue_train)


# 执行main函数
if __name__ == '__main__':
    tf.app.run()  

The method of executing the program

1. Use the default parameters in the program

  • python flags.py

write picture description here

2. Change the default parameters in the program at the command line

  • python flags.py --iteration=500000 --learning_rate=0.01 --data_dir='/home/test/' --continue_train=True

write picture description here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325447156&siteId=291194637