visualkeras: Spam Dense example#

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

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

pip install protobuf==5.29.4

14 import tensorflow as tf
15
16 # Clear any session to reset the state of TensorFlow/Keras
17 tf.keras.backend.clear_session()
18
19 import tensorflow.python as tf_python
20
21 # Clear any session to reset the state of TensorFlow/Keras
22 tf_python.keras.backend.clear_session()
25 model = tf_python.keras.models.Sequential()
26 model.add(tf_python.keras.layers.InputLayer(input_shape=(100,)))
27
28 # Add Dense layers
29 model.add(tf_python.keras.layers.Dense(64, activation="relu"))  # input_shape=(100,)
30 model.add(tf_python.keras.layers.Dense(32, activation="relu"))
31 model.add(tf_python.keras.layers.Dense(1, activation="sigmoid"))
32
33 # Compile the model
34 model.compile(optimizer="rmsprop", loss="binary_crossentropy", metrics=["accuracy"])
35 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
_________________________________________________________________
38 from scikitplot import visualkeras
41 img_spam = visualkeras.graph_view(
42     model,
43     # to_file="result_images/spam_dense_x.png",
44     save_fig=True,
45     save_fig_filename="spam_dense_graph.png",
46 )
47 img_spam
plot dense
<matplotlib.image.AxesImage object at 0x7f715c6b6310>
50 img_spam = visualkeras.layered_view(
51     model,
52     min_z=1,
53     min_xy=1,
54     max_z=4096,
55     max_xy=4096,
56     scale_z=1,
57     scale_xy=1,
58     font={"font_size": 7},
59     text_callable="default",
60     one_dim_orientation="x",
61     # to_file="result_images/spam_dense_x.png",
62     save_fig=True,
63     save_fig_filename="spam_dense_x.png",
64 )
65 img_spam
plot dense
<matplotlib.image.AxesImage object at 0x7f7154334790>
68 img_spam = visualkeras.layered_view(
69     model,
70     min_z=1,
71     min_xy=1,
72     max_z=4096,
73     max_xy=4096,
74     scale_z=1,
75     scale_xy=1,
76     font={"font_size": 7},
77     text_callable="default",
78     one_dim_orientation="y",
79     # to_file="result_images/spam_dense_y.png",
80     save_fig=True,
81     save_fig_filename="spam_dense_y.png",
82 )
83 img_spam
plot dense
<matplotlib.image.AxesImage object at 0x7f715437af90>
 86 img_spam = visualkeras.layered_view(
 87     model,
 88     min_z=1,
 89     min_xy=1,
 90     max_z=4096,
 91     max_xy=4096,
 92     scale_z=1,
 93     scale_xy=1,
 94     font={"font_size": 7},
 95     text_callable="default",
 96     one_dim_orientation="z",
 97     # to_file="result_images/spam_dense_z.png",
 98     save_fig=True,
 99     save_fig_filename="spam_dense_z.png",
100 )
101 img_spam
plot dense
<matplotlib.image.AxesImage object at 0x7f71543caf90>

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 2.771 seconds)

Related examples

Visualkeras: Spam Classification Conv1D Dense Example

Visualkeras: Spam Classification Conv1D Dense Example

visualkeras: transformers example

visualkeras: transformers example

visualkeras: custom VGG example

visualkeras: custom VGG example

visualkeras: custom vgg16 show dimension example

visualkeras: custom vgg16 show dimension example

Gallery generated by Sphinx-Gallery