2024-05-23 14:34:46 发布
网友
路缘石中有model.summary() method。它把表打印到标准输出。是否可以将此保存到文件?
如果您想要设置摘要的格式,可以将print函数传递给model.summary(),并以这种方式输出到文件:
print
model.summary()
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字符串,稍后可以将其重新导入。
model.to_json()
model.to_yaml()
在Python 3.4+中,使用contextlib.redirect_stdout是一种更具Python风格的方法
contextlib.redirect_stdout
from contextlib import redirect_stdout with open('modelsummary.txt', 'w') as f: with redirect_stdout(f): model.summary()
如果您想要设置摘要的格式,可以将
print
函数传递给model.summary()
,并以这种方式输出到文件:或者,可以使用
model.to_json()
或model.to_yaml()
将其序列化为json或yaml字符串,稍后可以将其重新导入。编辑
在Python 3.4+中,使用
contextlib.redirect_stdout
是一种更具Python风格的方法相关问题 更多 >
编程相关推荐