tf.keras.utils.plot_model
Stay organized with collections
Save and categorize content based on your preferences.
Converts a Keras model to dot format and save to a file.
tf.keras.utils.plot_model(
model,
to_file='model.png',
show_shapes=False,
show_dtype=False,
show_layer_names=True,
rankdir='TB',
expand_nested=False,
dpi=96,
layer_range=None,
show_layer_activations=False
)
Example:
input = tf.keras.Input(shape=(100,), dtype='int32', name='input')
x = tf.keras.layers.Embedding(
output_dim=512, input_dim=10000, input_length=100)(input)
x = tf.keras.layers.LSTM(32)(x)
x = tf.keras.layers.Dense(64, activation='relu')(x)
x = tf.keras.layers.Dense(64, activation='relu')(x)
x = tf.keras.layers.Dense(64, activation='relu')(x)
output = tf.keras.layers.Dense(1, activation='sigmoid', name='output')(x)
model = tf.keras.Model(inputs=[input], outputs=[output])
dot_img_file = '/tmp/model_1.png'
tf.keras.utils.plot_model(model, to_file=dot_img_file, show_shapes=True)
Args |
model
|
A Keras model instance
|
to_file
|
File name of the plot image.
|
show_shapes
|
whether to display shape information.
|
show_dtype
|
whether to display layer dtypes.
|
show_layer_names
|
whether to display layer names.
|
rankdir
|
rankdir argument passed to PyDot,
a string specifying the format of the plot: 'TB' creates a vertical
plot; 'LR' creates a horizontal plot.
|
expand_nested
|
Whether to expand nested models into clusters.
|
dpi
|
Dots per inch.
|
layer_range
|
input of list containing two str items, which is the
starting layer name and ending layer name (both inclusive) indicating the
range of layers for which the plot will be generated. It also accepts
regex patterns instead of exact name. In such case, start predicate will
be the first element it matches to layer_range[0] and the end predicate
will be the last element it matches to layer_range[1] . By default None
which considers all layers of model. Note that you must pass range such
that the resultant subgraph must be complete.
|
show_layer_activations
|
Display layer activations (only for layers that
have an activation property).
|
Raises |
ValueError
|
if plot_model is called before the model is built.
|
Returns |
A Jupyter notebook Image object if Jupyter is installed.
This enables in-line display of the model plots in notebooks.
|
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates. Some content is licensed under the numpy license.
Last updated 2022-10-27 UTC.
[null,null,["Last updated 2022-10-27 UTC."],[],[],null,["# tf.keras.utils.plot_model\n\n\u003cbr /\u003e\n\n|-------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/keras-team/keras/tree/v2.8.0/keras/utils/vis_utils.py#L351-L443) |\n\nConverts a Keras model to dot format and save to a file.\n\n#### View aliases\n\n\n**Compat aliases for migration**\n\nSee\n[Migration guide](https://www.tensorflow.org/guide/migrate) for\nmore details.\n\n[`tf.compat.v1.keras.utils.plot_model`](https://www.tensorflow.org/api_docs/python/tf/keras/utils/plot_model)\n\n\u003cbr /\u003e\n\n tf.keras.utils.plot_model(\n model,\n to_file='model.png',\n show_shapes=False,\n show_dtype=False,\n show_layer_names=True,\n rankdir='TB',\n expand_nested=False,\n dpi=96,\n layer_range=None,\n show_layer_activations=False\n )\n\n#### Example:\n\n input = tf.keras.Input(shape=(100,), dtype='int32', name='input')\n x = tf.keras.layers.Embedding(\n output_dim=512, input_dim=10000, input_length=100)(input)\n x = tf.keras.layers.LSTM(32)(x)\n x = tf.keras.layers.Dense(64, activation='relu')(x)\n x = tf.keras.layers.Dense(64, activation='relu')(x)\n x = tf.keras.layers.Dense(64, activation='relu')(x)\n output = tf.keras.layers.Dense(1, activation='sigmoid', name='output')(x)\n model = tf.keras.Model(inputs=[input], outputs=[output])\n dot_img_file = '/tmp/model_1.png'\n tf.keras.utils.plot_model(model, to_file=dot_img_file, show_shapes=True)\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|--------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `model` | A Keras model instance |\n| `to_file` | File name of the plot image. |\n| `show_shapes` | whether to display shape information. |\n| `show_dtype` | whether to display layer dtypes. |\n| `show_layer_names` | whether to display layer names. |\n| `rankdir` | `rankdir` argument passed to PyDot, a string specifying the format of the plot: 'TB' creates a vertical plot; 'LR' creates a horizontal plot. |\n| `expand_nested` | Whether to expand nested models into clusters. |\n| `dpi` | Dots per inch. |\n| `layer_range` | input of `list` containing two `str` items, which is the starting layer name and ending layer name (both inclusive) indicating the range of layers for which the plot will be generated. It also accepts regex patterns instead of exact name. In such case, start predicate will be the first element it matches to `layer_range[0]` and the end predicate will be the last element it matches to `layer_range[1]`. By default `None` which considers all layers of model. Note that you must pass range such that the resultant subgraph must be complete. |\n| `show_layer_activations` | Display layer activations (only for layers that have an `activation` property). |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|--------------|------------------------------------------------------|\n| `ValueError` | if `plot_model` is called before the model is built. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| A Jupyter notebook Image object if Jupyter is installed. This enables in-line display of the model plots in notebooks. ||\n\n\u003cbr /\u003e"]]