Temak
Temak

Reputation: 3009

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.

Upvotes: 6

Views: 5854

Answers (2)

rrobby86
rrobby86

Reputation: 1464

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)

Upvotes: 9

Prasad
Prasad

Reputation: 6034

You have to make use of tf.summary.text module. Check this simple example below:

summary_op1 = tf.summary.text('tag1', tf.convert_to_tensor('Tag1: Random Text 1'))
summary_op2 = tf.summary.text('tag2', tf.convert_to_tensor('Tag2: Random Text 2'))
summary_op3 = tf.summary.text('tag3', tf.convert_to_tensor('Tag3: Random Text 3'))

with tf.Session() as sess:
    summary_writer = tf.summary.FileWriter('./Tensorboard', sess.graph)
    for index, summary_op in enumerate([summary_op1, summary_op2, summary_op3]):
        text = sess.run(summary_op)
        summary_writer.add_summary(text, index)
summary_writer.close()

You will be getting an output something like this in Tensorboard:

enter image description here

Hope this answer helps you.

Upvotes: 4

Related Questions