C4 W1 A2:Module AttributeError: The layer has never been called and thus has no defined output shape

In Exercise 1 HappyModel

I tried following TensorFlow Keras 2.9.1 documentation and added layers using 1) adding layers directly in constructor , or 2) use model.add() method, but at testing both methods I got the error from the grader

AttributeError Traceback (most recent call last)
1 happy_model = happyModel()
2 # Print a summary for each layer
----> 3 for layer in summary(happy_model):
4 print(layer)

~/work/release/W1A2/test_utils.py in summary(model)
30 result =
31 for layer in model.layers:
—> 32 descriptors = [layer.class.name, layer.output_shape, layer.count_params()]
33 if (type(layer) == Conv2D):
34 descriptors.append(layer.padding)

/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer.py in output_shape(self)
2177 “”"
2178 if not self._inbound_nodes:
→ 2179 raise AttributeError('The layer has never been called ’
2180 ‘and thus has no defined output shape.’)
2181 all_output_shapes = set(

AttributeError: The layer has never been called and thus has no defined output shape.

I need help to understand why the grader does not see the layer I added.

My lab ID is mahftzaa

1 Like

Solution: Found the way to add input shape into Tensorflow layer class.

Tensorflow documentation does not document the supported attribute of input_shape.


Problem now fixed

[‘ZeroPadding2D’, (None, 70, 70, 3), 0, ((3, 3), (3, 3))]
[‘Conv2D’, (None, 64, 64, 32), 4736, ‘valid’, ‘linear’, ‘GlorotUniform’]
[‘BatchNormalization’, (None, 64, 64, 32), 128]
[‘ReLU’, (None, 64, 64, 32), 0]
[‘MaxPooling2D’, (None, 32, 32, 32), 0, (2, 2), (2, 2), ‘valid’]
[‘Flatten’, (None, 32768), 0]
[‘Dense’, (None, 1), 32769, ‘sigmoid’]
All tests passed!

Great to hear that you find the solution !

Regarding input_shape, Keras documents describe it. Here is the link.

And, one thing which may be useful to know is,.
All tensorflow layers such as ZeroPadding2D, Conv2D, … inherit tf.keras.layers.Layer.
So, everything is there.

Hope this helps some.

1 Like

Hi Nobu

Yes, hints are most helpful. I will note the difference between Keras and the Tensorflow layers using Keras API and always check the Keras manual first for any Kera specific options.