
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How can Tensorflow be used to add dense layers on top using 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.
- Related Articles
- How can Tensorflow be used to compose layers using Python?
- How can Tensorflow be used to implement custom layers?
- How can Tensorflow be used to add two matrices using Python?
- How can Tensorflow be used to construct an object for customized layers?
- How can Tensorflow be used to multiply two matrices using Python?
- How can Tensorflow be used to visualize training results using Python?
- How can Tensorflow be used to instantiate an estimator using Python?
- How can Tensorflow be used to decode the predictions using Python?
- How can Tensorflow be used to build normalization layer using Python?
- How can Tensorflow be used to check the predicrion using Python?
- How can Tensorflow be used to check the predictions using Python?
- How can Tensorflow be used to plot the results using Python?
- How can Tensorflow be used to visualize the data using Python?
- How can Tensorflow be used to standardize the data using Python?
- How can Tensorflow be used to compile the model using Python?
