74

I want to write a *.txt file with the neural network hyperparameters and the model architecture. Is it possible to write the object model.summary() to my output file?

(...)
summary = str(model.summary())
(...)
out = open(filename + 'report.txt','w')
out.write(summary)
out.close

It happens that I'm getting "None" as you can see below.

Hyperparameters
=========================

learning_rate: 0.01
momentum: 0.8
decay: 0.0
batch size: 128
no. epochs: 3
dropout: 0.5
-------------------------

None
val_acc: 0.232323229313
val_loss: 3.88496732712
train_acc: 0.0965207634216
train_loss: 4.07161939425
train/val loss ratio: 1.04804469418

Any idea how to deal with that?

1

9 Answers 9

79

With my version of Keras (2.0.6) and Python (3.5.0), this works for me:

# Create an empty model
from keras.models import Sequential
model = Sequential()

# Open the file
with open(filename + 'report.txt','w') as fh:
    # Pass the file handle in as a lambda function to make it callable
    model.summary(print_fn=lambda x: fh.write(x + '\n'))

This outputs the following lines to the file:

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
Total params: 0
Trainable params: 0
Non-trainable params: 0
_________________________________________________________________
2
  • 4
    New in version 2.0.6
    – Huo
    Aug 12, 2017 at 2:41
  • 1
    Just as an additional comment: you can also use line_length as an extra argument to tune how long the output line will be. This is useful because sometimes the layer name is too long and it is therefore cut: model.summary(line_length=80, print_fn=lambda x: fh.write(x + '\n'))
    – iipr
    Aug 14, 2019 at 17:33
36

For me, this worked to just get the model summary as a string:

stringlist = []
model.summary(print_fn=lambda x: stringlist.append(x))
short_model_summary = "\n".join(stringlist)
print(short_model_summary)
0
25

And if you want to write to a log:

import logging
logger = logging.getLogger(__name__)

model.summary(print_fn=logger.info)
12

I understand the OP has already accepted winni2k's answer, but since the question title actually implies saving the outputs of model.summary() to a string, not a file, the following code might help others who come to this page looking for that (like I did).

The code below was run using TensorFlow 1.12.0, which comes with Keras 2.1.6-tf on Python 3.6.2.

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Activation
import io

# Example model
model = Sequential([
    Dense(32, input_shape=(784,)),
    Activation('relu'),
    Dense(10),
    Activation('softmax'),
])

def get_model_summary(model):
    stream = io.StringIO()
    model.summary(print_fn=lambda x: stream.write(x + '\n'))
    summary_string = stream.getvalue()
    stream.close()
    return summary_string

model_summary_string = get_model_summary(model)

print(model_summary_string)

Which yields (as a string):

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense (Dense)                (None, 32)                25120     
_________________________________________________________________
activation (Activation)      (None, 32)                0         
_________________________________________________________________
dense_1 (Dense)              (None, 10)                330       
_________________________________________________________________
activation_1 (Activation)    (None, 10)                0         
=================================================================
Total params: 25,450
Trainable params: 25,450
Non-trainable params: 0
_________________________________________________________________
10

I stumbled upon the same problem as well! There are two possible workarounds:

Use to_json() method of the model

summary = str(model.to_json())

This is your case above.

Otherwise use the ascii method from keras_diagram

from keras_diagram import ascii
summary = ascii(model)
8

It's not the best way to do it but one thing you can do is to redirect stdout:

orig_stdout = sys.stdout
f = open('out.txt', 'w')
sys.stdout = f
print(model.summary())
sys.stdout = orig_stdout
f.close()

see "How to redirect 'print' output to a file using python?"

0
6

One option, although not an exact replacement for model.summary, is to export a model's configuration using model.get_config(). From the docs:

model.get_config(): returns a dictionary containing the configuration of the model. The model can be reinstantiated from its config via:

config = model.get_config()
model = Model.from_config(config)
# or, for Sequential:
model = Sequential.from_config(config)
2

As I came here to find a way to log the summary, I wanted to share this little twist to @ajb answer to avoid the INFO: at every line in the log file by using @FAnders answer:

def get_model_summary(model: tf.keras.Model) -> str:
    string_list = []
    model.summary(line_length=80, print_fn=lambda x: string_list.append(x))
    return "\n".join(string_list)

# some code
logging.info(get_model_summary(model)

which produces a log file as: enter image description here

1

I had this same problem. @Pasa's answer was very useful but I thought I would post a more minimal example: it's a reasonable assumption that you already have a Keras model at this point.

import io

s = io.StringIO()
model.summary(print_fn=lambda x: s.write(x + '\n'))
model_summary = s.getvalue()
s.close()

print("The model summary is:\n\n{}".format(model_summary))

An example of when having this string is useful: if you have a matplotlib plot. You can then use:

plt.text(0, 0.25, model_summary)

To write the summary of your model to your performance chart, for quick reference: enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.