How do I plot a Keras/Tensorflow subclassing API model?
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
The Keras Subclassing API (tf.keras.Model subclass) provides maximum flexibility for custom architectures, but tf.keras.utils.plot_model() cannot automatically plot subclassed models because the computation graph is not built until the model is called with data. Unlike Sequential or Functional API models, subclassed models define their forward pass in Python code (call() method), which Keras cannot inspect statically. You must build the model with a concrete input shape first, and even then the plot shows limited detail compared to Functional models.
The Problem
Solution 1: Build the Model First
Call the model with sample data or use model.build() to create the graph.
The resulting plot shows the model as a single block with input/output shapes but does not show individual layer connections like a Functional API plot does.
Solution 2: Create a Functional Equivalent for Plotting
Build a Functional API model that mirrors your subclassed model, then plot it.
This produces a detailed graph showing each layer, its shape, and connections.
Solution 3: Use model.summary() for Text Output
Solution 4: Visualize with TensorBoard
Prerequisite: Install Graphviz
plot_model requires Graphviz and pydot.
Common Pitfalls
- Plotting before building:
plot_model()requires the model to be built (weights allocated). Callmodel.build(input_shape=(...))or pass sample data through the model first. Without this, you getValueError: This model has not yet been built. - Expecting detailed layer graphs from subclassed models: Even after building,
plot_modelon a subclassed model shows a simplified view (single block). To get detailed layer-by-layer graphs, create a Functional API equivalent usingtf.keras.Inputandtf.keras.Model. - Missing Graphviz installation:
plot_modeldepends on the system-level Graphviz binary and thepydotPython package. Missing either producesImportError: Failed to import pydotorFileNotFoundError: "dot" not found in path. Install both the system package and the pip package. - Training-mode layers in
build_graph: Layers like Dropout and BatchNormalization behave differently during training vs inference. When creating a Functional equivalent, thetrainingparameter incall()defaults toFalse, so the plot reflects inference topology. This is usually correct for visualization purposes. - Nested subclassed models: If a subclassed model contains other subclassed models,
expand_nested=Truemay not fully expand them. Each nested model must also implementbuild_graph()for complete visualization.
Summary
- Subclassed models require
model.build()or a forward pass beforeplot_model()works - For detailed layer graphs, create a Functional API equivalent via a
build_graph()method - Use
model.summary()for quick text-based architecture overview - Use TensorBoard graph tracing for interactive visualization
- Install both Graphviz (system) and pydot (pip) as prerequisites
Related reading
- How do I print the model summary in PyTorch?
- How do I print the model summary in PyTorch?
- How do I save a trained model in PyTorch?
- How do I save and load BatchNormalization Layer in this Tensorflow model?
- How do I print inside the loss function during training in Keras?
- How do I profile a tf.data.Dataset?
- How do I pull from a Git repository through an HTTP proxy?
- How do I recreate docker-daemon's additional iptables rules?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.