dataframe - 神经网络摘要到数据框

标签 dataframe tensorflow keras neural-network

我们可以通过以下方式访问神经网络的摘要

model.summary()

但是有什么方法可以将这个结果转换成dataframe,这样我们就可以比较不同模型的特征了吗?

enter image description here

最佳答案

是的,您可以使用 print_fn 参数将输出保存为字符串,然后将其解析为 DataFrame:

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
import re
import pandas as pd

model = Sequential()
model.add(Dense(2, input_dim=1, activation='relu'))
model.add(Dense(1, activation='sigmoid'))

stringlist = []
model.summary(print_fn=lambda x: stringlist.append(x))
summ_string = "\n".join(stringlist)
print(summ_string) # entire summary in a variable

table = stringlist[1:-4][1::2] # take every other element and remove appendix

new_table = []
for entry in table:
    entry = re.split(r'\s{2,}', entry)[:-1] # remove whitespace
    new_table.append(entry)

df = pd.DataFrame(new_table[1:], columns=new_table[0])
print(df.head())

输出:

      Layer (type) Output Shape Param #
0    dense (Dense)    (None, 2)       4
1  dense_1 (Dense)    (None, 1)       3

关于dataframe - 神经网络摘要到数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63843093/

相关文章:

python - 在 Pandas 数据框中的任何列中删除具有 'question mark' 值的行

tensorflow - Tensorflow 中的 while_loop 错误

python - tf.split 的输出是什么?

python - 如何输出网络的第二层?

python - 如何从作为字典列表的 json 字符串中的值构建数据框?

python - 计算 Pandas 数据框行中的非空单元格并将计数添加为列

python - 使用 tf.train.batch 时形状错误

GPU 上的 Tensorflow 比 CPU 上的慢

python - 在 Golang 的 Tensorflow 中打开带有嵌入层的 Keras 模型

python - Pyspark:重命名 DataFrame 列中的字典键