Thomas Moore
Thomas Moore

Reputation: 981

Keras clarification on definition of hidden layer

I am following a tutorial on building a simple deep neural network in Keras, and the code provided was:

# create model
model = Sequential()
model.add(Dense(12, input_dim=8, activation='relu'))
model.add(Dense(8, activation='relu'))
model.add(Dense(1, activation='sigmoid'))

Is the first model.add line to define the first hidden layer, with 8 inputs in the input layer? Is there thus no need to specify the input layer except for the code input_dim=8?

Upvotes: 3

Views: 233

Answers (1)

Daniel Möller
Daniel Möller

Reputation: 86650

You're right.

When you're creating a Sequential model, the input "layer"* is defined by input_dim or by input_shape, or by batch_input_shape.

* - The input layer is not really a layer, but just a "container" for receiving data in a specific format.

Later you might find it very useful to use functional API models instead of sequential models. In that case, then you will define the input tensor with:

inputs = Input((8,))

And pass this tensor through the layers:

outputs = Dense(12, input_dim=8, activation='relu')(inputs)
outputs = Dense(8, activation='relu')(outputs)
outputs = Dense(1, activation='sigmoid')(outputs)

To create the model:

model = Model(inputs,outputs)

It seems too much trouble at first, but soon you will feel the need to create branches, join models, split models, etc.

Upvotes: 5

Related Questions