.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/visualkeras_CNN/plot_autoencoder.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code or to run this example in your browser via JupyterLite or Binder. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_visualkeras_CNN_plot_autoencoder.py: visualkeras: autoencoder example ========================================== An example showing the :py:func:`~scikitplot.visualkeras` function used by a :py:class:`~tensorflow.keras.Model` model. .. GENERATED FROM PYTHON SOURCE LINES 8-12 .. code-block:: Python # Authors: The scikit-plots developers # SPDX-License-Identifier: BSD-3-Clause .. GENERATED FROM PYTHON SOURCE LINES 13-27 .. code-block:: Python # visualkeras Need aggdraw tensorflow # !pip install scikitplot[core, cpu] # or # !pip install aggdraw # !pip install tensorflow # pip install protobuf==5.29.4 import tensorflow as tf # Clear any session to reset the state of TensorFlow/Keras tf.keras.backend.clear_session() .. rst-class:: sphx-glr-script-out .. code-block:: none /home/circleci/.pyenv/versions/3.11.14/lib/python3.11/site-packages/keras/src/export/tf2onnx_lib.py:8: FutureWarning: In the future `np.object` will be defined as the corresponding NumPy scalar. .. GENERATED FROM PYTHON SOURCE LINES 28-29 encoder Model .. GENERATED FROM PYTHON SOURCE LINES 29-48 .. code-block:: Python encoder_input = tf.keras.Input(shape=(28, 28, 1), name="img") x = tf.keras.layers.Conv2D(16, 3, activation="relu")(encoder_input) x = tf.keras.layers.Conv2D(32, 3, activation="relu")(x) x = tf.keras.layers.MaxPooling2D(3)(x) x = tf.keras.layers.Conv2D(32, 3, activation="relu")(x) x = tf.keras.layers.Conv2D(16, 3, activation="relu")(x) encoder_output = tf.keras.layers.GlobalMaxPooling2D()(x) encoder = tf.keras.Model(encoder_input, encoder_output, name="encoder") # autoencoder Model x = tf.keras.layers.Reshape((4, 4, 1))(encoder_output) x = tf.keras.layers.Conv2DTranspose(16, 3, activation="relu")(x) x = tf.keras.layers.Conv2DTranspose(32, 3, activation="relu")(x) x = tf.keras.layers.UpSampling2D(3)(x) x = tf.keras.layers.Conv2DTranspose(16, 3, activation="relu")(x) decoder_output = tf.keras.layers.Conv2DTranspose(1, 3, activation="relu")(x) autoencoder = tf.keras.Model(encoder_input, decoder_output, name="autoencoder") autoencoder.summary() .. rst-class:: sphx-glr-script-out .. code-block:: none Model: "autoencoder" ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┓ ┃ Layer (type) ┃ Output Shape ┃ Param # ┃ ┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━┩ │ img (InputLayer) │ (None, 28, 28, 1) │ 0 │ ├─────────────────────────────────┼────────────────────────┼───────────────┤ │ conv2d (Conv2D) │ (None, 26, 26, 16) │ 160 │ ├─────────────────────────────────┼────────────────────────┼───────────────┤ │ conv2d_1 (Conv2D) │ (None, 24, 24, 32) │ 4,640 │ ├─────────────────────────────────┼────────────────────────┼───────────────┤ │ max_pooling2d (MaxPooling2D) │ (None, 8, 8, 32) │ 0 │ ├─────────────────────────────────┼────────────────────────┼───────────────┤ │ conv2d_2 (Conv2D) │ (None, 6, 6, 32) │ 9,248 │ ├─────────────────────────────────┼────────────────────────┼───────────────┤ │ conv2d_3 (Conv2D) │ (None, 4, 4, 16) │ 4,624 │ ├─────────────────────────────────┼────────────────────────┼───────────────┤ │ global_max_pooling2d │ (None, 16) │ 0 │ │ (GlobalMaxPooling2D) │ │ │ ├─────────────────────────────────┼────────────────────────┼───────────────┤ │ reshape (Reshape) │ (None, 4, 4, 1) │ 0 │ ├─────────────────────────────────┼────────────────────────┼───────────────┤ │ conv2d_transpose │ (None, 6, 6, 16) │ 160 │ │ (Conv2DTranspose) │ │ │ ├─────────────────────────────────┼────────────────────────┼───────────────┤ │ conv2d_transpose_1 │ (None, 8, 8, 32) │ 4,640 │ │ (Conv2DTranspose) │ │ │ ├─────────────────────────────────┼────────────────────────┼───────────────┤ │ up_sampling2d (UpSampling2D) │ (None, 24, 24, 32) │ 0 │ ├─────────────────────────────────┼────────────────────────┼───────────────┤ │ conv2d_transpose_2 │ (None, 26, 26, 16) │ 4,624 │ │ (Conv2DTranspose) │ │ │ ├─────────────────────────────────┼────────────────────────┼───────────────┤ │ conv2d_transpose_3 │ (None, 28, 28, 1) │ 145 │ │ (Conv2DTranspose) │ │ │ └─────────────────────────────────┴────────────────────────┴───────────────┘ Total params: 28,241 (110.32 KB) Trainable params: 28,241 (110.32 KB) Non-trainable params: 0 (0.00 B) .. GENERATED FROM PYTHON SOURCE LINES 49-50 Build the model with an explicit input shape .. GENERATED FROM PYTHON SOURCE LINES 50-68 .. code-block:: Python autoencoder.build( input_shape=(None, 28, 28, 1) ) # Batch size of None, shape (28, 28, 1) # Create a dummy input tensor with a batch size of 1 dummy_input = tf.random.normal([1, 28, 28, 1]) # Batch size of 1, shape (28, 28, 1) # Run the dummy input through the model to trigger shape calculation encoder_output = autoencoder(dummy_input) # Now check the output shape of the encoder print("Output shape after running model with dummy input:", encoder_output.shape) # Check each layer's output shape after building the model for layer in encoder.layers: if hasattr(layer, "output_shape"): print(f"{layer.name} output shape: {layer.output_shape}") if hasattr(layer, "output"): print(f"{layer.name} shape: {layer.output.shape}") .. rst-class:: sphx-glr-script-out .. code-block:: none Output shape after running model with dummy input: (1, 28, 28, 1) img shape: (None, 28, 28, 1) conv2d shape: (None, 26, 26, 16) conv2d_1 shape: (None, 24, 24, 32) max_pooling2d shape: (None, 8, 8, 32) conv2d_2 shape: (None, 6, 6, 32) conv2d_3 shape: (None, 4, 4, 16) global_max_pooling2d shape: (None, 16) .. GENERATED FROM PYTHON SOURCE LINES 69-80 .. code-block:: Python from scikitplot import visualkeras img_encoder = visualkeras.layered_view( encoder, text_callable="default", # to_file="result_images/encoder.png", save_fig=True, save_fig_filename="encoder.png", ) img_encoder .. image-sg:: /auto_examples/visualkeras_CNN/images/sphx_glr_plot_autoencoder_001.png :alt: plot autoencoder :srcset: /auto_examples/visualkeras_CNN/images/sphx_glr_plot_autoencoder_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none 2026-01-06 16:56:15.716688: W scikitplot 140172358323072 utils_pil.py:204:load_font] Error loading system font: cannot open resource 2026-01-06 16:56:15.716780: W scikitplot 140172358323072 utils_pil.py:206:load_font] Falling back to PIL default font. 2026-01-06 16:56:15.716891: W scikitplot 140172358323072 _layered.py:216:layered_view] The legend_text_spacing_offset parameter is deprecated andwill be removed in a future release. .. GENERATED FROM PYTHON SOURCE LINES 81-89 .. code-block:: Python img_autoencoder = visualkeras.layered_view( autoencoder, # to_file="result_images/autoencoder.png", save_fig=True, save_fig_filename="autoencoder.png", ) img_autoencoder .. image-sg:: /auto_examples/visualkeras_CNN/images/sphx_glr_plot_autoencoder_002.png :alt: plot autoencoder :srcset: /auto_examples/visualkeras_CNN/images/sphx_glr_plot_autoencoder_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none 2026-01-06 16:56:15.906558: W scikitplot 140172358323072 utils_pil.py:204:load_font] Error loading system font: cannot open resource 2026-01-06 16:56:15.906680: W scikitplot 140172358323072 utils_pil.py:206:load_font] Falling back to PIL default font. 2026-01-06 16:56:15.906808: W scikitplot 140172358323072 _layered.py:216:layered_view] The legend_text_spacing_offset parameter is deprecated andwill be removed in a future release. .. GENERATED FROM PYTHON SOURCE LINES 90-109 .. code-block:: Python img_autoencoder_text = visualkeras.layered_view( autoencoder, min_z=1, min_xy=1, max_z=4096, max_xy=4096, scale_z=1, scale_xy=1, # font={"font_size": 14}, text_callable="default", # to_file="result_images/autoencoder_text.png", save_fig=True, save_fig_filename="autoencoder_text.png", overwrite=False, add_timestamp=True, verbose=True, ) img_autoencoder_text .. image-sg:: /auto_examples/visualkeras_CNN/images/sphx_glr_plot_autoencoder_003.png :alt: plot autoencoder :srcset: /auto_examples/visualkeras_CNN/images/sphx_glr_plot_autoencoder_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none 2026-01-06 16:56:16.057579: W scikitplot 140172358323072 utils_pil.py:204:load_font] Error loading system font: cannot open resource 2026-01-06 16:56:16.057785: W scikitplot 140172358323072 utils_pil.py:206:load_font] Falling back to PIL default font. 2026-01-06 16:56:16.057995: W scikitplot 140172358323072 _layered.py:216:layered_view] The legend_text_spacing_offset parameter is deprecated andwill be removed in a future release. [INFO] Saving path to: /home/circleci/repo/galleries/examples/visualkeras_CNN/result_images/autoencoder_text_20260106_165616Z.png .. GENERATED FROM PYTHON SOURCE LINES 110-118 .. tags:: model-type: classification model-workflow: model building plot-type: visualkeras domain: neural network level: beginner purpose: showcase .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 3.689 seconds) .. _sphx_glr_download_auto_examples_visualkeras_CNN_plot_autoencoder.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/scikit-plots/scikit-plots/maintenance/0.4.X?urlpath=lab/tree/notebooks/auto_examples/visualkeras_CNN/plot_autoencoder.ipynb :alt: Launch binder :width: 150 px .. container:: lite-badge .. image:: images/jupyterlite_badge_logo.svg :target: ../../lite/lab/index.html?path=auto_examples/visualkeras_CNN/plot_autoencoder.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_autoencoder.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_autoencoder.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_autoencoder.zip ` .. include:: plot_autoencoder.recommendations .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_