答案 0 :(得分:2)
使用变量(trainable = False)来指示迭代,并在训练进行时增加变量。每当我们要将摘要添加到张量板时,我们都会运行该变量并将结果作为add_summary
传递给global_step
。
例如
# the variable mentioned before, counting training steps
train_steps = tf.get_variable('train_steps', shape=[], initializer=tf.constant_initializer(), trainable=False)
step_op = tf.assign(train_steps, train_steps + 1)
# update learn_step whenever optimization is performed
with tf.control_dependencies([step_op]):
opt_op = optimize(loss)
...
train_steps, _, summary = sess.run([train_steps, opt_op, merged_op], feed_dict=feed_dict)
# write summary to tensorboard
writer.add_summary(summary, train_steps)
# save model
saver.save(sess, filename)