TensorFlow API _ 2 (Tf.app.flags.FLAGS)

Source: Internet
Author: User

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.

    1. 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:

    1. 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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.