plot_model
keras.plot_model(model, *, title=None, expand_nested=False)Draw a Keras model’s layers as a directed graph.
One box per layer, top to bottom from the inputs, with an arrow from each layer to the layers called on its output, as keras.utils.plot_model draws it. Left and Right walk the layers in the order data flows through them; the Inputs and Outputs rotor units follow the arrows, so a skip connection is heard as a second input. Each layer is announced with its type and, from a built model, its output shape and parameter count.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| model | (keras.Model, dict or str) |
A built Sequential or functional model, its get_config() dictionary, or the JSON model.to_json() returns. Only the config is read, so a model saved as JSON is drawn with no Keras installed. |
required |
| title | str | The chart’s title. By default the model’s name. | None |
| expand_nested | bool | Whether a model used as a layer, such as a Sequential block, is drawn as its own layers in a frame named for it – a scope, opened with Down and closed with Up – rather than as one box. |
False |
Returns
| Name | Type | Description |
|---|---|---|
matplotlib.figure.Figure |
The graph, ready for :func:maidr.show, :func:maidr.render or :func:maidr.save_html. The figure is not managed by pyplot, so plt.show() does not show it. |
Raises
| Name | Type | Description |
|---|---|---|
| TypeError | If model is not a model, a config or a model’s JSON, or is a subclassed model whose config Keras cannot give. |
|
| ValueError | If model is JSON that does not parse, or a config with no layers. |
Notes
The chart is a directed_graph, an experimental type that needs a maidr.js release carrying it; an older one shows the drawing without reading it. The copy of maidr.js py-maidr bundles does not carry it yet, so until it does, the chart is read only from the CDN: online under the default use_cdn="auto", or with use_cdn=True. With use_cdn=False, or offline, maidr warns and the chart is not read.
Both config formats are read: Keras 3’s, and Keras 2’s (tf.keras). A layer called more than once is one box fed by everything it was called on. A layer’s attributes are its type and, where its config has them, its units, filters, kernel size, activation and rate; a built model adds its output shape and parameter count, which a config alone does not hold.
Examples
>>> import maidr
>>> from maidr.keras import plot_model
>>> maidr.save_html(plot_model(model, expand_nested=True), "model.html")