# C5W4 EncoderLayer test Error

**URL:** <https://community.deeplearning.ai/t/c5w4-encoderlayer-test-error/176655>\
**Category:** Sequence Models\
**Tags:** coursera-platform\
**Created:** [August 2, 2022, 11:52pm UTC](https://community.deeplearning.ai/t/c5w4-encoderlayer-test-error/176655 "2022-08-02T23:52:43Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![lu6007](https://avatars.discourse-cdn.com/v4/letter/l/bc8723/32.png) [@lu6007](https://community.deeplearning.ai/u/lu6007)\
**Post date:** [August 2, 2022, 11:52pm UTC](https://community.deeplearning.ai/t/c5w4-encoderlayer-test-error/176655/1 "2022-08-02T23:52:43Z")

</div>

Hi,

I am having a hard time understanding and getting the code work. After trying several times, below is the current error message I have got, which seems to be a tensorflow version problem. Has anyone got the same problem? Thanks a lot!

* * *

AttributeError Traceback (most recent call last)  
 in   
1 # UNIT TEST  
----\> 2 EncoderLayer\_test(EncoderLayer)

~/work/W4A1/public\_tests.py in EncoderLayer\_test(target)  
87  
88 assert tf.is\_tensor(encoded), “Wrong type. Output must be a tensor”  
—\> 89 assert tuple(tf.shape(encoded).numpy()) == (1, q.shape[1], q.shape[2]), f"Wrong shape. We expected ((1, {q.shape[1]}, {q.shape[2]}))"  
90  
91 assert np.allclose(encoded.numpy(),

AttributeError: ‘KerasTensor’ object has no attribute ‘numpy’

---

<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:** [August 3, 2022, 2:54am UTC](https://community.deeplearning.ai/t/c5w4-encoderlayer-test-error/176655/2 "2022-08-03T02:54:17Z")

</div>

Are you running the notebook in the Coursera labs environment, or did you install it on your own local or cloud system?

---

<div class="post-metadata">

**Author:** ![lu6007](https://avatars.discourse-cdn.com/v4/letter/l/bc8723/32.png) [@lu6007](https://community.deeplearning.ai/u/lu6007)\
**Post date:** [August 3, 2022, 4:08am UTC](https://community.deeplearning.ai/t/c5w4-encoderlayer-test-error/176655/3 "2022-08-03T04:08:12Z")

</div>

I am running in the Coursera labs environment.

---

<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:** [August 3, 2022, 4:29am UTC](https://community.deeplearning.ai/t/c5w4-encoderlayer-test-error/176655/4 "2022-08-03T04:29:15Z")

</div>

This is interesting output.  
In this EncoderLayer exercise, output from each function (like self.mha, self.layernorm1, …) is tf.Tensor.  
But, if we look at your output, it is KerasTensor, which is typical output from a Keras layer. KerasTensor does not have an attribute numpy as we see in out traceback.

As the next step, my recommendation is to check (print) the output of all steps in EncoderLayer and see how the output is changed from tf.Tensor to KerasTensor. There are only 5 functions that you call. So, adding 5 print statements should guide you what you should do next.  
And, please share the result with us.

---

<div class="post-metadata">

**Author:** ![lu6007](https://avatars.discourse-cdn.com/v4/letter/l/bc8723/32.png) [@lu6007](https://community.deeplearning.ai/u/lu6007)\
**Post date:** [August 3, 2022, 6:00am UTC](https://community.deeplearning.ai/t/c5w4-encoderlayer-test-error/176655/5 "2022-08-03T06:00:30Z")

</div>

Um… Is this the correct way to call self.mha?

```
    attn_output = self.mha(query = query, value = value, training = training, attention_mask = mask)

```

And how do we initialize query and value?

---

<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:** [August 3, 2022, 6:28am UTC](https://community.deeplearning.ai/t/c5w4-encoderlayer-test-error/176655/6 "2022-08-03T06:28:07Z")

</div>

This is for Encoder. So, the role of Multi-head-attention is a self-attention.  
If you look at [tensorflow document](https://www.tensorflow.org/api_docs/python/tf/keras/layers/MultiHeadAttention), MHA takes (query, value, key, attention\_mask, return\_attention\_scores, training) as parameters. And, as the guide says, we can use default values for “return\_attention\_scores” and “training”.  
So, what we need is “query”, “value”, “key”, and “attention\_mask”. And, “attention\_mask” is the easiest one, which is passed as “mask”, as you wrote.  
Then, in the case of self-attention, we use “x” to all “query”, “value” and “key” to create a self-attention weight. That’s the key point.

Hope this helps.

---

<div class="post-metadata">

**Author:** ![lu6007](https://avatars.discourse-cdn.com/v4/letter/l/bc8723/32.png) [@lu6007](https://community.deeplearning.ai/u/lu6007)\
**Post date:** [August 3, 2022, 6:46am UTC](https://community.deeplearning.ai/t/c5w4-encoderlayer-test-error/176655/7 "2022-08-03T06:46:03Z")

</div>

I see. So we set query = x, value = x, key = x, and training = training. This seems to work. Thanks!
