Tensorflow project --FLAGS = tf.flags.FLAGS

  I recently CycleGAN code, see the code, there FLAGS = tf.flags.FLAGS and other statements, do not understand, I found the search, such statements are common in the framework of the project's use Tensorflow. When looking at the code and explain, to find a blogger on this part of the only sort, plus examples are explained very clearly, so it is applied directly to the author of the article.


Content includes the following several several functions we often see:

①tf.flags.DEFINE_xxx()

②FLAGS = tf.flags.FLAGS

③FLAGS._parse_flags()

simply put:

   To help us add optional command line arguments. That parameter can not modify the source code is repeated, but using the select function can set or modify the parameters to run the program from the command line.

For chestnut:

Train.py small part of the code program file is as follows:

 1 FLAGS = tf.flags.FLAGS
 2 
 3 tf.flags.DEFINE_string('name', 'default', 'name of the model')
 4 tf.flags.DEFINE_integer('num_seqs', 100, 'number of seqs in one batch')
 5 tf.flags.DEFINE_integer('num_steps', 100, 'length of one seq')
 6 tf.flags.DEFINE_integer('lstm_size', 128, 'size of hidden state of lstm')
 7 tf.flags.DEFINE_integer('num_layers', 2, 'number of lstm layers')
 8 tf.flags.DEFINE_boolean('use_embedding', False, 'whether to use embedding')
 9 tf.flags.DEFINE_integer('embedding_size', 128, 'size of embedding')
10 tf.flags.DEFINE_float('learning_rate', 0.001, 'learning_rate')
11 tf.flags.DEFINE_float('train_keep_prob', 0.5, 'dropout rate during training')
12 tf.flags.DEFINE_string('input_file', '', 'utf8 encoded text file')
13 tf.flags.DEFINE_integer('max_steps', 100000, 'max steps to train')
14 tf.flags.DEFINE_integer('save_every_n', 1000, 'save the model every n steps')
15 tf.flags.DEFINE_integer('log_every_n', 10, 'log to the screen every n steps')
16 tf.flags.DEFINE_integer('max_vocab', 3500, 'max char number')

We have to execute train.py file, at the command line, type the command line:

python train.py \
  --input_file data/shakespeare.txt  \         
  --name shakespeare \
  --num_steps 50 \
  --num_seqs 32 \
  --learning_rate 0.01 \
  --max_steps 20000

By entering a different filename argument to quickly complete the operation in assistant training set and loading a program, you do not need to enter the source code changes.


 

Hands-on

Now we have the following code:

. 1  Import tensorflow TF AS
 2  # take the code portion of the experiment 
. 3 tf.flags.DEFINE_integer ( ' num_seqs ' , 100, ' Number of Seqs in One BATCH ' )
 . 4 tf.flags.DEFINE_integer ( ' for num_steps ' , 100, ' One of SEQ length ' )
 . 5 tf.flags.DEFINE_integer ( ' lstm_size ' , 128, ' size of hidden State of LSTM ' )
 . 6  
. 7  # is determined by the following contents print () function 
. 8 the FLAGS = tf.flags.FLAGS# The FLAGS command line parameters stored data 
. 9 FLAGS._parse_flags () # parse it into a dictionary stored in the FLAGS .__ flags 
10  Print (the FLAGS. __Flags )
 . 11  
12 is  Print (FLAGS.num_seqs)
 13 is  
14  Print ( " \ nParameters: " )
 15  for attr, value in the sorted (the FLAGS. __flags .items ()):
 16      Print ( " {} = {} " .format (attr.upper (), value))
 . 17  Print ( "" )

Author: , link: https://blog.csdn.net/zzq060143/article/details/81952848

Thank author sharing.

Guess you like

Origin www.cnblogs.com/happy-sir/p/11486272.html