Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keras confusion about number of layers

I'm a bit confused about the number of layers that are used in Keras models. The documentation is rather opaque on the matter.

According to Jason Brownlee the first layer technically consists of two layers, the input layer, specified by input_dim and a hidden layer. See the first questions on his blog.

In all of the Keras documentation the first layer is generally specified as model.add(Dense(number_of_neurons, input_dim=number_of_cols_in_input, activtion=some_activation_function)).

The most basic model we could make would therefore be:

 model = Sequential()  model.add(Dense(1, input_dim = 100, activation = None)) 

Does this model consist of a single layer, where 100 dimensional input is passed through a single input neuron, or does it consist of two layers, first a 100 dimensional input layer and second a 1 dimensional hidden layer?

Further, if I were to specify a model like this, how many layers does it have?

model = Sequential() model.add(Dense(32, input_dim = 100, activation = 'sigmoid')) model.add(Dense(1))) 

Is this a model with 1 input layer, 1 hidden layer, and 1 output layer or is this a model with 1 input layer and 1 output layer?

like image 951
Tom Davidson Avatar asked Jul 26 '17 13:07

Tom Davidson


People also ask

How do you determine the number of hidden layers?

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.

Is more hidden layers better?

A single line will not work. As a result, we must use hidden layers in order to get the best decision boundary. In such case, we may still not use hidden layers but this will affect the classification accuracy. So, it is better to use hidden layers.

How many layers should neural network have?

In fact, there is a theoretical finding by Lippmann in the 1987 paper “An introduction to computing with neural nets” that shows that an MLP with two hidden layers is sufficient for creating classification regions of any desired shape.

How do you determine the number of neurons in the input layer?

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


1 Answers

Your first one consists of a 100 neurons input layer connected to one single output neuron

Your second one consists of a 100 neurons input layer, one hidden layer of 32 neurons and one output layer of one single neuron.

You have to think of your first layer as your input layer (with the same number of neurons as the dimenson, so 100 for you) connected to another layer with as many neuron as you specify (1 in your first case, 32 in the second one)

In Keras what is useful is the command

model.summary() 
like image 77
Nathan Avatar answered Sep 27 '22 21:09

Nathan