.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/visualkeras_ANN/plot_conv_dense.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_ANN_plot_conv_dense.py: Visualkeras: Spam Classification Conv1D Dense Example ====================================================================== An example showing Spam 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 :lineno-start: 9 # Authors: The scikit-plots developers # SPDX-License-Identifier: BSD-3-Clause .. GENERATED FROM PYTHON SOURCE LINES 13-22 Installing dependencies https://sphinx-gallery.github.io/stable/configuration.html#using-multiple-code-blocks-to-create-a-single-figure .. code-block:: bash %%bash # (e.g. %%bash or %%writefile) will be turned into a runnable code block. # pip install -q tensorflow # apt-get -qq install curl .. GENERATED FROM PYTHON SOURCE LINES 25-26 pip install protobuf==5.29.4 .. GENERATED FROM PYTHON SOURCE LINES 26-31 .. code-block:: Python :lineno-start: 26 import tensorflow as tf # Clear any session to reset the state of TensorFlow/Keras tf.keras.backend.clear_session() .. GENERATED FROM PYTHON SOURCE LINES 32-59 .. code-block:: Python :lineno-start: 32 model = tf.keras.models.Sequential() model.add(tf.keras.layers.InputLayer(input_shape=(100,))) # To convert 2D of input data into a 3D input # Reshape to a compatible shape for Conv1D as [batch_size, time_steps, input_dimension] # The Conv1D layer expects a 3D input: (batch_size, steps, channels). # The Reshape layer now reshapes the input to (n_timesteps,n_features) like (100, 1), # which matches the expected input of Conv1D. model.add( tf.keras.layers.Reshape((100, 1)) ) # Shape: (batch_size, 100, 1), input_shape=(100,) # Add Conv1D and other layers model.add(tf.keras.layers.Conv1D(32, 1, strides=1, activation="relu")) model.add(tf.keras.layers.Dropout(0.5)) model.add(tf.keras.layers.MaxPooling1D(pool_size=2)) # Flatten and add Dense layers model.add(tf.keras.layers.Flatten()) model.add(tf.keras.layers.Dense(64, activation="relu")) model.add(tf.keras.layers.Dense(32, activation="relu")) model.add(tf.keras.layers.Dense(1, activation="sigmoid")) # Compile the model model.compile(optimizer="rmsprop", loss="binary_crossentropy", metrics=["accuracy"]) model.summary() .. rst-class:: sphx-glr-script-out .. code-block:: none /home/circleci/.pyenv/versions/3.11.13/lib/python3.11/site-packages/keras/src/layers/core/input_layer.py:27: UserWarning: Argument `input_shape` is deprecated. Use `shape` instead. Model: "sequential" ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┓ ┃ Layer (type) ┃ Output Shape ┃ Param # ┃ ┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━┩ │ reshape (Reshape) │ (None, 100, 1) │ 0 │ ├─────────────────────────────────┼────────────────────────┼───────────────┤ │ conv1d (Conv1D) │ (None, 100, 32) │ 64 │ ├─────────────────────────────────┼────────────────────────┼───────────────┤ │ dropout (Dropout) │ (None, 100, 32) │ 0 │ ├─────────────────────────────────┼────────────────────────┼───────────────┤ │ max_pooling1d (MaxPooling1D) │ (None, 50, 32) │ 0 │ ├─────────────────────────────────┼────────────────────────┼───────────────┤ │ flatten (Flatten) │ (None, 1600) │ 0 │ ├─────────────────────────────────┼────────────────────────┼───────────────┤ │ dense (Dense) │ (None, 64) │ 102,464 │ ├─────────────────────────────────┼────────────────────────┼───────────────┤ │ dense_1 (Dense) │ (None, 32) │ 2,080 │ ├─────────────────────────────────┼────────────────────────┼───────────────┤ │ dense_2 (Dense) │ (None, 1) │ 33 │ └─────────────────────────────────┴────────────────────────┴───────────────┘ Total params: 104,641 (408.75 KB) Trainable params: 104,641 (408.75 KB) Non-trainable params: 0 (0.00 B) .. GENERATED FROM PYTHON SOURCE LINES 60-63 .. code-block:: Python :lineno-start: 60 import matplotlib.pyplot as plt from scikitplot import visualkeras .. GENERATED FROM PYTHON SOURCE LINES 64-82 .. code-block:: Python :lineno-start: 64 img_spam = visualkeras.layered_view( model, min_z=1, min_xy=1, max_z=4096, max_xy=4096, scale_z=6, scale_xy=0.2, font={"font_size": 14}, text_callable="default", one_dim_orientation="x", # to_file="./spam_conv_x.png", save_fig=True, save_fig_filename="spam_conv_x.png", show_fig=True, ) img_spam .. image-sg:: /auto_examples/visualkeras_ANN/images/sphx_glr_plot_conv_dense_001.png :alt: plot conv dense :srcset: /auto_examples/visualkeras_ANN/images/sphx_glr_plot_conv_dense_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 83-100 .. code-block:: Python :lineno-start: 83 img_spam = visualkeras.layered_view( model, min_z=1, min_xy=1, max_z=4096, max_xy=4096, scale_z=6, scale_xy=0.2, font={"font_size": 14}, text_callable="default", one_dim_orientation="y", # to_file="./spam_conv_y.png", save_fig=True, save_fig_filename="spam_conv_y.png", ) img_spam .. image-sg:: /auto_examples/visualkeras_ANN/images/sphx_glr_plot_conv_dense_002.png :alt: plot conv dense :srcset: /auto_examples/visualkeras_ANN/images/sphx_glr_plot_conv_dense_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 101-121 .. code-block:: Python :lineno-start: 101 img_spam = visualkeras.layered_view( model, min_z=1, min_xy=1, max_z=4096, max_xy=4096, scale_z=0.2, scale_xy=1, font={"font_size": 9}, text_callable="default", one_dim_orientation="z", # to_file="./spam_conv_z.png", save_fig=True, save_fig_filename="spam_conv_z.png", overwrite=False, add_timestamp=True, verbose=True, ) img_spam .. image-sg:: /auto_examples/visualkeras_ANN/images/sphx_glr_plot_conv_dense_003.png :alt: plot conv dense :srcset: /auto_examples/visualkeras_ANN/images/sphx_glr_plot_conv_dense_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none [INFO] Saving path to: /home/circleci/repo/galleries/examples/visualkeras_ANN/result_images/spam_conv_z_20250629_134219Z.png .. GENERATED FROM PYTHON SOURCE LINES 122-130 .. tags:: model-type: classification model-workflow: model building plot-type: visualkeras domain: neural network level: intermediate purpose: showcase .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 3.156 seconds) .. _sphx_glr_download_auto_examples_visualkeras_ANN_plot_conv_dense.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/main?urlpath=lab/tree/notebooks/auto_examples/visualkeras_ANN/plot_conv_dense.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_ANN/plot_conv_dense.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_conv_dense.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_conv_dense.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_conv_dense.zip ` .. include:: plot_conv_dense.recommendations .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_