How to manually create a tf.Summary()

后端 未结 3 1459
旧巷少年郎
旧巷少年郎 2020-12-04 11:37

I often want to log python variables --as opposed to tf tensors.

In the docs it says that \"you can pass a tf.Summary protocol buffer that you populate

相关标签:
3条回答
  • 2020-12-04 11:47

    You can create a tf.Summary object in your Python program and write it to the same tf.summary.FileWriter object that takes your TensorFlow-produced summaries using the SummaryWriter.add_summary() method.

    The tf.Summary class is a Python protocol buffer wrapper for the Summary protocol buffer. Each Summary contains a list of tf.Summary.Value protocol buffers, which each have a tag and a either a "simple" (floating-point scalar) value, an image, a histogram, or an audio snippet. For example, you can generate a scalar summary from a Python object as follows:

    writer = tf.train.SummaryWriter(...)
    value = 37.0
    summary = tf.Summary(value=[
        tf.Summary.Value(tag="summary_tag", simple_value=value), 
    ])
    writer.add_summary(summary)
    
    0 讨论(0)
  • 2020-12-04 11:47

    I needed to do many updates to the custom summary variable during training so I implemented mine like so:

    Before the loop:

    writer = tf.summary.FileWriter(log_folder)
    accuracy = None
    accuracy_summary = tf.Summary()
    accuracy_summary.value.add(tag='accuracy', simple_value=accuracy)
    

    Inside the loop:

    if i%20000 == 0:
        accuracy = get_accuracy()
        accuracy_summary.value[0].simple_value = accuracy
        writer.add_summary(accuracy_summary, i)
    

    I'm assuming the indexes to value are in the order in which the variables were added to the summary.

    0 讨论(0)
  • 2020-12-04 12:03

    If you want to log a python value you have to create a placeholder that have to be fed when running the tf.Summary op.

    Here's a code snipped

    value_ = tf.placeholder(tf.float32, [])
    summary_op = tf.scalar_summary("value_log", value_)
    my_python_variable = 10
    # define everything else you need...
    # ...
    with tf.Session() as sess:
        for i in range(0, 10):
            sess.run(summary_op, feed_dict={value_: my_python_variable*i})
    
    0 讨论(0)
提交回复
热议问题