DLS Course 4 [Week 1] convolutional_model

I am struggling with exercise 2 convolutional_model. I got the following error message and can’t find my failure in the code.

AttributeError: ‘Flatten’ object has no attribute ‘shape’

I read the related Q&A, couldn’t solve the problem. Could you help me?

As the error suggests, Flatten layer does not need ‘shape’ on input. A flatten layer flattens the input to a one dimensional vector, based on the shape of channel-height-width of the image (batch direction is NOT flattened). So, an image of size (1, 64, 10) (1 channel, 64 pixels height and 10 width) is converted to a vector with the shape (1, 640). Follow this help page on TensorFlow: tf.keras.layers.Flatten  |  TensorFlow Core v2.4.1 for more details. This operation only changes the shape of the tensor - it doesn’t add more weights to the model

Hi Jade,

In the functional API you have to apply the layers to the tensor that flows through the network.

Have a look here (scroll down to The Functional API):

https://towardsdatascience.com/3-ways-to-build-neural-networks-in-tensorflow-with-the-keras-api-80e92d3b5b7e

Good luck!

2 Likes

Thank you very much! The link is very helpful! I worked it out.

1 Like

Hi, I have the same problem(‘Flatten’ object has no attribute ‘shape’)…how did you fix it?
Thanks so much :slight_smile:

I read though the link above and found out that I didn’t add “(x)” at the end e.g.:
x = Dense(256, “relu”)(x)
outputs = Dense(10, “softmax”)(x)

Thanks very much, it will help me!

Perhaps a more complete answer for this question is:

  • The exercise instructions (and the Keras documentation) don’t clearly say that every layer needs to have a (…) parameter after it, not just the Conv2D and Dense layers.
3 Likes

same here…can anyone explain in detail in dm…

ok i think these notebooks are not that helpful for the best.
for anyone not understanding the comments here,(you will not benefit from them,but once you are done ,they’ll feel not as obscure)to note:
func(params)(input)
and although func api deals with non linearity, theres no non
linearity happening in this assignm.