lmp*_*oto 36 python deep-learning keras
我想用神经网络超参数和模型架构编写一个*.txt文件.是否可以将对象model.summary()写入我的输出文件?
(...)
summary = str(model.summary())
(...)
out = open(filename + 'report.txt','w')
out.write(summary)
out.close
Run Code Online (Sandbox Code Playgroud)
碰巧我正在得到一个"无",你可以在下面看到.
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
Run Code Online (Sandbox Code Playgroud)
知道怎么处理吗?谢谢
win*_*i2k 38
使用我的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'))
Run Code Online (Sandbox Code Playgroud)
这会将以下行输出到文件:
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
Total params: 0
Trainable params: 0
Non-trainable params: 0
_________________________________________________________________
Run Code Online (Sandbox Code Playgroud)
小智 14
对我来说,这样做只是将模型摘要作为字符串获取:
stringlist = []
model.summary(print_fn=lambda x: stringlist.append(x))
short_model_summary = "\n".join(stringlist)
print(short_model_summary)
Run Code Online (Sandbox Code Playgroud)
ajb*_*ajb 13
如果要写入日志:
import logging
logger = logging.getLogger(__name__)
model.summary(print_fn=logger.info)
Run Code Online (Sandbox Code Playgroud)
Pas*_*asa 10
我知道 OP 已经接受了 winni2k 的答案,但由于问题标题实际上意味着将输出保存model.summary()到string,而不是文件,以下代码可能会帮助其他人来到此页面寻找(就像我一样)。
下面的代码是使用 TensorFlow 运行的,TensorFlow1.12.0是2.1.6-tfPython上的 Keras 附带的3.6.2。
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Activation
import io
# Example model
model = Sequential([
Dense(32, input_shape=(784,)),
Activation('relu'),
Dense(10),
Activation('softmax'),
])
def get_model_summary(model):
stream = io.StringIO()
model.summary(print_fn=lambda x: stream.write(x + '\n'))
summary_string = stream.getvalue()
stream.close()
return summary_string
model_summary_string = get_model_summary(model)
print(model_summary_string)
Run Code Online (Sandbox Code Playgroud)
产生(作为字符串):
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense (Dense) (None, 32) 25120
_________________________________________________________________
activation (Activation) (None, 32) 0
_________________________________________________________________
dense_1 (Dense) (None, 10) 330
_________________________________________________________________
activation_1 (Activation) (None, 10) 0
=================================================================
Total params: 25,450
Trainable params: 25,450
Non-trainable params: 0
_________________________________________________________________
Run Code Online (Sandbox Code Playgroud)
这不是最好的方法,但你可以做的一件事是重定向stdout:
orig_stdout = sys.stdout
f = open('out.txt', 'w')
sys.stdout = f
print(model.summary())
sys.stdout = orig_stdout
f.close()
Run Code Online (Sandbox Code Playgroud)
请参阅"如何使用python将'print'输出重定向到文件?"
一种选择(尽管不是model.summary的精确替代)是使用导出模型的配置model.get_config()。从文档:
model.get_config():返回包含模型配置的字典。该模型可以通过以下方式从其配置中重新实例化:Run Code Online (Sandbox Code Playgroud)config = model.get_config() model = Model.from_config(config) # or, for Sequential: model = Sequential.from_config(config)
我也偶然发现了同样的问题!有两种可能的解决方法:
to_json()模型的使用方法
summary = str(model.to_json())
Run Code Online (Sandbox Code Playgroud)
上面是你的情况。
否则,请使用keras_diagram中的ascii方法
from keras_diagram import ascii
summary = ascii(model)
Run Code Online (Sandbox Code Playgroud)