C5 week 4 Transformer pos_encoding incompatible shapes

I ran W4A1C5’s


but I got

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

The above two shapes are the shapes ofx.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?

1 Like

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

1 Like

Could please you specify my problem?
I tried
x += self.pos_encoding[…]
x += self.pos_encoding

They both didn’t work.

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


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

The information is located in the instruction, though with a bit of caveat as I mentioned in this reply.