TensorFlow:了解 tf.summary.scalar 中的“collections”参数

TensorFlow: Understanding the `collections` argument in tf.summary.scalar

我正在使用 TensorBoard,特别是 tf.summary.scalar。在 documentation 中它有一个参数 collections=None,描述为:

collections: Optional list of graph collections keys. The new summary op is added to these collections. Defaults to [GraphKeys.SUMMARIES].

我不明白这个描述,collections是干什么用的。有人可以向我解释一下吗,也许可以为我指出一个很好的用例示例?

这是一个隐藏的gem!您可以向它提供您选择的标记摘要节点的字符串列表,例如

tf.summary.scalar('learning_rate', p_lr, collections=['train'])
tf.summary.scalar('loss', t_loss, collections=['train', 'test'])

然后按标签获取摘要,例如像这样:

s_training = tf.summary.merge_all('train')
s_test = tf.summary.merge_all('test')

我这样做是因为我经常想在验证阶段记录额外的信息;在上面的示例中,在评估(和编写)准确性时,我不必为学习率占位符 p_lr 提供值,例如 - 或者图的推理部分所依赖的任何东西。

提供(仅)自定义类别也有很好的副作用,例如从 Supervisor 中隐藏节点。如果你真的想控制你写摘要的确切时间(例如,在 Supervisor 的情况下使用 sv.summary_computed()),这是一个简单的方法。