2017-07-18 22 views
1

我正在研究張量流中的mnist example張量流中「FLAGS」的用途

我感到困惑與模塊FLAGS

# Basic model parameters as external flags. 
FLAGS = None 

在 「run_training」 funcition:

def run_training(): 
"""Train MNIST for a number of steps.""" 
# Tell TensorFlow that the model will be built into the default Graph. 
with tf.Graph().as_default(): 
# Input images and labels. 
images, labels = inputs(train=True, batch_size=FLAGS.batch_size, 
         num_epochs=FLAGS.num_epochs) 

有什麼用 「FLAGS.batch_size」 和 「FLAGS.num_epochs」 這裏的目的是什麼?我可以用一個像128這樣的常數來代替它嗎?

我在this site找到了類似的答案,但我仍然無法理解。

+1

您還沒有列入其中'FLAGS'被定義爲一些顯著的代碼。而且,你能解釋一下你不瞭解的其他問題嗎? –

回答

1

標誌通常用於解析命令行參數並保存輸入參數。你可以用常數來代替它們,但最好在標誌幫助下組織你的輸入參數。

1

對於mnist full_connect_reader的例子,實際上他們根本沒有使用tensorflow標誌。這裏的FLAGS只是作爲一個「全局變量」,它將在源代碼頁的按鈕中由「FLAGS,unparsed = parser.parse_known_args()」分配,並用於不同的功能。

的方式來使用tf.app.flags.FLAGS應該是:

import tensorflow as tf 

FLAGS = tf.app.flags.FLAGS 

tf.app.flags.DEFINE_integer('max_steps', 100, 
          """Number of batches to run.""") 
tf.app.flags.DEFINE_integer('num_gpus', 1, 
          """How many GPUs to use.""") 


def main(argv=None): 
    print(FLAGS.max_steps) 
    print(FLAGS.num_gpus) 

if __name__ == '__main__': 
    # the first param for argv is the program name 
    tf.app.run(main=main, argv=['tensorflow_read_data', '--max_steps', '50', '--num_gpus', '20'])