How to Print Model Summary in Keras

  • Post category:Python / Keras

Printing the model summary in Keras is a straightforward way to get a quick overview of your model’s architecture, including layer types, output shapes, and the number of trainable parameters. Here’s how to do it:

# Import necessary libraries
import keras
from keras.models import Sequential
from keras.layers import Dense

# Create a simple Keras model (for demonstration)
model = Sequential()
model.add(Dense(units=64, activation='relu', input_dim=100))
model.add(Dense(units=10, activation='softmax'))

# Print the model summary
model.summary()

Output:

Model: "sequential_7"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 dense_16 (Dense)            (None, 64)                6464      
                                                                 
 dense_17 (Dense)            (None, 10)                650       
                                                                 
=================================================================
Total params: 7,114
Trainable params: 7,114
Non-trainable params: 0
_________________________________________________________________

In the code above, we create a basic Keras model and then use the summary() method to print the model’s architecture summary.