如何防止摘要(或范围)"proliferation" 从 tf.scalar_summary 迁移到 tf.summary.scalar?
How do I prevent summary (or scope) "proliferation" migrating from tf.scalar_summary to tf.summary.scalar?
返回 before 2016-11-30 我可以使用类似下面的 TensorFlow/TensorBoard 代码来创建一个包含变量 'global_step_at_epoch' 的范围,该变量显示在我的模型运行的每个时期达到的全局步骤.
但是自从将 scalar_summary
替换为 summary.scalar
,如下所示,我为每个纪元获得了一个新的范围。因此,在 n
个纪元完成后,我得到范围和 TensorBoard 面板,用于 'global_step_at_epoch'、'global_step_at_epoch_1'、'global_step_at_epoch_2'、... 'global_step_at_epoch_n',每个都有一个点。
我如何从 scalar_summary
迁移到 summary.scalar
以便下面的代码(或类似代码)生成将所有单独的范围合并为一个范围,如 scalar_summary
曾经做过什么?
global_step = tf.Variable(0, trainable=False, name='global_step')
test_writer = tf.summary.FileWriter(...)
with tf.Session(config=tf.ConfigProto(allow_soft_placement=True,
log_device_placement=FLAGS.log_device_placement)) as sess:
sess.run(tf.initialize_all_variables())
test_writer.add_summary(sess.run(tf.summary.scalar('global_step_at_epoch', 0)), 0)
for ep in range(epochs):
mini_batches = ...
for mini_batch in mini_batches:
gs = int(sess.run(global_step))
test_writer.add_summary(sess.run(tf.summary.scalar('global_step_at_epoch', gs)), ep + 1)
我不确定它以前是如何工作的,但目前摘要的使用与其他节点的使用非常一致,因为您在构建阶段创建一次摘要,然后重复调用它在 Session
训练期间。
将此应用到您的示例中,可以得到:
global_step = tf.Variable(0, trainable=False, name='global_step')
test_writer = tf.summary.FileWriter(...)
# create summary op once here
gs_summary = tf.summary.scalar('global_step_at_epoch', global_step)
with tf.Session(config=tf.ConfigProto(allow_soft_placement=True,
log_device_placement=FLAGS.log_device_placement)) as sess:
sess.run(tf.initialize_all_variables())
test_writer.add_summary(sess.run(gs_summary), 0)
for ep in range(epochs):
mini_batches = ...
for mini_batch in mini_batches:
gs = int(sess.run(global_step))
test_writer.add_summary(sess.run(gs_summary), ep + 1)
这已经可以通过 tf.name_scope
上下文管理器来完成,方法是在第一次使用管理器时命名该管理器,并在您希望将新项目添加到现有范围时再次调用它(没有 "proliferation"):
with tf.name_scope('foo') as a_scope:
# some graph elements A
# Elsewhere ...
with tf.name_scope(a_scope):
# additional graph elements B
这会将 A 和 B 元素放在一个范围内 'foo'
,而
with tf.name_scope('foo') as:
# some graph elements A
with tf.name_scope('foo'):
# additional graph elements B
将元素分别放在不同的范围内,foo
和 foo_1
。
返回 before 2016-11-30 我可以使用类似下面的 TensorFlow/TensorBoard 代码来创建一个包含变量 'global_step_at_epoch' 的范围,该变量显示在我的模型运行的每个时期达到的全局步骤.
但是自从将 scalar_summary
替换为 summary.scalar
,如下所示,我为每个纪元获得了一个新的范围。因此,在 n
个纪元完成后,我得到范围和 TensorBoard 面板,用于 'global_step_at_epoch'、'global_step_at_epoch_1'、'global_step_at_epoch_2'、... 'global_step_at_epoch_n',每个都有一个点。
我如何从 scalar_summary
迁移到 summary.scalar
以便下面的代码(或类似代码)生成将所有单独的范围合并为一个范围,如 scalar_summary
曾经做过什么?
global_step = tf.Variable(0, trainable=False, name='global_step')
test_writer = tf.summary.FileWriter(...)
with tf.Session(config=tf.ConfigProto(allow_soft_placement=True,
log_device_placement=FLAGS.log_device_placement)) as sess:
sess.run(tf.initialize_all_variables())
test_writer.add_summary(sess.run(tf.summary.scalar('global_step_at_epoch', 0)), 0)
for ep in range(epochs):
mini_batches = ...
for mini_batch in mini_batches:
gs = int(sess.run(global_step))
test_writer.add_summary(sess.run(tf.summary.scalar('global_step_at_epoch', gs)), ep + 1)
我不确定它以前是如何工作的,但目前摘要的使用与其他节点的使用非常一致,因为您在构建阶段创建一次摘要,然后重复调用它在 Session
训练期间。
将此应用到您的示例中,可以得到:
global_step = tf.Variable(0, trainable=False, name='global_step')
test_writer = tf.summary.FileWriter(...)
# create summary op once here
gs_summary = tf.summary.scalar('global_step_at_epoch', global_step)
with tf.Session(config=tf.ConfigProto(allow_soft_placement=True,
log_device_placement=FLAGS.log_device_placement)) as sess:
sess.run(tf.initialize_all_variables())
test_writer.add_summary(sess.run(gs_summary), 0)
for ep in range(epochs):
mini_batches = ...
for mini_batch in mini_batches:
gs = int(sess.run(global_step))
test_writer.add_summary(sess.run(gs_summary), ep + 1)
这已经可以通过 tf.name_scope
上下文管理器来完成,方法是在第一次使用管理器时命名该管理器,并在您希望将新项目添加到现有范围时再次调用它(没有 "proliferation"):
with tf.name_scope('foo') as a_scope:
# some graph elements A
# Elsewhere ...
with tf.name_scope(a_scope):
# additional graph elements B
这会将 A 和 B 元素放在一个范围内 'foo'
,而
with tf.name_scope('foo') as:
# some graph elements A
with tf.name_scope('foo'):
# additional graph elements B
将元素分别放在不同的范围内,foo
和 foo_1
。