tf.app.flags.FLAGS 的作用
tensorflow 中定義了 tf.app.flags.FLAGS ,用於接受從終端傳入的命令行參數。
tf.app.flags.FLAGS 類型
tf中每個合法類型都有對應的 “DEFINE_xxx”函數。常用:
參數說明
“DEFINE_xxx”函數帶3個參數,分別是變量名稱,默認值;
用法
tf.app.flags.DEFINE_string('ckpt_path',
'model/model.ckpt-100000',
'''Checkpoint directory to restore''')
定義一個名稱是 “ckpt_path” 的變量;
默認值是 ckpt_path = ‘model/model.ckpt-100000’;
描述信息表明這是一個用於保存節點信息的路徑。
使用示例
# -*- coding=utf-8 -*-
import tensorflow as tf
FLAGS = tf.app.flags.FLAGS
tf.app.flags.DEFINE_string('ckpt_path', 'model/model.ckpt-100000', '''模型保存路徑''')
tf.app.flags.DEFINE_float('learning_rate',0.0001,'''初始學習率''')
tf.app.flags.DEFINE_integer('train_steps', 50000, '''總的訓練輪數''')
tf.app.flags.DEFINE_boolean('is_use_gpu', False, '''是否使用GPU''')
print ('模型保存路徑: {}'.format(FLAGS.ckpt_path))
print ('初始學習率: {}'.format(FLAGS.learning_rate))
print ('總的訓練次數: {}'.format(FLAGS.train_steps))
print ('是否使用GPU: {}'.format(FLAGS.is_use_gpu))
使用 ‘-h’ 指令查看幫助信息
python test.py -h
按默認設置執行程序:
傳入用戶自定義的命令行參數:
python flags_test.py --ckpt_path abc/cba --learning_rate 0.001 --train_steps 10000 --is_use_gpu True