1. 程式人生 > >flags的解析方法

flags的解析方法

1、使用argparse包

import argparse
parser = argparse.ArgumentParser()
parser.add_argument(
      '--learning_rate',
      type=float,
      default=0.01,
      help='Initial learning rate.'
  )
parser.add_argument(
      '--max_steps',
      type=int,
      default=2000,
      help='Number of steps to run trainer.'
) FLAGS, unparsed = parser.parse_known_args() # 輸出時, print(FLAGS.learning_rate) #輸出=0.01

2、使用tf的包(1)

import tensorflow as tf
FLAGS = tf.app.flags.FLAGS

tf.app.flags.DEFINE_string('train_dir', 'tmp/train/',
                           """Directory where to write event logs """
                           """and checkpoint."""
) tf.app.flags.DEFINE_integer('max_steps', 700, """Number of batches to run.""") tf.app.flags.DEFINE_boolean('log_device_placement', False, """Whether to log device placement.""") print(FLAGS.max_steps) # 輸出=700

3、使用tf的包(2)——簡潔用法

from tensorflow import flags
flags.DEFINE
_float("threshold", 0.1, "detection threshold") flags.DEFINE_string("model", "", "configuration of choice") flags.DEFINE_string("trainer", "rmsprop", "training algorithm") FLAGS = flags.FLAGS print(FLAGS.threshold) # 輸出=0.1