Neural Network Model Architecture Visualization | by Himanshu Sharma | Towards Data Science

Neural Network is a subset of Machine Learning which is inspired by the human brain and is used to solve AI-related problems. It is inspired by neurons found in the human brain, it is a network that is connected using different neurons called a Neural Network. It is an advanced field of Data Science that generally helps in solving the problem related to Artificial Intelligence.
Visualizing a neural network can be helpful in understanding how different layers are connected to each other and how many neurons are there between each layer. It is one of the best ways to understand how a black box model looks from the inside. Visualizing can also be helpful by presenting a model to someone to make them understand how different layers of a neural network are actually connected to each other.
Visualkeras is an open-source Python library that is used for creating Neural Network model visualization. It is one of the best libraries for understanding how different layers are connected.
In this article, we will explore Visualkeras and create some visualizations using them.
Let’s get started...
We will start by installing Visualkeras using pip installation. The command given below will install Visualkeras using pip.
pip install visualkeras
In this step, we will import all the libraries that are required for creating the visualizations.
from tensorflow import kerasfrom tensorflow.keras import layersimport visualkeras
Now we will start by creating a neural network model. We will define a model using different layers like Dense, Flatten, Conv2D, etc. Then we will visualize the model after that.
encoder_input = keras.Input(shape=(28, 28, 1), name='img')x = layers.Conv2D(16, 3, activation='relu')(encoder_input)x = layers.Conv2D(32, 3, activation='relu')(x)x = layers.MaxPooling2D(3)(x)x = layers.Conv2D(32, 3, activation='relu')(x)x = layers.Conv2D(16, 3, activation='relu')(x)encoder_output = layers.GlobalMaxPooling2D()(x)encoder = keras.Model(encoder_input, encoder_output, name='encoder')visualkeras.layered_view(encoder, to_file='encoder.png')x = layers.Reshape((4, 4, 1))(encoder_output)x = layers.Conv2DTranspose(16, 3, activation='relu')(x)x = layers.Conv2DTranspose(32, 3, activation='relu')(x)x = layers.UpSampling2D(3)(x)x = layers.Conv2DTranspose(16, 3, activation='relu')(x)decoder_output = layers.Conv2DTranspose(1, 3, activation='relu')(x)autoencoder = keras.Model(encoder_input, decoder_output, name='autoencoder')
Here you can see how we defined a Neural Network Model using different layers. Now let us visualize the Model.
Now we will use Visualkeras to visualize the model.
visualkeras.layered_view(autoencoder, to_file="model.png')
Here we can see how different layers of the Neural Network are connected. This helps in understanding how the model looks from the inside.
Try this with different models, create different visualizations, and let me know your comments in the response section.
This article is in collaboration with Piyush Ingale.
Thanks for reading! If you want to get in touch with me, feel free to reach me at hmix13@gmail.com or my LinkedIn Profile. You can view my Github profile for different data science projects and packages tutorials. Also, feel free to explore my profile and read different articles I have written related to Data Science.