
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Add Dense Layers Using TensorFlow in Python
A dense layer can be added to the sequential model using the ‘add’ method, and specifying the type of layer as ‘Dense’. The layers are first flattened, and then a layer is added. This new layer will be applied to the entire training dataset.
Read More: What is TensorFlow and how Keras work with TensorFlow to create Neural Networks?
We will use the Keras Sequential API, which is helpful in building a sequential model that is used to work with a plain stack of layers, where every layer has exactly one input tensor and one output tensor.
We are using the Google Colaboratory to run the below code. Google Colab or Colaboratory helps run Python code over the browser and requires zero configuration and free access to GPUs (Graphical Processing Units). Colaboratory has been built on top of Jupyter Notebook.
print("Adding dense layer on top") model.add(layers.Flatten()) model.add(layers.Dense(64, activation='relu')) model.add(layers.Dense(10)) print("Complete architecture of the model") model.summary()
Code credit: https://www.tensorflow.org/tutorials/images/cnn
Output
Adding dense layer on top Complete architecture of the model Model: "sequential_1" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= conv2d_3 (Conv2D) (None, 30, 30, 32) 896 _________________________________________________________________ max_pooling2d_2 (MaxPooling2 (None, 15, 15, 32) 0 _________________________________________________________________ conv2d_4 (Conv2D) (None, 13, 13, 64) 18496 _________________________________________________________________ max_pooling2d_3 (MaxPooling2 (None, 6, 6, 64) 0 _________________________________________________________________ conv2d_5 (Conv2D) (None, 4, 4, 64) 36928 _________________________________________________________________ flatten (Flatten) (None, 1024) 0 _________________________________________________________________ dense (Dense) (None, 64) 65600 _________________________________________________________________ dense_1 (Dense) (None, 10) 650 ================================================================= Total params: 122,570 Trainable params: 122,570 Non-trainable params: 0 _________________________________________________________________
Explanation
- To complete the model, the last output tensor from the convolutional base (of shape (4, 4, 64)) is fed to one or more Dense layers to perform classification.
- Dense layers will take vectors as input (which are 1D), and the current output is a 3D tensor.
- Next, the 3D output is flattened to 1D, and one or more Dense layers are added on top.
- CIFAR has 10 output classes, so a final Dense layer with 10 outputs is added.
- The (4, 4, 64) outputs are flattened into vectors of shape (1024) before going through two Dense layers.