Gaurav Gupta
Gaurav Gupta

Reputation: 4691

Tensorboard: How to view model summary?

Problem Statement

In the above statement, I am able to plot multiple runs of the model with their names. Now I need Tensorboard to show configuration/summary of the model against each run as well.

Question is Would it be possible to view the model summary in the Tensorboard for corresponding to each run of the model?

This is how tensorboard looks like

Upvotes: 2

Views: 2208

Answers (1)

javidcf
javidcf

Reputation: 59701

You can use a text summary with the model summary, something like this:

import tensorflow as tf

# Get model summary as a string
def get_summary_str(model):
    lines = []
    model.summary(print_fn=lines.append)
    # Add initial spaces to avoid markdown formatting in TensorBoard
    return '    ' + '\n    '.join(lines)

# Write a string to TensorBoard (1.x)
def write_string_summary_v1(writer, s):
    with tf.Graph().as_default(), tf.Session() as sess:
        summary = tf.summary.text('Model configuration', tf.constant(s))
        writer.add_summary(sess.run(summary))

# Write a string to TensorBoard (2.x)
def write_string_summary_v2(writer, s):
    with writer.as_default():
        tf.summary.text('Model configuration', s, step=0)

# Model 1
inp1 = tf.keras.Input(shape=(10,))
out1 = tf.keras.layers.Dense(100)(inp1)
model1 = tf.keras.Model(inputs=inp1, outputs=out1)
# Model 2
inp2 = tf.keras.Input(shape=(10,))
out2 = tf.keras.layers.Dense(200)(inp2)
out2 = tf.keras.layers.Dense(100)(out2)
model2 = tf.keras.Model(inputs=inp2, outputs=out2)
# Write model summaries to TensorBoard (1.x)
with tf.summary.FileWriter('log/model1') as writer1:
    write_string_summary_v1(writer1, get_summary_str(model1))
with tf.summary.FileWriter('log/model2') as writer2:
    write_string_summary_v1(writer2, get_summary_str(model2))
# Write model summaries to TensorBoard (2.x)
writer1 = tf.summary.create_file_writer('log/model1')
write_string_summary_v2(writer1, get_summary_str(model1))
writer2 = tf.summary.create_file_writer('log/model2')
write_string_summary_v2(writer2, get_summary_str(model2))

For some reason, writing the summary in 2.0 works fine, but 2.0 TensorBoard fails when I try to show it, which I think is a bug. However, TensorBoard 1.15 shows it just fine (written from either version). The result would look something like this:

TensorBoard result

Upvotes: 2

Related Questions