How to save model.summary () file to Keras file?

Keras has a model.summary () method . It prints a table in stdout. Can I save this to a file?

+4
source share
1 answer

If you want to format the summary, you can pass the function printto model.summary()and output it to a file as follows:

def myprint(s):
    with open('modelsummary.txt','w') as f:
        print(s, file=f)

model.summary(print_fn=myprint)

Alternatively, you can serialize it to a json or yaml string using model.to_json()or model.to_yaml(), which you can import later.

Edit

In Python 3.4+, a more pythonic way to do this is with contextlib.redirect_stdout

import redirect_stdout from contextlib

with open('modelsummary.txt', 'w') as f:
    with redirect_stdout(f):
        model.summary()
+8
source

Source: https://habr.com/ru/post/1681818/


All Articles