[英]How to save model.summary() to file in Keras?
如果您想要摘要的格式,您可以将print
函数传递给model.summary()
并以这种方式输出到文件:
def myprint(s):
with open('modelsummary.txt','a') as f:
print(s, file=f)
model.summary(print_fn=myprint)
或者,您可以使用model.to_json()
或model.to_yaml()
将其序列化为 json 或 yaml 字符串,稍后可以将其导入回来。
在 Python 3.4+ 中执行此操作的更 Pythonic 方法是使用contextlib.redirect_stdout
from contextlib import redirect_stdout
with open('modelsummary.txt', 'w') as f:
with redirect_stdout(f):
model.summary()
在这里,您还有另一个选择:
with open('modelsummary.txt', 'w') as f:
model.summary(print_fn=lambda x: f.write(x + '\n'))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.