# How can Tensorflow be used to add dense layers on top using Python?

PythonServer Side ProgrammingProgrammingTensorflow

#### Beyond Basic Programming - Intermediate Python

Most Popular

36 Lectures 3 hours

#### Practical Machine Learning using Python

Best Seller

91 Lectures 23.5 hours

#### Practical Data Science using Python

22 Lectures 6 hours

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.

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")
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.
Updated on 20-Feb-2021 07:08:34