Question to Flatten and input_shape

Hi all,

I have a very basic question about the code snipped:
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(512, activation=tf.nn.relu),
tf.keras.layers.Dense(10, activation=tf.nn.softmax)

To be precise it is about the input_shape=(28,28). In some videos I see that the input shape is set. In other videos the input_shape is not set. i.e., the line reduces to:

Please, can you help me understanding when I have to set the input_shape?

Many thanks

Hello @Dennis_Linke I hope you are having a nice day :).
To answer your question, keras setss the input_shape parameter automaticaly based on the first layer of the model, unless you explicitly sets it. So it is mostly up to you and to your usecase. However, it is recommended to set the input_shape parameter explicitly which can help catching the errors early if the input data does not match the expected shape.
I hope this answers your question :slight_smile:

Hello @Maryam,

many thanks for your quick reply and the explanation. That made everything clear to me :slight_smile:

Best regards