Keras: why input layer requires to be an instance of a class layer?

Created on 6 Mar 2017  路  4Comments  路  Source: keras-team/keras

reproducible error:

(X_train, y_train), (X_test, y_test) = mnist.load_data()
model = Sequential([
    Input(shape=(784,)),
    Dense(200, activation='relu'),
    Dropout(0.5),
    Dense(200, activation='relu'),
    Dropout(0.5),
    Dense(100, activation='relu'),
    Dropout(0.5),
    Dense(nb_classes, activation='softmax')
    ])

error:

TypeError: The added layer must be an instance of class Layer. Found: Tensor("input_1:0", shape=(?, 784), |dtype=float32)

Most helpful comment

Hello,

Input doesn't return a layer
https://github.com/fchollet/keras/blob/master/keras/engine/topology.py#L1142

It is a wrapper of InputLayer.

Also, if you are using Sequential, you may also skip the Input and just specify the input_shape for the first Layer.

All 4 comments

Hello,

Input doesn't return a layer
https://github.com/fchollet/keras/blob/master/keras/engine/topology.py#L1142

It is a wrapper of InputLayer.

Also, if you are using Sequential, you may also skip the Input and just specify the input_shape for the first Layer.

@unrealwill thanks. So just to be clear in the functional api you can use it without the any of the above issues?

Hello,

Input doesn't return a layer
https://github.com/fchollet/keras/blob/master/keras/engine/topology.py#L1142

It is a wrapper of InputLayer.

Also, if you are using Sequential, you may also skip the Input and just specify the input_shape for the first Layer.

Could you give an example of how to just specify the input shape?

model = keras.Sequential()

model.add(Conv2D(32,kernel_size=(3,3),activation='relu',input_shape=(IMG_ROWS,IMG_COLS,1)))
model.add(MaxPooling2D(2,2))
model.add(Dropout(0.25))

model.compile(loss=keras.losses.categorical_crossentropy, optimizer = 'adam', metrics = ['accuracy'])

model.summary()

In this way you can enter your input shape.

Was this page helpful?
0 / 5 - 0 ratings