Keras confusion about number of layers

13,934

Solution 1

For your first question, the model is :

1 input layer and 1 output layer.

For the second question :

1 input layer

1 hidden layer

1 activation layer (The sigmoid one)

1 output layer

For the input layer, this is abstracted by Keras with the input_dim arg or input_shape, but you can find this layer in :

from keras.layers import Input

Same for the activation layer.

from keras.layers import Activation

Solution 2

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()
Share:
13,934
Tom Davidson
Author by

Tom Davidson

Updated on June 15, 2022

Comments

  • Tom Davidson
    Tom Davidson almost 2 years

    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?

  • Tom Wyllie
    Tom Wyllie almost 7 years
    beat me to suggesting model.summary ;) it's a lifesaver and I'd highly recommend it to OP.
  • Tom Davidson
    Tom Davidson almost 7 years
    Based on the other answer I'm not totally sure you're right about the first model. I think it is just one input layer and a single output layer without any hidden layers. I think you're correct about the second model though.
  • Pusheen_the_dev
    Pusheen_the_dev almost 7 years
    Hi, indeed, I wrote it too quick. It's composed of 1 input layer (100 neurons) and one output layer. (Composed of 1 neuron) I'm going to edit it
  • Tom Davidson
    Tom Davidson almost 7 years
    Thanks for clarifying
  • DataMan
    DataMan about 6 years
    By activation layer, do you mean activation function?
  • Advika
    Advika about 6 years
    As here we see one hidden layer. Is it possible to add multiple hidden layer?
  • ksha
    ksha over 5 years
    What is an 'Activation Layer'?
  • thepurpleowl
    thepurpleowl over 5 years
    I think similar to first case, for the second case it'll be three layers, one input, one hidden and one output. @Pusheen_the_dev don't you think so?
  • gust
    gust over 4 years
    @Advika I'd be blasted if you still haven't gotten an answer but for posterity's sake: model.add(Dense(32, activation = 'sigmoid')) would add a hidden layer of 32 neurons with sigmoid activation given that you are using model = Sequential(). Another way to add layers is through the keras functional API
  • Edison
    Edison almost 2 years
    Hi. For a binary classification problem with shape (129880, 33) what would you recommend? Maybe model.add(Dense(50, input_shape=(X_train.shape[1],), activation='relu', input_dim=32)) model.add(Dense(1, activation='sigmoid')) ? input_dim=32 represents columns/features right? 50 correspond to a hidden layer with 50 neurons right? How to choose that?