4

I am working with TensorBoard, specifically tf.summary.scalar. In the documentation it has an arugment collections=None, which is described as:

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

I don't understand this description, and what collections is used for. Can someone please explain this to me, and perhaps point me towards a good example use-case?

Toke Faurby
  • 5,788
  • 9
  • 41
  • 62

1 Answers1

8

It's a hidden gem! You can provide it a list of strings of your choice that label the summary node, e.g.

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

and then fetch the summaries by their label, e.g. like so:

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

I'm doing it like that because I often want to log extra information during the validation phase; in the above example, I don't have to provide a value for the learning rate placeholder p_lr when evaluating (and writing) the accuracy, for example - or anything really that the inference part of the graph relies on.

Providing (only) custom categories also has the nice side effect of hiding the node from Supervisor, for example. If you really want to have control over when exactly you write a summary (e.g. using sv.summary_computed() in case of Supervisor), that's an easy way to go.

sunside
  • 8,069
  • 9
  • 51
  • 74
  • 2
    Great description! `collection`s can be used more generally when ever you want to access all of something that are spread out all over e.g. if you want all biases in a graph this could be used. Discussed [here](http://stackoverflow.com/questions/36533723/tensorflow-get-all-variables-in-scope). See `get_collection` documentation [here](https://www.tensorflow.org/versions/r0.11/api_docs/python/framework/#get_collection). – Toke Faurby Feb 01 '17 at 15:49