I am trying to save model to JSON with Keras and getting condensed JSON code.
Is it possible to save in pretty-printed human friendly JSON here?
The to_json
method from keras accepted **kwargs
and passed them to json.dumps
. Therefore this is the one line solution:
print(model.to_json(indent=4))
It generate results similar to the example of @anton-vbr.
You can use pprint.pformat
to retrieve a pretty string:
import pprint
json_str = model.to_json()
formatted_str = pprint.pformat(json.loads(json_str), indent=4)
If you don't want to save a copy of the formatted json, rather if you prefer to save it to a file, you can use pprint.pprint
and specify stream=...
with a file handler:
pprint.pprint(json.loads(json_str), indent=1, stream=open('model.json', 'w'))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With