Architecture diagrams make a Keras model easier to review when layer order, shape changes, or branching connections matter. A plotted PNG lets another reader inspect the graph before training, export, or code review without opening the model-building code.
The graph comes from the model's layer connections rather than its trained weights. Keras uses pydot and Graphviz to render those connections as an image, so both the Python package and the system renderer must be available.
An active Python virtual environment with Keras 3 and JAX is required. The package commands use Ubuntu or Debian repositories; plotting the architecture does not require training the model or supplying a dataset.
Steps to plot a Keras model diagram:
- Refresh the Ubuntu or Debian package metadata.
$ sudo apt update
- Install Graphviz for the dot renderer.
$ sudo apt install --assume-yes graphviz
- Install pydot in the active Python environment.
$ python -m pip install pydot
pydot is the Python bridge used by Keras, while Graphviz supplies the renderer that writes the image.
Related: How to install Keras with pip - Create plot_model_diagram.py in your text editor with the backend selection and imports.
- plot_model_diagram.py
import os os.environ["KERAS_BACKEND"] = "jax" import keras from keras import layers
KERAS_BACKEND must be set before importing keras. If the project uses TensorFlow or PyTorch, the corresponding backend value is tensorflow or torch.
Related: How to set the Keras backend - Add the Functional model after the imports in plot_model_diagram.py.
inputs = keras.Input(shape=(12,), name="features") x = layers.Dense(16, activation="relu", name="feature_encoder")(inputs) x = layers.Dropout(0.2, name="regularizer")(x) outputs = layers.Dense(3, activation="softmax", name="risk_score")(x) model = keras.Model(inputs=inputs, outputs=outputs, name="risk_score_model")
The input has 12 features, the hidden layer has 16 units, and the output has 3 scores. The explicit input defines the shapes before any data passes through the model.
Related: How to create a functional model in Keras - Append the diagram export after the model definition in plot_model_diagram.py.
keras.utils.plot_model( model, to_file="model-plot.png", show_shapes=True, show_layer_names=True, show_layer_activations=True, rankdir="TB", dpi=160, )
show_shapes, show_layer_names, and show_layer_activations add the inspection labels. rankdir=“TB” arranges the layers from top to bottom. The export replaces model-plot.png in the working directory if that file already exists.
- Save plot_model_diagram.py.
- Run plot_model_diagram.py with the active Python environment.
$ python plot_model_diagram.py
A successful run writes model-plot.png without printing a message. A missing pydot or Graphviz dependency produces an error instead.
Related: How to install Keras with pip - Verify the layer connections and shapes in model-plot.png with an image viewer.
The diagram should connect features to feature_encoder, regularizer, and risk_score in that order. The hidden layer shows relu and shape (None, 16); the final layer shows softmax and shape (None, 3). None represents the variable batch size. An unreadable image, a missing connection, or different layer labels means the exported graph does not match this model.