Search code examples
loggingtensorflowtensorboard

How do I construct an arbitrary text summary in TensorFlow?


I want to log some arbitrary strings in TensorBoard.

I know how to do it for arbitrary scalars:

from tensorflow.core.framework import summary_pb2
value = summary_pb2.Summary.Value(tag='Accuracy', simple_value=0.95)
my_summary = summary_pb2.Summary(value=[value])

summary_writer = tf.summary.FileWriter()
summary_writer.add_summary(summary)

But how to do the same thing but for arbitrary text summary?
Something like (which doesn't exist):

value = summary_pb2.Summary.Text(tag='MyTag', str='Arbitrary text come here')

UPD: Note that I provided an example how to create an arbitrary scalar summary without calling session.run(...). I want to be able to do it for text as well.


Solution

  • I have been searching for an answer, too. Peeking at some source code for TensorFlow/Board, I found a way which seems to work (I don't know whether a simpler solution exists).

    value = "Random text"
    text_tensor = tf.make_tensor_proto(value, dtype=tf.string)
    meta = tf.SummaryMetadata()
    meta.plugin_data.plugin_name = "text"
    summary = tf.Summary()
    summary.value.add(tag="whatever", metadata=meta, tensor=text_tensor)
    summary_writer.add_summary(summary)