我有2个脚本:
脚本2使用argparse
定义其标志。
标志logdir在script2中定义,但是需要在script1中设置值。
它由tensorboard.main.run_main
parser.add_argument(
'--logdir',
metavar='PATH',
type=str,
default='',
help='''\
我的代码:
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from threading import Thread
import logging
import os
import tensorboard.main as tb_main
import tensorflow as tf
# Environment variable containing port to launch TensorBoard on, set by TonY.
TB_PORT_ENV_VAR = 'TB_PORT'
tf.flags.DEFINE_string('data_dir', '/tmp/', 'Directory for storing input data')
tf.flags.DEFINE_string('working_dir', '/tmp/working_dir', 'Directory under which events and output will be stored (in separate subdirectories).')
# Training parameters
tf.flags.DEFINE_integer("steps", 2500, "The number of training steps to execute.")
tf.flags.DEFINE_integer("batch_size", 64, "The batch size per step.")
# tf.flags.DEFINE_string('logdir', '/tmp/input_data',
# 'Directory for storing input data')
FLAGS = tf.flags.FLAGS
def start_tensorboard(checkpoint_dir):
FLAGS.logdir = checkpoint_dir
if TB_PORT_ENV_VAR in os.environ:
FLAGS.port = os.environ[TB_PORT_ENV_VAR]
tb_thread = Thread(target=tb_main.run_main)
tb_thread.daemon = True
logging.info(
"Starting TensorBoard with --logdir=" + checkpoint_dir + " in daemon "
"thread...")
tb_thread.start()
def main(_):
# Create some variables.
start_tensorboard(FLAGS.working_dir)
v1 = tf.get_variable("v1", shape=[3], initializer=tf.zeros_initializer)
v2 = tf.get_variable("v2", shape=[5], initializer=tf.zeros_initializer)
inc_v1 = v1.assign(v1 + 1)
dec_v2 = v2.assign(v2 - 1)
# Add an op to initialize the variables.
init_op = tf.global_variables_initializer()
# Add ops to save and restore all the variables.
saver = tf.train.Saver()
# Later, launch the model, initialize the variables, do some work, and save
# the
# variables to disk.
with tf.Session() as sess:
sess.run(init_op)
# Do some work with the model.
inc_v1.op.run()
dec_v2.op.run()
# Save the variables to disk.
save_path = saver.save(sess, "/tmp/model.ckpt")
print("Model saved in path: %s" % save_path)
if __name__ == '__main__':
tf.app.run()
由于在第一个脚本中未定义我的标志而导致的错误是:
absl.flags._exceptions.UnrecognizedFlagError: Unknown command line flag 'logdir'
如果我定义了它(在上面评论),我得到:
absl.flags._exceptions.DuplicateFlagError: The flag 'help' is defined twice. First from tensorflow.python.platform.app, Second from absl.app. Description from first occurrence: show this help