在破折号上显示模型摘要

Displaying model summary on dash

我有一个经过训练的 Keras 模型并将其存储在变量 'model' 中。 当我打印 model.summary() 时,我得到类似 table 和列 headers

的东西
Layer (type)        Output Shape         Param#       Connected to

如何在破折号中显示此 table?我需要先把它变成某种数据框吗?或者我可以调用仪表板上打印的摘要和输出吗?

我设法用这段代码把它变成了一个字符串。我的代码:

strlist = []
model.summary(print_fn=lambda x: strlist.append(x))
str_sum = "\n".join(strlist)

app = dash.Dash()

app.layout = html.Div(children=[
            html.Div(str_sum)
     ])

if __name__ == '__main__':
  app.run_server() 

但是我在仪表板上看到的内容看起来杂乱无章而且顺序不对 它看起来像:

Model: "model"_________________________________Layer (type) Output Shape Param #
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =   
input_1 (InputLayer) [None, 256, 256, 3) 0 __________________________________
block1_conv1 (Conv2D) (None, 127, 127, 32) 864 input_1[0][0]

感谢一些意见!

Dash 将输出呈现为 html,因此您需要格外小心换行符。尝试用以下

替换您的 app.layout
app.layout = html.Div(html.P(
    children=str_sum, style={'whiteSpace': 'pre-wrap'}))

适合我: