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

**URL:** <https://community.deeplearning.ai/t/c4-w1-a2-module-attributeerror-the-layer-has-never-been-called-and-thus-has-no-defined-output-shape/148644>\
**Category:** Convolutional Neural Networks\
**Tags:** coursera-platform\
**Created:** [June 27, 2022, 3:26pm UTC](https://community.deeplearning.ai/t/c4-w1-a2-module-attributeerror-the-layer-has-never-been-called-and-thus-has-no-defined-output-shape/148644 "2022-06-27T15:26:36Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nelson\_Tam](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/nelson_tam/32/19929_2.png) [@Nelson\_Tam](https://community.deeplearning.ai/u/Nelson_Tam)\
**Post date:** [June 27, 2022, 3:26pm UTC](https://community.deeplearning.ai/t/c4-w1-a2-module-attributeerror-the-layer-has-never-been-called-and-thus-has-no-defined-output-shape/148644/1 "2022-06-27T15:26:37Z")

</div>

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)  
 in   
1 happy\_model = happyModel()  
2 # Print a summary for each layer  
----\> 3 for layer in summary(happy\_model):  
4 print(layer)  
5

~/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

---

<div class="post-metadata">

**Author:** ![Nelson\_Tam](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/nelson_tam/32/19929_2.png) [@Nelson\_Tam](https://community.deeplearning.ai/u/Nelson_Tam)\
**Post date:** [June 27, 2022, 3:53pm UTC](https://community.deeplearning.ai/t/c4-w1-a2-module-attributeerror-the-layer-has-never-been-called-and-thus-has-no-defined-output-shape/148644/2 "2022-06-27T15:53:47Z")

</div>

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

> **[tf.keras.layers.ZeroPadding2D  |  TensorFlow Core v2.9.1](https://www.tensorflow.org/api_docs/python/tf/keras/layers/ZeroPadding2D)**
>
> Zero-padding layer for 2D input (e.g. picture).

Tensorflow documentation does not document the supported attribute of input\_shape.

---

<div class="post-metadata">

**Author:** ![Nelson\_Tam](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/nelson_tam/32/19929_2.png) [@Nelson\_Tam](https://community.deeplearning.ai/u/Nelson_Tam)\
**Post date:** [June 27, 2022, 4:10pm UTC](https://community.deeplearning.ai/t/c4-w1-a2-module-attributeerror-the-layer-has-never-been-called-and-thus-has-no-defined-output-shape/148644/3 "2022-06-27T16:10:52Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![anon57530071](https://avatars.discourse-cdn.com/v4/letter/a/46a35a/32.png) [@anon57530071](https://community.deeplearning.ai/u/anon57530071)\
**Post date:** [June 27, 2022, 4:26pm UTC](https://community.deeplearning.ai/t/c4-w1-a2-module-attributeerror-the-layer-has-never-been-called-and-thus-has-no-defined-output-shape/148644/4 "2022-06-27T16:26:44Z")

</div>

Great to hear that you find the solution !

Regarding input\_shape, Keras documents describe it. Here is the [link](https://keras.io/guides/sequential_model/).

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.

 ![image](https://global.discourse-cdn.com/dlai/original/3X/d/2/d2748928fa686f90d16ca7f0e7f07343f88c25e8.png)

Hope this helps some.

---

<div class="post-metadata">

**Author:** ![Nelson\_Tam](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/nelson_tam/32/19929_2.png) [@Nelson\_Tam](https://community.deeplearning.ai/u/Nelson_Tam)\
**Post date:** [June 29, 2022, 1:19pm UTC](https://community.deeplearning.ai/t/c4-w1-a2-module-attributeerror-the-layer-has-never-been-called-and-thus-has-no-defined-output-shape/148644/5 "2022-06-29T13:19:41Z")

</div>

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.

Nelson

---

<div class="post-metadata">

**Author:** ![Alex\_Tu](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/alex_tu/32/94489_2.png) [@Alex\_Tu](https://community.deeplearning.ai/u/Alex_Tu)\
**Post date:** [June 2, 2024, 7:31am UTC](https://community.deeplearning.ai/t/c4-w1-a2-module-attributeerror-the-layer-has-never-been-called-and-thus-has-no-defined-output-shape/148644/6 "2024-06-02T07:31:06Z")

</div>

So, how did you add input shape to tfl.ZeroPadding2D?

Thanks

---

<div class="post-metadata">

**Author:** ![TMosh](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/tmosh/32/9321_2.png) [@TMosh](https://community.deeplearning.ai/u/TMosh)\
**Post date:** [June 3, 2024, 11:37pm UTC](https://community.deeplearning.ai/t/c4-w1-a2-module-attributeerror-the-layer-has-never-been-called-and-thus-has-no-defined-output-shape/148644/7 "2024-06-03T23:37:13Z")

</div>

@Alex_Tu: You’ve added your reply to a thread that has been cold for two years. Usually it’s better to start a new thread for your question.

= = = = =  
The ZeroPadding2D(…) layer can accept an `input_shape = (...)` parameter. You have to provide the desired shape as a tuple.
