1

I would like to get a list of summaries created with a model in tensorflow. I know that one can get a dictionary of key:value of evaluated summaries.

Can one get a list of summary keys before evaluating them from summary_proto object? I need it to initialize a dictionary of lists where I will store summaries from each epoch, instead of storing list of dictionaries.

summary_proto = tf.Summary()
Community
  • 1
  • 1
Dima Lituiev
  • 12,544
  • 10
  • 41
  • 58

1 Answers1

3

It would probably be easiest to initialize your dictionary of lists on demand, using the following code:

train_op = ...
summary_op = tf.merge_all_summaries()

summaries = {}

sess = tf.Session()

for _ in range(NUM_EPOCHS):
  _, summary_str = sess.run([train_op, summary_op], feed_dict=feed_dict)
  summary_proto = tf.Summary()
  summary_proto.ParseFromString(summary_str)

  for val in summary_proto.value:
    try:
      list_for_tag = summaries[val.tag]
    except KeyError:
      list_for_tag = []
      summaries[val.tag] = list_for_tag

    # Assuming all summaries are scalars.
    list_for_tag.append(val.simple_value)

However, to answer your original question, it is possible to get the individual tags by evaluating the tag inputs to the individual summary ops (which most likely do not depend on the result of training):

summaries = {}

sess = tf.Session()

all_summary_tensors = tf.get_collection(tf.GraphKeys.SUMMARIES)

for summary_t in all_summary_tensors:
  tag_input = summary_t.op.inputs[0]  # The tag input is the 0th input.
  tags = sess.run(tag_input)

  if isinstance(tags, str):
    summaries[tags] = []
  else:
    for tag in tags.flatten():
      summaries[tag] = []
mrry
  • 125,488
  • 26
  • 399
  • 400