Visualkeras: Spam Classification Conv1D Dense Example#

An example showing Spam the visualkeras function used by a tf.keras.Model model.

# Authors: The scikit-plots developers
# SPDX-License-Identifier: BSD-3-Clause

Force garbage collection

import gc

gc.collect()
3
# 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()
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()
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)
from scikitplot import visualkeras

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",
)

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 = 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,
)
plot conv dense
[INFO] Saving path to: /home/circleci/repo/galleries/examples/visualkeras_ANN/result_images/spam_conv_z_20250422_153921Z.png
[INFO] Image saved using Matplotlib: /home/circleci/repo/galleries/examples/visualkeras_ANN/result_images/spam_conv_z_20250422_153921Z.png

Tags: model-type: classification model-workflow: model building plot-type: visualkeras domain: neural network level: intermediate purpose: showcase

Total running time of the script: (0 minutes 3.677 seconds)

Related examples

visualkeras: Spam Dense example

visualkeras: Spam Dense example

visualkeras: autoencoder example

visualkeras: autoencoder example

visualkeras: transformers example

visualkeras: transformers example

visualkeras: custom VGG example

visualkeras: custom VGG example

Gallery generated by Sphinx-Gallery