DLS course 5 Week 1 RNN forward tuple does not support item asisgnment

I have an issue about a tuple for storing an activation in a[:,:,t]

TypeError Traceback (most recent call last)
9 parameters_tmp[‘by’] = np.random.randn(2, 1)
—> 11 a_tmp, y_pred_tmp, caches_tmp = rnn_forward(x_tmp, a0_tmp, parameters_tmp)
12 print(“a[4][1] = \n”, a_tmp[4][1])
13 print(“a.shape = \n”, a_tmp.shape)

in rnn_forward(x, a0, parameters)
43 a_next, yt_pred, cache = rnn_cell_forward(x[:,:,t], a_next, parameters)
44 # Save the value of the new “next” hidden state in a (≈1 line)
—> 45 a[:,:,t] = a_next
46 # Save the value of the prediction in y (≈1 line)
47 y_pred[:,:,t] = yt_pred

TypeError: ‘tuple’ object does not support item assignment

I tried storing a_next into a[:,:,t].

for additional information. I am getting a_next from rnn_cell_forward in which I am using the rnn_cell_forward function with (x[:,:,t], a_next, parameters) as arguments(this passes, may not be right i guess). The original arguments in the original function are xt, a_prev, parameters. I have altered the arguments a little to be able to put in what I have access to.

I note that a_prev and a_next are not the same but I do not have any a_prev, there aren’t too many moving parts in this code block that could be used instead of a_prev besides a_next and a0. Maybe if I could do some form of a_next at a t-1 time point to get a psedo ‘a_prev’ to put into rnn_cell_forward to give me a correct a_next and therefore not error when storing that in a[:,:,t].

These are just my thoughts there might be another reason I can’t store something in a[:,:,t] as a tuple. Any thoughts are welcome.

There may be an error in your rnn_cell_forward() function, which makes the a_next incorrect.

Note that a_next should be a 2D matrix of floats - not a tuple.

1 Like

my a_next from rnn_cell_forward is

a_next = np.tanh((np.dot(Waa, a_prev))+(np.dot(Wax, xt)) + ba) This both clears the cell block and also looks right to me. I use np.dot for multiplication, I use a_prev for a(t-1). I include all of it in the tanh.


a_next = np.tanh((np.dot(Waa, a_prev))+(np.dot(Wax, xt)) + ba)

a_prev appears to be the hidden step at time step t-1. Is that the same as a(t-1). a(t) is hidden state at time step t. so a(t-1) is hidden state at time step t-1. So a(t-1) is a_prev and used correctly in the above a_next equation in rnn_cell_forward? if a_next is wrong here or a tuple rather than float that could explain why my a[:,:,t] = a_next is not working because it is a tuple being assigned.

update for those of you playing along at home. The tuples arising in from initializing ‘a’ and ‘y_pred’ were causing errors. I made them into dtype = int and my program ran but gave me wrong values of ‘a’ and ‘y_pred’…all zeros for final output. Like mentioned above I wanted floats so changing the dtype = float made it work and all tests passed. Something as simple as dtype can be the difference between it not running, running and getting wrong answer, and running and getting right answer.

If anyone can tell me why int’s would give all zeros and floats would give me the correct answer that would be much appreciated. My guess is the function from earlier rnn_cell_forward only can take floats. So with ints it wouldn’t go in and my final values were just the ‘a’ and ‘y_pred’ initialized zero values.