gpt4 book ai didi

python - Keras model.summary() 对象到字符串

转载 作者:IT老高 更新时间:2023-10-28 21:56:30 24 4
gpt4 key购买 nike

我想编写一个带有神经网络超参数和模型架构的 *.txt 文件。是否可以将对象 model.summary() 写入我的输出文件?

(...)
summary = str(model.summary())
(...)
out = open(filename + 'report.txt','w')
out.write(summary)
out.close

正如您在下面看到的那样,我碰巧得到“无”。

Hyperparameters
=========================

learning_rate: 0.01
momentum: 0.8
decay: 0.0
batch size: 128
no. epochs: 3
dropout: 0.5
-------------------------

None
val_acc: 0.232323229313
val_loss: 3.88496732712
train_acc: 0.0965207634216
train_loss: 4.07161939425
train/val loss ratio: 1.04804469418

知道如何处理吗?

最佳答案

使用我的 Keras 版本 (2.0.6) 和 Python (3.5.0),这对我有用:

# Create an empty model
from keras.models import Sequential
model = Sequential()

# Open the file
with open(filename + 'report.txt','w') as fh:
# Pass the file handle in as a lambda function to make it callable
model.summary(print_fn=lambda x: fh.write(x + '\n'))

这会将以下行输出到文件中:

_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
Total params: 0
Trainable params: 0
Non-trainable params: 0
_________________________________________________________________

关于python - Keras model.summary() 对象到字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41665799/

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