visualkeras: Spam Dense example#

An example showing 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()

import tensorflow.python as tf_python

# Clear any session to reset the state of TensorFlow/Keras
tf_python.keras.backend.clear_session()
model = tf_python.keras.models.Sequential()
model.add(tf_python.keras.layers.InputLayer(input_shape=(100,)))

# Add Dense layers
model.add(tf_python.keras.layers.Dense(64, activation="relu"))  # input_shape=(100,)
model.add(tf_python.keras.layers.Dense(32, activation="relu"))
model.add(tf_python.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 #
=================================================================
dense (Dense)                (None, 64)                6464
_________________________________________________________________
dense_1 (Dense)              (None, 32)                2080
_________________________________________________________________
dense_2 (Dense)              (None, 1)                 33
=================================================================
Total params: 8,577
Trainable params: 8,577
Non-trainable params: 0
_________________________________________________________________
from scikitplot import visualkeras

img_spam = visualkeras.layered_view(
    model,
    min_z=1,
    min_xy=1,
    max_z=4096,
    max_xy=4096,
    scale_z=1,
    scale_xy=1,
    font={"font_size": 7},
    text_callable="default",
    one_dim_orientation="x",
    # to_file="result_images/spam_dense_x.png",
    save_fig=True,
    save_fig_filename="spam_dense_x.png",
)

img_spam = visualkeras.layered_view(
    model,
    min_z=1,
    min_xy=1,
    max_z=4096,
    max_xy=4096,
    scale_z=1,
    scale_xy=1,
    font={"font_size": 7},
    text_callable="default",
    one_dim_orientation="y",
    # to_file="result_images/spam_dense_y.png",
    save_fig=True,
    save_fig_filename="spam_dense_y.png",
)

img_spam = visualkeras.layered_view(
    model,
    min_z=1,
    min_xy=1,
    max_z=4096,
    max_xy=4096,
    scale_z=1,
    scale_xy=1,
    font={"font_size": 7},
    text_callable="default",
    one_dim_orientation="z",
    # to_file="result_images/spam_dense_z.png",
    save_fig=True,
    save_fig_filename="spam_dense_z.png",
)
plot dense

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

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

Related examples

Visualkeras: Spam Classification Conv1D Dense Example

Visualkeras: Spam Classification Conv1D Dense Example

visualkeras: custom VGG example

visualkeras: custom VGG example

visualkeras: ResNetV2 example

visualkeras: ResNetV2 example

visualkeras: transformers example

visualkeras: transformers example

Gallery generated by Sphinx-Gallery