Keras confusion about number of layers

Viewed 14733

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?

3 Answers
# Create a `Sequential` model and add a Dense layer as the first layer.
model = tf.keras.models.Sequential()
model.add(tf.keras.Input(shape=(16,)))
model.add(tf.keras.layers.Dense(32, activation='relu'))
# Now the model will take as input arrays of shape (None, 16)
# and output arrays of shape (None, 32).
# Note that after the first layer, you don't need to specify
# the size of the input anymore:
model.add(tf.keras.layers.Dense(32))
model.output_shape

(None, 32)

model.layers

[<keras.layers.core.dense.Dense at 0x7f494062e950>, <keras.layers.core.dense.Dense at 0x7f4944048d90>]

model.summary()

Output

it may help you understand clearly

Related