"FLAGS" 在tensorflow中的目的是什么
What's the purpose of "FLAGS" in tensorflow
我正在研究tensorflow中的mnist example。
我对模块 FLAGS 感到困惑
# Basic model parameters as external flags.
FLAGS = None
在"run_training"函数中:
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中找到了类似的答案,但我还是看不懂。
标志通常用于解析命令行参数和保存输入参数。您可以用常量替换它们,但最好在标志的帮助下组织您的输入参数。
以mnist为例full_connect_reader,实际上他们根本没有使用tensorflow FLAGS。这里的FLAGS,只是作为一个"global variable",会在源代码页的按钮中被"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'])
我正在研究tensorflow中的mnist example。
我对模块 FLAGS 感到困惑
# Basic model parameters as external flags.
FLAGS = None
在"run_training"函数中:
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中找到了类似的答案,但我还是看不懂。
标志通常用于解析命令行参数和保存输入参数。您可以用常量替换它们,但最好在标志的帮助下组织您的输入参数。
以mnist为例full_connect_reader,实际上他们根本没有使用tensorflow FLAGS。这里的FLAGS,只是作为一个"global variable",会在源代码页的按钮中被"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'])