Today, In this tutorial, you will discover How to visualize deep learning models in Tensorflow.
The Keras is a high-level API of Tensorflow which provides tools for creating deep learning models. Those models are sometimes complex, to visualize those models Keras provide the utility to plot the Tensorflow model as a graph.
Before visualizing the model, Let’s create a new model first. The following model takes input as the image of (28 * 28) pixels and returns an output as a tensor of ten different classes; that is the length of the output tensor is ten.
Here is a code,
import tensorflow as tf
My_model = tf.keras.Sequential([
tf.keras.Input((28, 28, 1)),
tf.keras.layers.Conv2D(32, 3, activation="relu"),
tf.keras.layers.Conv2D(16, 3, activation="relu"),
tf.keras.layers.Dense(128, activation="relu"),
tf.keras.layers.Dense(64, activation="relu"),
tf.keras.layers.Dense(10, activation="softmax")])
Now, plot this model as a graph using the plot_model method in the tf.keras.utils
tf.keras.utils.plot_model(My_model, show_shapes=True)
Let see another example, In this example, the model will be complex.
The above model was a sequential Model but this model is created with Functional API.
def get_model():
inputs = tf.keras.Input((128, ))
x = tf.keras.layers.Dense(10, "relu")(inputs)
x = tf.keras.layers.Dense(64, activation="relu")(x)
outputs = tf.keras.layers.Dense(10, activation="softmax")(x)
return tf.keras.Model(inputs, outputs)
inputs = tf.keras.Input((128, ))
m1 = get_model()(inputs)
m2 = get_model()(inputs)
m3 = get_model()(inputs)
outputs = tf.keras.layers.average([m1, m2, m3])
avg_model = tf.keras.Model(inputs, outputs)
plotting above model; that is avg_model as a graph
tf.keras.utils.plot_model(avg_model, show_shapes=True)