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

ValueError: could not broadcast input array from shape (5,10) into shape (3,10)

I am wondering if it has to do with how I am initializing a and y_pred with zeros. I’m using the np.zeros function with x as for both of them since x has dimensions nx, m, tx and a and y_pred have dimensions na, m, tx, and ny, m, and tx respectively. I know that nx and ny are the same but maybe na is 1 off from nx becuase of the initial activation? Thats just a thought I had as a possibility.

Thank you. I understand that this is the directions. but at the end of the day I’m just creating an array of zeros of the correct shape. So couldn’t I use any same shaped item to create a zero array? Your point very well be my problem in the sense that I was saying that I’m using x and x might not be the right shape, although close. So my two questions are couldn’t you get the right size array of zeros by any means that arrives at that correct sized array of zeros, and secondly. Is there a discrepancy between the shape of x and shape of a like I was hinting at, such as a being one larger than x becuase of the activation zero part of a. Thanks

shapes and zeros have been haunting me throughout this whole specialization. I feel like I understand them but then a new problem hits me. If Paul were here he’d be getting on my case about using them wrong here again haha. He’s right but still. I tried this way:

a = np.zeros(np.shape(n_a), np.shape(x[1]), np.shape(x[2]))

Sorry, our messages crossed “in midair”. But what does np.shape(n_a) have to do with the value of n_a? Even if n_a were not a scalar, there would still be no relationship between shape and value, right?

n_a is the number of units in the vector. It says to initialize the ‘a’ vector with n_a number of units as index ‘0’ so I was doing that. I can’t use ‘a’ yet because that is what i’m initializing.

to comment on “nothing to do with nx” I understand this, and maybe its bad programming. but if nx was equivalent in shape to na and I had access to nx then couldn’t I still GET what I wanted by using the shape of nx as na(again I realize na and nx may be different sizes). It’s kinda like if tx and ty are equal, I could use their shapes interchangeably. Or I could just hardcode in the numbers.

if n_a as a scalar number of hidden units was 100, then yes I guess it wouldn’t make sense to zero the scalar. By using n_a.shape I was trying to take the say (100,1) array shape that n_a is and zero that.

A scalar doesn’t have a shape, right? Only a numpy array has a shape.

Note that you are given x and a0 as parameters to this function. You can use a0 to find the number of elements in a, right? Have you tried printing what you get from a0.shape?

Right, because a0 is a 2 dimensional numpy array and you’ve “sliced” it on the first dimension. So now try this:

print(f"a0.shape = {a0.shape}")

What does that give you?

This is just basic numpy stuff. I’ve commented at least a couple of times above that the shape of an object and the values of an object are not the same.