Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to choose the number of units for the Dense layer in the Convoluted neural network for a Image classification problem?

from keras import layers
from keras import models
model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu',
input_shape=(150, 150, 3)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(128, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(128, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Flatten())
model.add(layers.Dense(512, activation='relu'))
model.add(layers.Dense(1, activation='sigmoid'))

While reading the code for a binary classification problem on classifying images as either cats or dogs, in the Dense layer, they used 512 units. How did they come up with that? Is there a formula to get the number of units in the Dense layer. Usually if there are many features, we choose large number of units in the Dense layer.But here how do we identify the features?I know that the output Dense layer has one unit as its a binary classification problem so the out put will either be 0 or 1 by sigmoid function.

like image 478
Savannah Madison Avatar asked Jun 20 '20 10:06

Savannah Madison


People also ask

How do you determine the number of units in a dense layer?

Use the Keras callback ReduceLROnPlateau for this purpose. Set it to monitor validation accuracy and reduce the learning rate if it fails to improve after a specified number of epochs. Documentation is here. Also use the Keras callback ModelCheckpoint to save the model with the lowest validation loss.

How do you choose the number of layers on CNN?

1. The number of hidden neurons should be between the size of the input layer and the size of the output layer. 2. The number of hidden neurons should be 2/3 the size of the input layer, plus the size of the output layer.

How many dense layers do I need?

It's depend more on number of classes. For 20 classes 2 layers 512 should be more then enough. If you want to experiment you can try also 2 x 256 and 2 x 1024. Less then 256 may work too, but you may underutilize power of previous conv layers.

What are units in dense layer?

Units are one of the most basic and necessary parameters of the Keras dense layer which defines the size of the output from the dense layer. It must be a positive integer since it represents the dimensionality of the output vector.


1 Answers

My experience with CNNs is to start out with a simple model initially and evaluate its performance. If you achieve a satisfactory level of training and validation accuracy stop there. If not try adjusting hyper parameters like learning rate to achieve better performance before adding more complexity to your model. I have found using an adjustable learning rate to be helpful in improving model performance. Use the Keras callback ReduceLROnPlateau for this purpose. Set it to monitor validation accuracy and reduce the learning rate if it fails to improve after a specified number of epochs. Documentation is here. Also use the Keras callback ModelCheckpoint to save the model with the lowest validation loss. Documentation is here. If these methods do not achieve the desired level of training accuracy, then you may want to increase the model complexity by adding more nodes to the dense layer or adding additional dense layers. If your model had high training accuracy but poor validation accuracy your model may be over fitting. In this case add a dropout layer. Documentation is here. The issue with adding more complexity to your model is the tendency for it to over fit. So if you increase the nodes in the dense layer or add additional dense layers and have poor validation accuracy you will have to add dropout. In addition you may want to consider alternate approaches to control over fitting like regularizers. Documentation for that is here. For your specific example I think you have more nodes in the dense layer then is needed. Try something like 64 nodes to begin with.

like image 128
Gerry P Avatar answered Nov 15 '22 08:11

Gerry P