-3

I am learning convolution neural network with keras. While implementing convolution2d we do not mention the input shape for the second time. Why is so? Thanks in advance :)

For example:

model = Sequential()

model.add(Convolution2D(32, 3, 3, activation='relu', input_shape=(1,28,28)))
model.add(Convolution2D(32, 3, 3, activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))
desertnaut
  • 57,590
  • 26
  • 140
  • 166

2 Answers2

2

The input shape is only defined on your very first layer. From this link Guide to Sequential modelling

The model needs to know what input shape it should expect. For this reason, the first layer in a Sequential model (and only the first, because following layers can do automatic shape inference) needs to receive information about its input shape. There are several possible ways to do this:

VegardKT
  • 1,226
  • 10
  • 21
  • 1
    Correct (+1), plus that the `input_shape` implicitly defines an input layer, which is explicitly defined in the case of the Functional API: [Keras Sequential model input layer](https://stackoverflow.com/questions/46572674/keras-sequential-model-input-layer/46574294#46574294) – desertnaut Aug 07 '18 at 17:42
0

The input shape is only needed for the first layer because this adds information about the input to the model,which cannot be inferred by Keras, as the data is given by you.

Subsequent layers can have their output shape inferred by Keras as they usually represent fixed transformations, so only the input shape to the first layer is needed.

Dr. Snoopy
  • 55,122
  • 7
  • 121
  • 140