The use of Tf.app.flags.FLAGS, mainly when executing the program with the command line, need to pass some parameters, the code is as follows:
Create a new file named: app_flags.py.
#coding: Utf-8
Import TensorFlow as TF
FLAGS = Tf.app.flags.FLAGS
Tf.app.flags.DEFINE_string ("Train_data_path", "/home/libo3/train.txt", "Training Data dir")
Tf.app.flags.DEFINE_string ("Log_dir", "./logs", "the Log dir")
Tf.app.flags.DEFINE_integer ("Max_sentence_len", "Max num of tokens per query")
Tf.app.flags.DEFINE_integer ("Embedding_size", "Embedding Size")
Tf.app.flags.DEFINE_float ("Learning_rate", 0.001, "learning rate")
def main (UNUSED_ARGV):
Train_data_path = Flags.train_data_path
Print ("Train_data_path", Train_data_path)
Max_sentence_len = Flags.max_sentence_len
Print ("Max_sentence_len", Max_sentence_len)
Embdeeing_size = Flags.embedding_size
Print ("Embedding_size", Embdeeing_size)
ABC = tf.add (Max_sentence_len, Embdeeing_size)
init = Tf.global_variables_initializer ()
#with TF. Session () as Sess:
#sess. Run (init)
#print ("abc", Sess.run (ABC))
SV = Tf.train.Supervisor (Logdir=flags.log_dir, Init_op=init)
With Sv.managed_session () as Sess:
Print ("ABC:", Sess.run (ABC))
# Sv.saver.save (Sess, "/home/yongcai/tmp/")
# This ensures that the main function is not executed if the file is import from another file
if __name__ = = ' __main__ ':
Tf.app.run () # Parse command-line arguments, call main function main (SYS.ARGV)
Call Method:
The parameters can be modified as required.
- Python app_flags.py--train_data_path < absolute path train.txt>--max_sentence_len --embedding_size 100-- Learning_rate 0.05
If this is called:
- Python app_flags.py
The parameters in the default in the program are automatically invoked when the program executes.
TensorFlow API _ 2 (Tf.app.flags.FLAGS)