# C5 week 4 Transformer pos\_encoding incompatible shapes

**URL:** <https://community.deeplearning.ai/t/c5-week-4-transformer-pos-encoding-incompatible-shapes/25730>\
**Category:** Sequence Models\
**Tags:** coursera-platform\
**Created:** [August 7, 2021, 8:18am UTC](https://community.deeplearning.ai/t/c5-week-4-transformer-pos-encoding-incompatible-shapes/25730 "2021-08-07T08:18:40Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![DLZECX](https://avatars.discourse-cdn.com/v4/letter/d/ba8739/32.png) [@DLZECX](https://community.deeplearning.ai/u/DLZECX)\
**Post date:** [August 7, 2021, 8:18am UTC](https://community.deeplearning.ai/t/c5-week-4-transformer-pos-encoding-incompatible-shapes/25730/1 "2021-08-07T08:18:40Z")

</div>

Hello  
I ran W4A1C5’s

```auto
Encoder_test(Encoder)

```

but I got

```auto
InvalidArgumentError: Incompatible shapes: [2,3,4] vs. [1,5,4] [Op:AddV2]

```

The above two shapes are the shapes of`x.shape` and `self.pos_encoding.shape`

I have passed through C2’s unit test and I stuck here for quite long. Could someone help me with the shape of the `pos_encoding`?

---

<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 7, 2021, 5:52pm UTC](https://community.deeplearning.ai/t/c5-week-4-transformer-pos-encoding-incompatible-shapes/25730/2 "2021-08-07T17:52:48Z")

</div>

Perhaps the problem is in the Encoder() class, in the code you’re using for this line:  
x += self.pos\_encoding[…]

---

<div class="post-metadata">

**Author:** ![DLZECX](https://avatars.discourse-cdn.com/v4/letter/d/ba8739/32.png) [@DLZECX](https://community.deeplearning.ai/u/DLZECX)\
**Post date:** [August 9, 2021, 1:43am UTC](https://community.deeplearning.ai/t/c5-week-4-transformer-pos-encoding-incompatible-shapes/25730/3 "2021-08-09T01:43:39Z")

</div>

Could please you specify my problem?  
I tried  
x += self.pos\_encoding[…]  
and  
x += self.pos\_encoding

They both didn’t work.

---

<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 9, 2021, 4:20am UTC](https://community.deeplearning.ai/t/c5-week-4-transformer-pos-encoding-incompatible-shapes/25730/4 "2021-08-09T04:20:15Z")

</div>

You need to replace the […] with the correct index expression.

---

<div class="post-metadata">

**Author:** ![DLZECX](https://avatars.discourse-cdn.com/v4/letter/d/ba8739/32.png) [@DLZECX](https://community.deeplearning.ai/u/DLZECX)\
**Post date:** [August 9, 2021, 3:30pm UTC](https://community.deeplearning.ai/t/c5-week-4-transformer-pos-encoding-incompatible-shapes/25730/5 "2021-08-09T15:30:58Z")

</div>

Thank you a lot, I’ve solved the problem now.

---

<div class="post-metadata">

**Author:** ![Tom\_Pham](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/tom_pham/32/23356_2.png) [@Tom\_Pham](https://community.deeplearning.ai/u/Tom_Pham)\
**Post date:** [January 30, 2023, 4:20pm UTC](https://community.deeplearning.ai/t/c5-week-4-transformer-pos-encoding-incompatible-shapes/25730/6 "2023-01-30T16:20:00Z")

</div>

The information is located in the instruction, though with a bit of caveat as I mentioned in [this reply](https://community.deeplearning.ai/t/week-4-encoder-error-incompatible-shapes-2-3-4-vs-1-5-4-op-addv2/11932/10).

---

<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:** [March 29, 2023, 5:50am UTC](https://community.deeplearning.ai/t/c5-week-4-transformer-pos-encoding-incompatible-shapes/25730/7 "2023-03-29T05:50:50Z")

</div>


