When should I specify the input shape?

In the lecture and also optional lab, input shape is described as ‘optional’ to have. However, in course 2 week 2 exercise 2, it seems like without specifying input size, it results in error. Why sometimes it is required while other times it is optional?
This question is related to C2_W2_Assignment Practice Lab error: This model has not yet been built - #9 by rgaston.

Normally in a designed model with defined number of layers and neurons per layer (deep learning) the input shape needs to be set accordingly. There may be ML models that might not require a predefined input shape maybe like clustering models but I think NN models need the shape of input features.

Hello Yifei @Yifei_Zhang,

If you don’t give it the input_shape, when you fit the model, the model sees your data and it infers the input_shape from your data.

If you don’t give it the input_shape, but trying to print a model summary of it (C2 W2 Ex2), the model doesn’t know how many parameters are needed for the first Dense layer and so it will complain.

input_shape is optional because it is possible for the model to infer it from your data, but only if it sees your data. In general, I would suggest you to specify it unless not specifying is necessary.


1 Like