Esteban Jimenez
Esteban Jimenez

Reputation: 17

How are the input layers in Keras defined?

So I have this assignment to train a very simple neural network. Our dataset has 6 features that are fed into the network and we are required to train it and then predict one output number. The professor gave us the code and basically told us to learn by ourselves lol. So my doubt is, in the following code, in which the layers for the neural network are defined, does the first dense layer defined (the one with 50 nodes) corresponds to the input layer, or is it the first hidden layer? If it's the first hidden layer, how are input layers defined?

Thanks in advance!

def get_compiled_model():
          model = tf.keras.Sequential([      
            tf.keras.layers.Dense(50, activation='relu', input_shape=(6,)),     
            tf.keras.layers.Dense(30, activation='relu'),     
            tf.keras.layers.Dense(30, activation='relu'),     
            tf.keras.layers.Dense(1, activation='linear'),   
   ])

Upvotes: 0

Views: 2903

Answers (2)

Susmit Agrawal
Susmit Agrawal

Reputation: 3764

The first dense layer is the first hidden layer. Keras automatically provides an input layer in Sequential objects, and the number of units is defined by input_shape or input_dim.

You can also explicitly state the input layer as follows:

def get_compiled_model():
    model = tf.keras.Sequential([
        tf.keras.layers.InputLayer((6,)),
        tf.keras.layers.Dense(50, activation='relu'),     
        tf.keras.layers.Dense(30, activation='relu'),     
        tf.keras.layers.Dense(30, activation='relu'),     
        tf.keras.layers.Dense(1, activation='linear'),   
    ])

Upvotes: 3

The Guy with The Hat
The Guy with The Hat

Reputation: 11132

It is the first hidden layer. The input layer isn't defined as a separate layer; it simply consists of the input data, and its size is defined by input_shape=(6,).

Upvotes: 0

Related Questions