Week 1, Assignment 1, ValueError: shapes (5,10) and (5,10) not aligned: 10 (dim 1) != 5 (dim 0)

I’m having troble with the lstm_cell_forward function

My code looks like this:

{moderator edit - solution code removed}

but when I run the test thats what show up:

ValueError                                Traceback (most recent call last)
<ipython-input-17-7dee818208b9> in <module>
     15 parameters_tmp['by'] = np.random.randn(2, 1)
---> 17 a_next_tmp, c_next_tmp, yt_tmp, cache_tmp = lstm_cell_forward(xt_tmp, a_prev_tmp, c_prev_tmp, parameters_tmp)
     19 print("a_next[4] = \n", a_next_tmp[4])

<ipython-input-16-43407a88eb05> in lstm_cell_forward(xt, a_prev, c_prev, parameters)
     56     it = sigmoid(np.dot(Wi, concat) + bi)
     57     cct = np.tanh(np.dot(Wc, concat) + bc)
---> 58     c_next = np.dot(ft, c_prev) + np.dot(it, cct)
     59     ot = sigmoid(Wo + bo)
     60     a_next = np.dot(ot, np.tanh(c_next))

<__array_function__ internals> in dot(*args, **kwargs)

ValueError: shapes (5,10) and (5,10) not aligned: 10 (dim 1) != 5 (dim 0)

What am I doing wrong?

The dimensions on that dot product are obviously wrong. So there must be something in the earlier computations that is wrong. Have you compared them carefully to the formulas shown in the instructions?

Just posting your code and expecting us to fix it for you is a) against the rules and b) robs you of the experience of learning how to debug. Once you finish these courses and try to do this stuff “for real”, who is going to do your debugging for you?

One important thing to realize is that when Prof Ng uses “*” as the operator, that is elementwise multiply, not dot product. That is what the issue is here. You have used the wrong operation, which is why it fails.

Once you fix that, you need to carefully check your code for ot and a_next as well. Both are incorrect.

1 Like

Thanks verry much! And sorry for the code, I was stuck on that for literaly 3 days, I would never have caught the error here.

1 Like

Great! Glad that it helped. Onwards! :nerd_face:


Hi Gabriel, I made the same mistake. I found the instructions for the lab misleading , as we were told explicitly to use np.dot to multiply matrices. But for c<t> and a<t> we must use “*” (element-wise multiply).

Prof Ng is very consistent in his notation. If and only if he uses “*” in the mathematical expression does he mean “elementwise” multiply. When he means dot product style multiply, he simply writes the operands adjacent to one another with no explicit operator. So the information is all there in an unambiguous form if you are careful to observe what is written.