How to Plot Model in Keras

  • Post category:Keras / Python

Visualizing a Keras model is helpful for understanding its architecture and ensuring it’s constructed as intended. You can use various libraries to plot Keras models, with one of the most common options being plot_model from the Keras utils module. Here’s how to do it:

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

# 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'))

# Generate a visualization of the model
plot_model(model, to_file='my_keras_model.png', show_shapes=True)

After running this code, you will find a PNG image file in your working directory that visually represents your Keras model’s architecture. You can open this image to inspect the model’s layers, connections, and shapes.