如何将model.summary()保存到Keras中的文件? [英] How to save model.summary() to file in Keras?
本文介绍了如何将model.summary()保存到Keras中的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Keras中有 model.summary()方法.它将表打印到标准输出.可以将其保存到文件吗?
There is model.summary() method in Keras. It prints table to stdout. Is it possible to save this to file?
推荐答案
如果您想要摘要的格式,则可以将 print
函数传递给 model.summary()
并以这种方式输出到文件:
If you want the formatting of summary you can pass a print
function to model.summary()
and output to file that way:
def myprint(s):
with open('modelsummary.txt','w+') as f:
print(s, file=f)
model.summary(print_fn=myprint)
或者,您可以使用 model.to_json()
或 model.to_yaml()
将其序列化为json或yaml字符串,然后稍后再导入.
Alternatively, you can serialize it to a json or yaml string with model.to_json()
or model.to_yaml()
which can be imported back later.
在Python 3.4+中执行此操作的另一种pythonic方法是使用 contextlib.redirect_stdout
An more pythonic way to do this in Python 3.4+ is to use contextlib.redirect_stdout
from contextlib import redirect_stdout
with open('modelsummary.txt', 'w') as f:
with redirect_stdout(f):
model.summary()
这篇关于如何将model.summary()保存到Keras中的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文