Tensorflow摘要:如何在评估之前获取项目

时间:2016-01-09 12:55:58

标签: python tensorflow

我想获得一个使用tensorflow中的模型创建的摘要列表。我知道one can get a dictionary of key:value of evaluated summaries

在从summary_proto对象评估汇总密钥之前,可以获取汇总密钥列表吗?我需要它来初始化列表字典,我将存储每个时代的摘要,而不是存储字典列表。

summary_proto = tf.Summary()

1 个答案:

答案 0 :(得分:2)

使用以下代码按需初始化列表字典可能最简单:

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)

但是,要回答原始问题,可以通过评估各个摘要操作的tag 输入来获取单个标记(这很可能不依赖于结果培训):

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] = []