gpt4 book ai didi

python - 张量板 : How to view model summary?

转载 作者:太空宇宙 更新时间:2023-11-03 20:12:25 25 4
gpt4 key购买 nike

问题陈述

  • 运行具有多种配置的模型并比较图表。根据绘图分析,选择配置。

在上面的语句中,我能够使用模型的名称来绘制模型的多次运行。现在我还需要 Tensorboard 来显示每次运行的模型配置/摘要。

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

This is how tensorboard looks like

最佳答案

您可以使用 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 表现得很好(从任一版本编写)。结果看起来像这样:

TensorBoard result

关于python - 张量板 : How to view model summary?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58642687/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com