问题陈述


运行具有多种配置的模型并比较图。基于图分析,选择一个配置。


在上面的陈述中,我能够用其名称绘制模型的多次运行。现在我需要Tensorboard来针对每次运行显示模型的配置/摘要。

问题是
是否可以在Tensorboard中查看与模型的每次运行相对应的模型摘要?

python - Tensorboard:如何查看模型摘要?-LMLPHP

最佳答案

您可以在模型摘要中使用text摘要,如下所示:

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))


由于某种原因,在2.0中编写摘要效果很好,但是当我尝试显示摘要时,2.0 TensorBoard失败,我认为这是一个错误。但是,TensorBoard 1.15可以很好地显示(从任一版本编写)。结果看起来像这样:

python - Tensorboard:如何查看模型摘要?-LMLPHP

关于python - Tensorboard:如何查看模型摘要?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58642687/

10-12 21:56